linux/drivers/usb/typec/fusb302/fusb302.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Copyright 2016-2017 Google, Inc
   4 *
   5 * Fairchild FUSB302 Type-C Chip Driver
   6 */
   7
   8#include <linux/debugfs.h>
   9#include <linux/delay.h>
  10#include <linux/errno.h>
  11#include <linux/extcon.h>
  12#include <linux/gpio.h>
  13#include <linux/i2c.h>
  14#include <linux/interrupt.h>
  15#include <linux/kernel.h>
  16#include <linux/module.h>
  17#include <linux/mutex.h>
  18#include <linux/of_device.h>
  19#include <linux/of_gpio.h>
  20#include <linux/pinctrl/consumer.h>
  21#include <linux/proc_fs.h>
  22#include <linux/regulator/consumer.h>
  23#include <linux/sched/clock.h>
  24#include <linux/seq_file.h>
  25#include <linux/slab.h>
  26#include <linux/string.h>
  27#include <linux/types.h>
  28#include <linux/usb/typec.h>
  29#include <linux/usb/tcpm.h>
  30#include <linux/usb/pd.h>
  31#include <linux/workqueue.h>
  32
  33#include "fusb302_reg.h"
  34
  35/*
  36 * When the device is SNK, BC_LVL interrupt is used to monitor cc pins
  37 * for the current capability offered by the SRC. As FUSB302 chip fires
  38 * the BC_LVL interrupt on PD signalings, cc lvl should be handled after
  39 * a delay to avoid measuring on PD activities. The delay is slightly
  40 * longer than PD_T_PD_DEBPUNCE (10-20ms).
  41 */
  42#define T_BC_LVL_DEBOUNCE_DELAY_MS 30
  43
  44enum toggling_mode {
  45        TOGGLINE_MODE_OFF,
  46        TOGGLING_MODE_DRP,
  47        TOGGLING_MODE_SNK,
  48        TOGGLING_MODE_SRC,
  49};
  50
  51static const char * const toggling_mode_name[] = {
  52        [TOGGLINE_MODE_OFF]     = "toggling_OFF",
  53        [TOGGLING_MODE_DRP]     = "toggling_DRP",
  54        [TOGGLING_MODE_SNK]     = "toggling_SNK",
  55        [TOGGLING_MODE_SRC]     = "toggling_SRC",
  56};
  57
  58enum src_current_status {
  59        SRC_CURRENT_DEFAULT,
  60        SRC_CURRENT_MEDIUM,
  61        SRC_CURRENT_HIGH,
  62};
  63
  64static const u8 ra_mda_value[] = {
  65        [SRC_CURRENT_DEFAULT] = 4,      /* 210mV */
  66        [SRC_CURRENT_MEDIUM] = 9,       /* 420mV */
  67        [SRC_CURRENT_HIGH] = 18,        /* 798mV */
  68};
  69
  70static const u8 rd_mda_value[] = {
  71        [SRC_CURRENT_DEFAULT] = 38,     /* 1638mV */
  72        [SRC_CURRENT_MEDIUM] = 38,      /* 1638mV */
  73        [SRC_CURRENT_HIGH] = 61,        /* 2604mV */
  74};
  75
  76#define LOG_BUFFER_ENTRIES      1024
  77#define LOG_BUFFER_ENTRY_SIZE   128
  78
  79struct fusb302_chip {
  80        struct device *dev;
  81        struct i2c_client *i2c_client;
  82        struct tcpm_port *tcpm_port;
  83        struct tcpc_dev tcpc_dev;
  84        struct tcpc_config tcpc_config;
  85
  86        struct regulator *vbus;
  87
  88        int gpio_int_n;
  89        int gpio_int_n_irq;
  90        struct extcon_dev *extcon;
  91
  92        struct workqueue_struct *wq;
  93        struct delayed_work bc_lvl_handler;
  94
  95        atomic_t pm_suspend;
  96        atomic_t i2c_busy;
  97
  98        /* lock for sharing chip states */
  99        struct mutex lock;
 100
 101        /* chip status */
 102        enum toggling_mode toggling_mode;
 103        enum src_current_status src_current_status;
 104        bool intr_togdone;
 105        bool intr_bc_lvl;
 106        bool intr_comp_chng;
 107
 108        /* port status */
 109        bool pull_up;
 110        bool vconn_on;
 111        bool vbus_on;
 112        bool charge_on;
 113        bool vbus_present;
 114        enum typec_cc_polarity cc_polarity;
 115        enum typec_cc_status cc1;
 116        enum typec_cc_status cc2;
 117        u32 snk_pdo[PDO_MAX_OBJECTS];
 118
 119#ifdef CONFIG_DEBUG_FS
 120        struct dentry *dentry;
 121        /* lock for log buffer access */
 122        struct mutex logbuffer_lock;
 123        int logbuffer_head;
 124        int logbuffer_tail;
 125        u8 *logbuffer[LOG_BUFFER_ENTRIES];
 126#endif
 127};
 128
 129/*
 130 * Logging
 131 */
 132
 133#ifdef CONFIG_DEBUG_FS
 134
 135static bool fusb302_log_full(struct fusb302_chip *chip)
 136{
 137        return chip->logbuffer_tail ==
 138                (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
 139}
 140
 141static void _fusb302_log(struct fusb302_chip *chip, const char *fmt,
 142                         va_list args)
 143{
 144        char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
 145        u64 ts_nsec = local_clock();
 146        unsigned long rem_nsec;
 147
 148        if (!chip->logbuffer[chip->logbuffer_head]) {
 149                chip->logbuffer[chip->logbuffer_head] =
 150                                kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
 151                if (!chip->logbuffer[chip->logbuffer_head])
 152                        return;
 153        }
 154
 155        vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
 156
 157        mutex_lock(&chip->logbuffer_lock);
 158
 159        if (fusb302_log_full(chip)) {
 160                chip->logbuffer_head = max(chip->logbuffer_head - 1, 0);
 161                strlcpy(tmpbuffer, "overflow", sizeof(tmpbuffer));
 162        }
 163
 164        if (chip->logbuffer_head < 0 ||
 165            chip->logbuffer_head >= LOG_BUFFER_ENTRIES) {
 166                dev_warn(chip->dev,
 167                         "Bad log buffer index %d\n", chip->logbuffer_head);
 168                goto abort;
 169        }
 170
 171        if (!chip->logbuffer[chip->logbuffer_head]) {
 172                dev_warn(chip->dev,
 173                         "Log buffer index %d is NULL\n", chip->logbuffer_head);
 174                goto abort;
 175        }
 176
 177        rem_nsec = do_div(ts_nsec, 1000000000);
 178        scnprintf(chip->logbuffer[chip->logbuffer_head],
 179                  LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
 180                  (unsigned long)ts_nsec, rem_nsec / 1000,
 181                  tmpbuffer);
 182        chip->logbuffer_head = (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
 183
 184abort:
 185        mutex_unlock(&chip->logbuffer_lock);
 186}
 187
 188static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
 189{
 190        va_list args;
 191
 192        va_start(args, fmt);
 193        _fusb302_log(chip, fmt, args);
 194        va_end(args);
 195}
 196
 197static int fusb302_debug_show(struct seq_file *s, void *v)
 198{
 199        struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
 200        int tail;
 201
 202        mutex_lock(&chip->logbuffer_lock);
 203        tail = chip->logbuffer_tail;
 204        while (tail != chip->logbuffer_head) {
 205                seq_printf(s, "%s\n", chip->logbuffer[tail]);
 206                tail = (tail + 1) % LOG_BUFFER_ENTRIES;
 207        }
 208        if (!seq_has_overflowed(s))
 209                chip->logbuffer_tail = tail;
 210        mutex_unlock(&chip->logbuffer_lock);
 211
 212        return 0;
 213}
 214DEFINE_SHOW_ATTRIBUTE(fusb302_debug);
 215
 216static struct dentry *rootdir;
 217
 218static void fusb302_debugfs_init(struct fusb302_chip *chip)
 219{
 220        mutex_init(&chip->logbuffer_lock);
 221        if (!rootdir)
 222                rootdir = debugfs_create_dir("fusb302", NULL);
 223
 224        chip->dentry = debugfs_create_file(dev_name(chip->dev),
 225                                           S_IFREG | 0444, rootdir,
 226                                           chip, &fusb302_debug_fops);
 227}
 228
 229static void fusb302_debugfs_exit(struct fusb302_chip *chip)
 230{
 231        debugfs_remove(chip->dentry);
 232        debugfs_remove(rootdir);
 233}
 234
 235#else
 236
 237static void fusb302_log(const struct fusb302_chip *chip,
 238                        const char *fmt, ...) { }
 239static void fusb302_debugfs_init(const struct fusb302_chip *chip) { }
 240static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
 241
 242#endif
 243
 244#define FUSB302_RESUME_RETRY 10
 245#define FUSB302_RESUME_RETRY_SLEEP 50
 246
 247static bool fusb302_is_suspended(struct fusb302_chip *chip)
 248{
 249        int retry_cnt;
 250
 251        for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) {
 252                if (atomic_read(&chip->pm_suspend)) {
 253                        dev_err(chip->dev, "i2c: pm suspend, retry %d/%d\n",
 254                                retry_cnt + 1, FUSB302_RESUME_RETRY);
 255                        msleep(FUSB302_RESUME_RETRY_SLEEP);
 256                } else {
 257                        return false;
 258                }
 259        }
 260
 261        return true;
 262}
 263
 264static int fusb302_i2c_write(struct fusb302_chip *chip,
 265                             u8 address, u8 data)
 266{
 267        int ret = 0;
 268
 269        atomic_set(&chip->i2c_busy, 1);
 270
 271        if (fusb302_is_suspended(chip)) {
 272                atomic_set(&chip->i2c_busy, 0);
 273                return -ETIMEDOUT;
 274        }
 275
 276        ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
 277        if (ret < 0)
 278                fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
 279                            data, address, ret);
 280        atomic_set(&chip->i2c_busy, 0);
 281
 282        return ret;
 283}
 284
 285static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
 286                                   u8 length, const u8 *data)
 287{
 288        int ret = 0;
 289
 290        if (length <= 0)
 291                return ret;
 292        atomic_set(&chip->i2c_busy, 1);
 293
 294        if (fusb302_is_suspended(chip)) {
 295                atomic_set(&chip->i2c_busy, 0);
 296                return -ETIMEDOUT;
 297        }
 298
 299        ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
 300                                             length, data);
 301        if (ret < 0)
 302                fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
 303                            address, length, ret);
 304        atomic_set(&chip->i2c_busy, 0);
 305
 306        return ret;
 307}
 308
 309static int fusb302_i2c_read(struct fusb302_chip *chip,
 310                            u8 address, u8 *data)
 311{
 312        int ret = 0;
 313
 314        atomic_set(&chip->i2c_busy, 1);
 315
 316        if (fusb302_is_suspended(chip)) {
 317                atomic_set(&chip->i2c_busy, 0);
 318                return -ETIMEDOUT;
 319        }
 320
 321        ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
 322        *data = (u8)ret;
 323        if (ret < 0)
 324                fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
 325        atomic_set(&chip->i2c_busy, 0);
 326
 327        return ret;
 328}
 329
 330static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
 331                                  u8 length, u8 *data)
 332{
 333        int ret = 0;
 334
 335        if (length <= 0)
 336                return ret;
 337        atomic_set(&chip->i2c_busy, 1);
 338
 339        if (fusb302_is_suspended(chip)) {
 340                atomic_set(&chip->i2c_busy, 0);
 341                return -ETIMEDOUT;
 342        }
 343
 344        ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
 345                                            length, data);
 346        if (ret < 0) {
 347                fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
 348                            address, length, ret);
 349                goto done;
 350        }
 351        if (ret != length) {
 352                fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
 353                            ret, length, address);
 354                ret = -EIO;
 355        }
 356
 357done:
 358        atomic_set(&chip->i2c_busy, 0);
 359
 360        return ret;
 361}
 362
 363static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
 364                                  u8 mask, u8 value)
 365{
 366        int ret = 0;
 367        u8 data;
 368
 369        ret = fusb302_i2c_read(chip, address, &data);
 370        if (ret < 0)
 371                return ret;
 372        data &= ~mask;
 373        data |= value;
 374        ret = fusb302_i2c_write(chip, address, data);
 375        if (ret < 0)
 376                return ret;
 377
 378        return ret;
 379}
 380
 381static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
 382                                u8 set_bits)
 383{
 384        return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
 385}
 386
 387static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
 388                                  u8 clear_bits)
 389{
 390        return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
 391}
 392
 393static int fusb302_sw_reset(struct fusb302_chip *chip)
 394{
 395        int ret = 0;
 396
 397        ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
 398                                FUSB_REG_RESET_SW_RESET);
 399        if (ret < 0)
 400                fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
 401        else
 402                fusb302_log(chip, "sw reset");
 403
 404        return ret;
 405}
 406
 407static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip)
 408{
 409        int ret = 0;
 410
 411        ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
 412                                   FUSB_REG_CONTROL3_N_RETRIES_3 |
 413                                   FUSB_REG_CONTROL3_AUTO_RETRY);
 414
 415        return ret;
 416}
 417
 418/*
 419 * initialize interrupt on the chip
 420 * - unmasked interrupt: VBUS_OK
 421 */
 422static int fusb302_init_interrupt(struct fusb302_chip *chip)
 423{
 424        int ret = 0;
 425
 426        ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
 427                                0xFF & ~FUSB_REG_MASK_VBUSOK);
 428        if (ret < 0)
 429                return ret;
 430        ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
 431        if (ret < 0)
 432                return ret;
 433        ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
 434        if (ret < 0)
 435                return ret;
 436        ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
 437                                     FUSB_REG_CONTROL0_INT_MASK);
 438        if (ret < 0)
 439                return ret;
 440
 441        return ret;
 442}
 443
 444static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
 445{
 446        int ret = 0;
 447
 448        ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
 449
 450        return ret;
 451}
 452
 453static int tcpm_init(struct tcpc_dev *dev)
 454{
 455        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
 456                                                 tcpc_dev);
 457        int ret = 0;
 458        u8 data;
 459
 460        ret = fusb302_sw_reset(chip);
 461        if (ret < 0)
 462                return ret;
 463        ret = fusb302_enable_tx_auto_retries(chip);
 464        if (ret < 0)
 465                return ret;
 466        ret = fusb302_init_interrupt(chip);
 467        if (ret < 0)
 468                return ret;
 469        ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
 470        if (ret < 0)
 471                return ret;
 472        ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
 473        if (ret < 0)
 474                return ret;
 475        chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
 476        ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
 477        if (ret < 0)
 478                return ret;
 479        fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
 480
 481        return ret;
 482}
 483
 484static int tcpm_get_vbus(struct tcpc_dev *dev)
 485{
 486        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
 487                                                 tcpc_dev);
 488        int ret = 0;
 489
 490        mutex_lock(&chip->lock);
 491        ret = chip->vbus_present ? 1 : 0;
 492        mutex_unlock(&chip->lock);
 493
 494        return ret;
 495}
 496
 497static int tcpm_get_current_limit(struct tcpc_dev *dev)
 498{
 499        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
 500                                                 tcpc_dev);
 501        int current_limit = 0;
 502        unsigned long timeout;
 503
 504        if (!chip->extcon)
 505                return 0;
 506
 507        /*
 508         * USB2 Charger detection may still be in progress when we get here,
 509         * this can take upto 600ms, wait 800ms max.
 510         */
 511        timeout = jiffies + msecs_to_jiffies(800);
 512        do {
 513                if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_SDP) == 1)
 514                        current_limit = 500;
 515
 516                if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_CDP) == 1 ||
 517                    extcon_get_state(chip->extcon, EXTCON_CHG_USB_ACA) == 1)
 518                        current_limit = 1500;
 519
 520                if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_DCP) == 1)
 521                        current_limit = 2000;
 522
 523                msleep(50);
 524        } while (current_limit == 0 && time_before(jiffies, timeout));
 525
 526        return current_limit;
 527}
 528
 529static int fusb302_set_cc_pull(struct fusb302_chip *chip,
 530                               bool pull_up, bool pull_down)
 531{
 532        int ret = 0;
 533        u8 data = 0x00;
 534        u8 mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
 535                  FUSB_REG_SWITCHES0_CC2_PU_EN |
 536                  FUSB_REG_SWITCHES0_CC1_PD_EN |
 537                  FUSB_REG_SWITCHES0_CC2_PD_EN;
 538
 539        if (pull_up)
 540                data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
 541                        FUSB_REG_SWITCHES0_CC1_PU_EN :
 542                        FUSB_REG_SWITCHES0_CC2_PU_EN;
 543        if (pull_down)
 544                data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
 545                        FUSB_REG_SWITCHES0_CC2_PD_EN;
 546        ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
 547                                     mask, data);
 548        if (ret < 0)
 549                return ret;
 550        chip->pull_up = pull_up;
 551
 552        return ret;
 553}
 554
 555static int fusb302_set_src_current(struct fusb302_chip *chip,
 556                                   enum src_current_status status)
 557{
 558        int ret = 0;
 559
 560        chip->src_current_status = status;
 561        switch (status) {
 562        case SRC_CURRENT_DEFAULT:
 563                ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
 564                                             FUSB_REG_CONTROL0_HOST_CUR_MASK,
 565                                             FUSB_REG_CONTROL0_HOST_CUR_DEF);
 566                break;
 567        case SRC_CURRENT_MEDIUM:
 568                ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
 569                                             FUSB_REG_CONTROL0_HOST_CUR_MASK,
 570                                             FUSB_REG_CONTROL0_HOST_CUR_MED);
 571                break;
 572        case SRC_CURRENT_HIGH:
 573                ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
 574                                             FUSB_REG_CONTROL0_HOST_CUR_MASK,
 575                                             FUSB_REG_CONTROL0_HOST_CUR_HIGH);
 576                break;
 577        default:
 578                break;
 579        }
 580
 581        return ret;
 582}
 583
 584static int fusb302_set_toggling(struct fusb302_chip *chip,
 585                                enum toggling_mode mode)
 586{
 587        int ret = 0;
 588
 589        /* first disable toggling */
 590        ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
 591                                     FUSB_REG_CONTROL2_TOGGLE);
 592        if (ret < 0)
 593                return ret;
 594        /* mask interrupts for SRC or SNK */
 595        ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
 596                                   FUSB_REG_MASK_BC_LVL |
 597                                   FUSB_REG_MASK_COMP_CHNG);
 598        if (ret < 0)
 599                return ret;
 600        chip->intr_bc_lvl = false;
 601        chip->intr_comp_chng = false;
 602        /* configure toggling mode: none/snk/src/drp */
 603        switch (mode) {
 604        case TOGGLINE_MODE_OFF:
 605                ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
 606                                             FUSB_REG_CONTROL2_MODE_MASK,
 607                                             FUSB_REG_CONTROL2_MODE_NONE);
 608                if (ret < 0)
 609                        return ret;
 610                break;
 611        case TOGGLING_MODE_SNK:
 612                ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
 613                                             FUSB_REG_CONTROL2_MODE_MASK,
 614                                             FUSB_REG_CONTROL2_MODE_UFP);
 615                if (ret < 0)
 616                        return ret;
 617                break;
 618        case TOGGLING_MODE_SRC:
 619                ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
 620                                             FUSB_REG_CONTROL2_MODE_MASK,
 621                                             FUSB_REG_CONTROL2_MODE_DFP);
 622                if (ret < 0)
 623                        return ret;
 624                break;
 625        case TOGGLING_MODE_DRP:
 626                ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
 627                                             FUSB_REG_CONTROL2_MODE_MASK,
 628                                             FUSB_REG_CONTROL2_MODE_DRP);
 629                if (ret < 0)
 630                        return ret;
 631                break;
 632        default:
 633                break;
 634        }
 635
 636        if (mode == TOGGLINE_MODE_OFF) {
 637                /* mask TOGDONE interrupt */
 638                ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
 639                                           FUSB_REG_MASKA_TOGDONE);
 640                if (ret < 0)
 641                        return ret;
 642                chip->intr_togdone = false;
 643        } else {
 644                /* unmask TOGDONE interrupt */
 645                ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
 646                                             FUSB_REG_MASKA_TOGDONE);
 647                if (ret < 0)
 648                        return ret;
 649                chip->intr_togdone = true;
 650                /* start toggling */
 651                ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
 652                                           FUSB_REG_CONTROL2_TOGGLE);
 653                if (ret < 0)
 654                        return ret;
 655                /* during toggling, consider cc as Open */
 656                chip->cc1 = TYPEC_CC_OPEN;
 657                chip->cc2 = TYPEC_CC_OPEN;
 658        }
 659        chip->toggling_mode = mode;
 660
 661        return ret;
 662}
 663
 664static const char * const typec_cc_status_name[] = {
 665        [TYPEC_CC_OPEN]         = "Open",
 666        [TYPEC_CC_RA]           = "Ra",
 667        [TYPEC_CC_RD]           = "Rd",
 668        [TYPEC_CC_RP_DEF]       = "Rp-def",
 669        [TYPEC_CC_RP_1_5]       = "Rp-1.5",
 670        [TYPEC_CC_RP_3_0]       = "Rp-3.0",
 671};
 672
 673static const enum src_current_status cc_src_current[] = {
 674        [TYPEC_CC_OPEN]         = SRC_CURRENT_DEFAULT,
 675        [TYPEC_CC_RA]           = SRC_CURRENT_DEFAULT,
 676        [TYPEC_CC_RD]           = SRC_CURRENT_DEFAULT,
 677        [TYPEC_CC_RP_DEF]       = SRC_CURRENT_DEFAULT,
 678        [TYPEC_CC_RP_1_5]       = SRC_CURRENT_MEDIUM,
 679        [TYPEC_CC_RP_3_0]       = SRC_CURRENT_HIGH,
 680};
 681
 682static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
 683{
 684        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
 685                                                 tcpc_dev);
 686        int ret = 0;
 687        bool pull_up, pull_down;
 688        u8 rd_mda;
 689
 690        mutex_lock(&chip->lock);
 691        switch (cc) {
 692        case TYPEC_CC_OPEN:
 693                pull_up = false;
 694                pull_down = false;
 695                break;
 696        case TYPEC_CC_RD:
 697                pull_up = false;
 698                pull_down = true;
 699                break;
 700        case TYPEC_CC_RP_DEF:
 701        case TYPEC_CC_RP_1_5:
 702        case TYPEC_CC_RP_3_0:
 703                pull_up = true;
 704                pull_down = false;
 705                break;
 706        default:
 707                fusb302_log(chip, "unsupported cc value %s",
 708                            typec_cc_status_name[cc]);
 709                ret = -EINVAL;
 710                goto done;
 711        }
 712        ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
 713        if (ret < 0) {
 714                fusb302_log(chip, "cannot stop toggling, ret=%d", ret);
 715                goto done;
 716        }
 717        ret = fusb302_set_cc_pull(chip, pull_up, pull_down);
 718        if (ret < 0) {
 719                fusb302_log(chip,
 720                            "cannot set cc pulling up %s, down %s, ret = %d",
 721                            pull_up ? "True" : "False",
 722                            pull_down ? "True" : "False",
 723                            ret);
 724                goto done;
 725        }
 726        /* reset the cc status */
 727        chip->cc1 = TYPEC_CC_OPEN;
 728        chip->cc2 = TYPEC_CC_OPEN;
 729        /* adjust current for SRC */
 730        if (pull_up) {
 731                ret = fusb302_set_src_current(chip, cc_src_current[cc]);
 732                if (ret < 0) {
 733                        fusb302_log(chip, "cannot set src current %s, ret=%d",
 734                                    typec_cc_status_name[cc], ret);
 735                        goto done;
 736                }
 737        }
 738        /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
 739        if (pull_up) {
 740                rd_mda = rd_mda_value[cc_src_current[cc]];
 741                ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
 742                if (ret < 0) {
 743                        fusb302_log(chip,
 744                                    "cannot set SRC measure value, ret=%d",
 745                                    ret);
 746                        goto done;
 747                }
 748                ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
 749                                             FUSB_REG_MASK_BC_LVL |
 750                                             FUSB_REG_MASK_COMP_CHNG,
 751                                             FUSB_REG_MASK_COMP_CHNG);
 752                if (ret < 0) {
 753                        fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
 754                                    ret);
 755                        goto done;
 756                }
 757                chip->intr_bc_lvl = false;
 758                chip->intr_comp_chng = true;
 759        }
 760        if (pull_down) {
 761                ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
 762                                             FUSB_REG_MASK_BC_LVL |
 763                                             FUSB_REG_MASK_COMP_CHNG,
 764                                             FUSB_REG_MASK_BC_LVL);
 765                if (ret < 0) {
 766                        fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
 767                                    ret);
 768                        goto done;
 769                }
 770                chip->intr_bc_lvl = true;
 771                chip->intr_comp_chng = false;
 772        }
 773        fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
 774done:
 775        mutex_unlock(&chip->lock);
 776
 777        return ret;
 778}
 779
 780static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
 781                       enum typec_cc_status *cc2)
 782{
 783        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
 784                                                 tcpc_dev);
 785
 786        mutex_lock(&chip->lock);
 787        *cc1 = chip->cc1;
 788        *cc2 = chip->cc2;
 789        fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
 790                    typec_cc_status_name[*cc2]);
 791        mutex_unlock(&chip->lock);
 792
 793        return 0;
 794}
 795
 796static int tcpm_set_polarity(struct tcpc_dev *dev,
 797                             enum typec_cc_polarity polarity)
 798{
 799        return 0;
 800}
 801
 802static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
 803{
 804        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
 805                                                 tcpc_dev);
 806        int ret = 0;
 807        u8 switches0_data = 0x00;
 808        u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
 809                            FUSB_REG_SWITCHES0_VCONN_CC2;
 810
 811        mutex_lock(&chip->lock);
 812        if (chip->vconn_on == on) {
 813                fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
 814                goto done;
 815        }
 816        if (on) {
 817                switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
 818                                 FUSB_REG_SWITCHES0_VCONN_CC2 :
 819                                 FUSB_REG_SWITCHES0_VCONN_CC1;
 820        }
 821        ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
 822                                     switches0_mask, switches0_data);
 823        if (ret < 0)
 824                goto done;
 825        chip->vconn_on = on;
 826        fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
 827done:
 828        mutex_unlock(&chip->lock);
 829
 830        return ret;
 831}
 832
 833static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
 834{
 835        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
 836                                                 tcpc_dev);
 837        int ret = 0;
 838
 839        mutex_lock(&chip->lock);
 840        if (chip->vbus_on == on) {
 841                fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
 842        } else {
 843                if (on)
 844                        ret = regulator_enable(chip->vbus);
 845                else
 846                        ret = regulator_disable(chip->vbus);
 847                if (ret < 0) {
 848                        fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
 849                                    on ? "enable" : "disable", ret);
 850                        goto done;
 851                }
 852                chip->vbus_on = on;
 853                fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
 854        }
 855        if (chip->charge_on == charge)
 856                fusb302_log(chip, "charge is already %s",
 857                            charge ? "On" : "Off");
 858        else
 859                chip->charge_on = charge;
 860
 861done:
 862        mutex_unlock(&chip->lock);
 863
 864        return ret;
 865}
 866
 867static int tcpm_set_current_limit(struct tcpc_dev *dev, u32 max_ma, u32 mv)
 868{
 869        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
 870                                                 tcpc_dev);
 871
 872        fusb302_log(chip, "current limit: %d ma, %d mv (not implemented)",
 873                    max_ma, mv);
 874
 875        return 0;
 876}
 877
 878static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
 879{
 880        return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
 881                                    FUSB_REG_CONTROL0_TX_FLUSH);
 882}
 883
 884static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
 885{
 886        return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
 887                                    FUSB_REG_CONTROL1_RX_FLUSH);
 888}
 889
 890static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
 891{
 892        if (on)
 893                return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
 894                                            FUSB_REG_SWITCHES1_AUTO_GCRC);
 895        return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
 896                                            FUSB_REG_SWITCHES1_AUTO_GCRC);
 897}
 898
 899static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
 900{
 901        int ret = 0;
 902        u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
 903        u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
 904                              FUSB_REG_MASKA_HARDSENT |
 905                              FUSB_REG_MASKA_TX_SUCCESS |
 906                              FUSB_REG_MASKA_HARDRESET;
 907        u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
 908
 909        ret = on ?
 910                fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
 911                fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
 912        if (ret < 0)
 913                return ret;
 914        ret = on ?
 915                fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
 916                fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
 917        if (ret < 0)
 918                return ret;
 919        ret = on ?
 920                fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
 921                fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
 922        return ret;
 923}
 924
 925static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
 926{
 927        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
 928                                                 tcpc_dev);
 929        int ret = 0;
 930
 931        mutex_lock(&chip->lock);
 932        ret = fusb302_pd_rx_flush(chip);
 933        if (ret < 0) {
 934                fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
 935                goto done;
 936        }
 937        ret = fusb302_pd_tx_flush(chip);
 938        if (ret < 0) {
 939                fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
 940                goto done;
 941        }
 942        ret = fusb302_pd_set_auto_goodcrc(chip, on);
 943        if (ret < 0) {
 944                fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
 945                            on ? "on" : "off", ret);
 946                goto done;
 947        }
 948        ret = fusb302_pd_set_interrupts(chip, on);
 949        if (ret < 0) {
 950                fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
 951                            on ? "on" : "off", ret);
 952                goto done;
 953        }
 954        fusb302_log(chip, "pd := %s", on ? "on" : "off");
 955done:
 956        mutex_unlock(&chip->lock);
 957
 958        return ret;
 959}
 960
 961static const char * const typec_role_name[] = {
 962        [TYPEC_SINK]            = "Sink",
 963        [TYPEC_SOURCE]          = "Source",
 964};
 965
 966static const char * const typec_data_role_name[] = {
 967        [TYPEC_DEVICE]          = "Device",
 968        [TYPEC_HOST]            = "Host",
 969};
 970
 971static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
 972                          enum typec_role pwr, enum typec_data_role data)
 973{
 974        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
 975                                                 tcpc_dev);
 976        int ret = 0;
 977        u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
 978                            FUSB_REG_SWITCHES1_DATAROLE;
 979        u8 switches1_data = 0x00;
 980
 981        mutex_lock(&chip->lock);
 982        if (pwr == TYPEC_SOURCE)
 983                switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
 984        if (data == TYPEC_HOST)
 985                switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
 986        ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
 987                                     switches1_mask, switches1_data);
 988        if (ret < 0) {
 989                fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
 990                            typec_role_name[pwr], typec_data_role_name[data],
 991                            ret);
 992                goto done;
 993        }
 994        fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
 995                    typec_data_role_name[data]);
 996done:
 997        mutex_unlock(&chip->lock);
 998
 999        return ret;
1000}
1001
1002static int tcpm_start_drp_toggling(struct tcpc_dev *dev,
1003                                   enum typec_cc_status cc)
1004{
1005        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1006                                                 tcpc_dev);
1007        int ret = 0;
1008
1009        mutex_lock(&chip->lock);
1010        ret = fusb302_set_src_current(chip, cc_src_current[cc]);
1011        if (ret < 0) {
1012                fusb302_log(chip, "unable to set src current %s, ret=%d",
1013                            typec_cc_status_name[cc], ret);
1014                goto done;
1015        }
1016        ret = fusb302_set_toggling(chip, TOGGLING_MODE_DRP);
1017        if (ret < 0) {
1018                fusb302_log(chip,
1019                            "unable to start drp toggling, ret=%d", ret);
1020                goto done;
1021        }
1022        fusb302_log(chip, "start drp toggling");
1023done:
1024        mutex_unlock(&chip->lock);
1025
1026        return ret;
1027}
1028
1029static int fusb302_pd_send_message(struct fusb302_chip *chip,
1030                                   const struct pd_message *msg)
1031{
1032        int ret = 0;
1033        u8 buf[40];
1034        u8 pos = 0;
1035        int len;
1036
1037        /* SOP tokens */
1038        buf[pos++] = FUSB302_TKN_SYNC1;
1039        buf[pos++] = FUSB302_TKN_SYNC1;
1040        buf[pos++] = FUSB302_TKN_SYNC1;
1041        buf[pos++] = FUSB302_TKN_SYNC2;
1042
1043        len = pd_header_cnt_le(msg->header) * 4;
1044        /* plug 2 for header */
1045        len += 2;
1046        if (len > 0x1F) {
1047                fusb302_log(chip,
1048                            "PD message too long %d (incl. header)", len);
1049                return -EINVAL;
1050        }
1051        /* packsym tells the FUSB302 chip that the next X bytes are payload */
1052        buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
1053        memcpy(&buf[pos], &msg->header, sizeof(msg->header));
1054        pos += sizeof(msg->header);
1055
1056        len -= 2;
1057        memcpy(&buf[pos], msg->payload, len);
1058        pos += len;
1059
1060        /* CRC */
1061        buf[pos++] = FUSB302_TKN_JAMCRC;
1062        /* EOP */
1063        buf[pos++] = FUSB302_TKN_EOP;
1064        /* turn tx off after sending message */
1065        buf[pos++] = FUSB302_TKN_TXOFF;
1066        /* start transmission */
1067        buf[pos++] = FUSB302_TKN_TXON;
1068
1069        ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
1070        if (ret < 0)
1071                return ret;
1072        fusb302_log(chip, "sending PD message header: %x", msg->header);
1073        fusb302_log(chip, "sending PD message len: %d", len);
1074
1075        return ret;
1076}
1077
1078static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1079{
1080        return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1081                                    FUSB_REG_CONTROL3_SEND_HARDRESET);
1082}
1083
1084static const char * const transmit_type_name[] = {
1085        [TCPC_TX_SOP]                   = "SOP",
1086        [TCPC_TX_SOP_PRIME]             = "SOP'",
1087        [TCPC_TX_SOP_PRIME_PRIME]       = "SOP''",
1088        [TCPC_TX_SOP_DEBUG_PRIME]       = "DEBUG'",
1089        [TCPC_TX_SOP_DEBUG_PRIME_PRIME] = "DEBUG''",
1090        [TCPC_TX_HARD_RESET]            = "HARD_RESET",
1091        [TCPC_TX_CABLE_RESET]           = "CABLE_RESET",
1092        [TCPC_TX_BIST_MODE_2]           = "BIST_MODE_2",
1093};
1094
1095static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1096                            const struct pd_message *msg)
1097{
1098        struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1099                                                 tcpc_dev);
1100        int ret = 0;
1101
1102        mutex_lock(&chip->lock);
1103        switch (type) {
1104        case TCPC_TX_SOP:
1105                ret = fusb302_pd_send_message(chip, msg);
1106                if (ret < 0)
1107                        fusb302_log(chip,
1108                                    "cannot send PD message, ret=%d", ret);
1109                break;
1110        case TCPC_TX_HARD_RESET:
1111                ret = fusb302_pd_send_hardreset(chip);
1112                if (ret < 0)
1113                        fusb302_log(chip,
1114                                    "cannot send hardreset, ret=%d", ret);
1115                break;
1116        default:
1117                fusb302_log(chip, "type %s not supported",
1118                            transmit_type_name[type]);
1119                ret = -EINVAL;
1120        }
1121        mutex_unlock(&chip->lock);
1122
1123        return ret;
1124}
1125
1126static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1127{
1128        if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1129                return TYPEC_CC_RP_3_0;
1130        if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1131                return TYPEC_CC_RP_1_5;
1132        if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1133                return TYPEC_CC_RP_DEF;
1134        return TYPEC_CC_OPEN;
1135}
1136
1137static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1138{
1139        struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1140                                                 bc_lvl_handler.work);
1141        int ret = 0;
1142        u8 status0;
1143        u8 bc_lvl;
1144        enum typec_cc_status cc_status;
1145
1146        mutex_lock(&chip->lock);
1147        if (!chip->intr_bc_lvl) {
1148                fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1149                goto done;
1150        }
1151        ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1152        if (ret < 0)
1153                goto done;
1154        fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1155        if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1156                fusb302_log(chip, "CC activities detected, delay handling");
1157                mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1158                                 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1159                goto done;
1160        }
1161        bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1162        cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1163        if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1164                if (chip->cc1 != cc_status) {
1165                        fusb302_log(chip, "cc1: %s -> %s",
1166                                    typec_cc_status_name[chip->cc1],
1167                                    typec_cc_status_name[cc_status]);
1168                        chip->cc1 = cc_status;
1169                        tcpm_cc_change(chip->tcpm_port);
1170                }
1171        } else {
1172                if (chip->cc2 != cc_status) {
1173                        fusb302_log(chip, "cc2: %s -> %s",
1174                                    typec_cc_status_name[chip->cc2],
1175                                    typec_cc_status_name[cc_status]);
1176                        chip->cc2 = cc_status;
1177                        tcpm_cc_change(chip->tcpm_port);
1178                }
1179        }
1180
1181done:
1182        mutex_unlock(&chip->lock);
1183}
1184
1185#define PDO_FIXED_FLAGS \
1186        (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1187
1188static const u32 src_pdo[] = {
1189        PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1190};
1191
1192static const u32 snk_pdo[] = {
1193        PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1194};
1195
1196static const struct tcpc_config fusb302_tcpc_config = {
1197        .src_pdo = src_pdo,
1198        .nr_src_pdo = ARRAY_SIZE(src_pdo),
1199        .operating_snk_mw = 2500,
1200        .type = TYPEC_PORT_DRP,
1201        .data = TYPEC_PORT_DRD,
1202        .default_role = TYPEC_SINK,
1203        .alt_modes = NULL,
1204};
1205
1206static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1207{
1208        fusb302_tcpc_dev->init = tcpm_init;
1209        fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1210        fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
1211        fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1212        fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1213        fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1214        fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1215        fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1216        fusb302_tcpc_dev->set_current_limit = tcpm_set_current_limit;
1217        fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1218        fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1219        fusb302_tcpc_dev->start_drp_toggling = tcpm_start_drp_toggling;
1220        fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1221}
1222
1223static const char * const cc_polarity_name[] = {
1224        [TYPEC_POLARITY_CC1]    = "Polarity_CC1",
1225        [TYPEC_POLARITY_CC2]    = "Polarity_CC2",
1226};
1227
1228static int fusb302_set_cc_polarity(struct fusb302_chip *chip,
1229                                   enum typec_cc_polarity cc_polarity)
1230{
1231        int ret = 0;
1232        u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
1233                            FUSB_REG_SWITCHES0_CC2_PU_EN |
1234                            FUSB_REG_SWITCHES0_VCONN_CC1 |
1235                            FUSB_REG_SWITCHES0_VCONN_CC2 |
1236                            FUSB_REG_SWITCHES0_MEAS_CC1 |
1237                            FUSB_REG_SWITCHES0_MEAS_CC2;
1238        u8 switches0_data = 0x00;
1239        u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
1240                            FUSB_REG_SWITCHES1_TXCC2_EN;
1241        u8 switches1_data = 0x00;
1242
1243        if (cc_polarity == TYPEC_POLARITY_CC1) {
1244                switches0_data = FUSB_REG_SWITCHES0_MEAS_CC1;
1245                if (chip->vconn_on)
1246                        switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1247                if (chip->pull_up)
1248                        switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1249                switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1250        } else {
1251                switches0_data = FUSB_REG_SWITCHES0_MEAS_CC2;
1252                if (chip->vconn_on)
1253                        switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1254                if (chip->pull_up)
1255                        switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1256                switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1257        }
1258        ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
1259                                     switches0_mask, switches0_data);
1260        if (ret < 0)
1261                return ret;
1262        ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1263                                     switches1_mask, switches1_data);
1264        if (ret < 0)
1265                return ret;
1266        chip->cc_polarity = cc_polarity;
1267
1268        return ret;
1269}
1270
1271static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1272                                      u8 togdone_result)
1273{
1274        int ret = 0;
1275        u8 status0;
1276        u8 bc_lvl;
1277        enum typec_cc_polarity cc_polarity;
1278        enum typec_cc_status cc_status_active, cc1, cc2;
1279
1280        /* set pull_up, pull_down */
1281        ret = fusb302_set_cc_pull(chip, false, true);
1282        if (ret < 0) {
1283                fusb302_log(chip, "cannot set cc to pull down, ret=%d", ret);
1284                return ret;
1285        }
1286        /* set polarity */
1287        cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1288                      TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1289        ret = fusb302_set_cc_polarity(chip, cc_polarity);
1290        if (ret < 0) {
1291                fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1292                            cc_polarity_name[cc_polarity], ret);
1293                return ret;
1294        }
1295        /* fusb302_set_cc_polarity() has set the correct measure block */
1296        ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1297        if (ret < 0)
1298                return ret;
1299        bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1300        cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1301        /* restart toggling if the cc status on the active line is OPEN */
1302        if (cc_status_active == TYPEC_CC_OPEN) {
1303                fusb302_log(chip, "restart toggling as CC_OPEN detected");
1304                ret = fusb302_set_toggling(chip, chip->toggling_mode);
1305                return ret;
1306        }
1307        /* update tcpm with the new cc value */
1308        cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1309              cc_status_active : TYPEC_CC_OPEN;
1310        cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1311              cc_status_active : TYPEC_CC_OPEN;
1312        if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1313                chip->cc1 = cc1;
1314                chip->cc2 = cc2;
1315                tcpm_cc_change(chip->tcpm_port);
1316        }
1317        /* turn off toggling */
1318        ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
1319        if (ret < 0) {
1320                fusb302_log(chip,
1321                            "cannot set toggling mode off, ret=%d", ret);
1322                return ret;
1323        }
1324        /* unmask bc_lvl interrupt */
1325        ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1326        if (ret < 0) {
1327                fusb302_log(chip,
1328                            "cannot unmask bc_lcl interrupt, ret=%d", ret);
1329                return ret;
1330        }
1331        chip->intr_bc_lvl = true;
1332        fusb302_log(chip, "detected cc1=%s, cc2=%s",
1333                    typec_cc_status_name[cc1],
1334                    typec_cc_status_name[cc2]);
1335
1336        return ret;
1337}
1338
1339static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1340                                      u8 togdone_result)
1341{
1342        /*
1343         * - set polarity (measure cc, vconn, tx)
1344         * - set pull_up, pull_down
1345         * - set cc1, cc2, and update to tcpm_port
1346         * - set I_COMP interrupt on
1347         */
1348        int ret = 0;
1349        u8 status0;
1350        u8 ra_mda = ra_mda_value[chip->src_current_status];
1351        u8 rd_mda = rd_mda_value[chip->src_current_status];
1352        bool ra_comp, rd_comp;
1353        enum typec_cc_polarity cc_polarity;
1354        enum typec_cc_status cc_status_active, cc1, cc2;
1355
1356        /* set pull_up, pull_down */
1357        ret = fusb302_set_cc_pull(chip, true, false);
1358        if (ret < 0) {
1359                fusb302_log(chip, "cannot set cc to pull up, ret=%d", ret);
1360                return ret;
1361        }
1362        /* set polarity */
1363        cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1) ?
1364                      TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1365        ret = fusb302_set_cc_polarity(chip, cc_polarity);
1366        if (ret < 0) {
1367                fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1368                            cc_polarity_name[cc_polarity], ret);
1369                return ret;
1370        }
1371        /* fusb302_set_cc_polarity() has set the correct measure block */
1372        ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1373        if (ret < 0)
1374                return ret;
1375        usleep_range(50, 100);
1376        ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1377        if (ret < 0)
1378                return ret;
1379        rd_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
1380        if (!rd_comp) {
1381                ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1382                if (ret < 0)
1383                        return ret;
1384                usleep_range(50, 100);
1385                ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1386                if (ret < 0)
1387                        return ret;
1388                ra_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
1389        }
1390        if (rd_comp)
1391                cc_status_active = TYPEC_CC_OPEN;
1392        else if (ra_comp)
1393                cc_status_active = TYPEC_CC_RD;
1394        else
1395                /* Ra is not supported, report as Open */
1396                cc_status_active = TYPEC_CC_OPEN;
1397        /* restart toggling if the cc status on the active line is OPEN */
1398        if (cc_status_active == TYPEC_CC_OPEN) {
1399                fusb302_log(chip, "restart toggling as CC_OPEN detected");
1400                ret = fusb302_set_toggling(chip, chip->toggling_mode);
1401                return ret;
1402        }
1403        /* update tcpm with the new cc value */
1404        cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1405              cc_status_active : TYPEC_CC_OPEN;
1406        cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1407              cc_status_active : TYPEC_CC_OPEN;
1408        if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1409                chip->cc1 = cc1;
1410                chip->cc2 = cc2;
1411                tcpm_cc_change(chip->tcpm_port);
1412        }
1413        /* turn off toggling */
1414        ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
1415        if (ret < 0) {
1416                fusb302_log(chip,
1417                            "cannot set toggling mode off, ret=%d", ret);
1418                return ret;
1419        }
1420        /* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1421        ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1422        if (ret < 0)
1423                return ret;
1424        /* unmask comp_chng interrupt */
1425        ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1426                                     FUSB_REG_MASK_COMP_CHNG);
1427        if (ret < 0) {
1428                fusb302_log(chip,
1429                            "cannot unmask bc_lcl interrupt, ret=%d", ret);
1430                return ret;
1431        }
1432        chip->intr_comp_chng = true;
1433        fusb302_log(chip, "detected cc1=%s, cc2=%s",
1434                    typec_cc_status_name[cc1],
1435                    typec_cc_status_name[cc2]);
1436
1437        return ret;
1438}
1439
1440static int fusb302_handle_togdone(struct fusb302_chip *chip)
1441{
1442        int ret = 0;
1443        u8 status1a;
1444        u8 togdone_result;
1445
1446        ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1447        if (ret < 0)
1448                return ret;
1449        togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1450                         FUSB_REG_STATUS1A_TOGSS_MASK;
1451        switch (togdone_result) {
1452        case FUSB_REG_STATUS1A_TOGSS_SNK1:
1453        case FUSB_REG_STATUS1A_TOGSS_SNK2:
1454                return fusb302_handle_togdone_snk(chip, togdone_result);
1455        case FUSB_REG_STATUS1A_TOGSS_SRC1:
1456        case FUSB_REG_STATUS1A_TOGSS_SRC2:
1457                return fusb302_handle_togdone_src(chip, togdone_result);
1458        case FUSB_REG_STATUS1A_TOGSS_AA:
1459                /* doesn't support */
1460                fusb302_log(chip, "AudioAccessory not supported");
1461                fusb302_set_toggling(chip, chip->toggling_mode);
1462                break;
1463        default:
1464                fusb302_log(chip, "TOGDONE with an invalid state: %d",
1465                            togdone_result);
1466                fusb302_set_toggling(chip, chip->toggling_mode);
1467                break;
1468        }
1469        return ret;
1470}
1471
1472static int fusb302_pd_reset(struct fusb302_chip *chip)
1473{
1474        return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1475                                    FUSB_REG_RESET_PD_RESET);
1476}
1477
1478static int fusb302_pd_read_message(struct fusb302_chip *chip,
1479                                   struct pd_message *msg)
1480{
1481        int ret = 0;
1482        u8 token;
1483        u8 crc[4];
1484        int len;
1485
1486        /* first SOP token */
1487        ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1488        if (ret < 0)
1489                return ret;
1490        ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1491                                     (u8 *)&msg->header);
1492        if (ret < 0)
1493                return ret;
1494        len = pd_header_cnt_le(msg->header) * 4;
1495        /* add 4 to length to include the CRC */
1496        if (len > PD_MAX_PAYLOAD * 4) {
1497                fusb302_log(chip, "PD message too long %d", len);
1498                return -EINVAL;
1499        }
1500        if (len > 0) {
1501                ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1502                                             (u8 *)msg->payload);
1503                if (ret < 0)
1504                        return ret;
1505        }
1506        /* another 4 bytes to read CRC out */
1507        ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1508        if (ret < 0)
1509                return ret;
1510        fusb302_log(chip, "PD message header: %x", msg->header);
1511        fusb302_log(chip, "PD message len: %d", len);
1512
1513        /*
1514         * Check if we've read off a GoodCRC message. If so then indicate to
1515         * TCPM that the previous transmission has completed. Otherwise we pass
1516         * the received message over to TCPM for processing.
1517         *
1518         * We make this check here instead of basing the reporting decision on
1519         * the IRQ event type, as it's possible for the chip to report the
1520         * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
1521         * to check the message type to ensure correct reporting to TCPM.
1522         */
1523        if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
1524                tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1525        else
1526                tcpm_pd_receive(chip->tcpm_port, msg);
1527
1528        return ret;
1529}
1530
1531static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1532{
1533        struct fusb302_chip *chip = dev_id;
1534        int ret = 0;
1535        u8 interrupt;
1536        u8 interrupta;
1537        u8 interruptb;
1538        u8 status0;
1539        bool vbus_present;
1540        bool comp_result;
1541        bool intr_togdone;
1542        bool intr_bc_lvl;
1543        bool intr_comp_chng;
1544        struct pd_message pd_msg;
1545
1546        mutex_lock(&chip->lock);
1547        /* grab a snapshot of intr flags */
1548        intr_togdone = chip->intr_togdone;
1549        intr_bc_lvl = chip->intr_bc_lvl;
1550        intr_comp_chng = chip->intr_comp_chng;
1551
1552        ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1553        if (ret < 0)
1554                goto done;
1555        ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1556        if (ret < 0)
1557                goto done;
1558        ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1559        if (ret < 0)
1560                goto done;
1561        ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1562        if (ret < 0)
1563                goto done;
1564        fusb302_log(chip,
1565                    "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1566                    interrupt, interrupta, interruptb, status0);
1567
1568        if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1569                vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1570                fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1571                            vbus_present ? "On" : "Off");
1572                if (vbus_present != chip->vbus_present) {
1573                        chip->vbus_present = vbus_present;
1574                        tcpm_vbus_change(chip->tcpm_port);
1575                }
1576        }
1577
1578        if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1579                fusb302_log(chip, "IRQ: TOGDONE");
1580                ret = fusb302_handle_togdone(chip);
1581                if (ret < 0) {
1582                        fusb302_log(chip,
1583                                    "handle togdone error, ret=%d", ret);
1584                        goto done;
1585                }
1586        }
1587
1588        if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1589                fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1590                /*
1591                 * as BC_LVL interrupt can be affected by PD activity,
1592                 * apply delay to for the handler to wait for the PD
1593                 * signaling to finish.
1594                 */
1595                mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1596                                 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1597        }
1598
1599        if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1600                comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1601                fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1602                            comp_result ? "true" : "false");
1603                if (comp_result) {
1604                        /* cc level > Rd_threashold, detach */
1605                        if (chip->cc_polarity == TYPEC_POLARITY_CC1)
1606                                chip->cc1 = TYPEC_CC_OPEN;
1607                        else
1608                                chip->cc2 = TYPEC_CC_OPEN;
1609                        tcpm_cc_change(chip->tcpm_port);
1610                }
1611        }
1612
1613        if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1614                fusb302_log(chip, "IRQ: PD collision");
1615                tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1616        }
1617
1618        if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1619                fusb302_log(chip, "IRQ: PD retry failed");
1620                tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1621        }
1622
1623        if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1624                fusb302_log(chip, "IRQ: PD hardreset sent");
1625                ret = fusb302_pd_reset(chip);
1626                if (ret < 0) {
1627                        fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1628                        goto done;
1629                }
1630                tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1631        }
1632
1633        if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1634                fusb302_log(chip, "IRQ: PD tx success");
1635                ret = fusb302_pd_read_message(chip, &pd_msg);
1636                if (ret < 0) {
1637                        fusb302_log(chip,
1638                                    "cannot read in PD message, ret=%d", ret);
1639                        goto done;
1640                }
1641        }
1642
1643        if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1644                fusb302_log(chip, "IRQ: PD received hardreset");
1645                ret = fusb302_pd_reset(chip);
1646                if (ret < 0) {
1647                        fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1648                        goto done;
1649                }
1650                tcpm_pd_hard_reset(chip->tcpm_port);
1651        }
1652
1653        if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1654                fusb302_log(chip, "IRQ: PD sent good CRC");
1655                ret = fusb302_pd_read_message(chip, &pd_msg);
1656                if (ret < 0) {
1657                        fusb302_log(chip,
1658                                    "cannot read in PD message, ret=%d", ret);
1659                        goto done;
1660                }
1661        }
1662done:
1663        mutex_unlock(&chip->lock);
1664
1665        return IRQ_HANDLED;
1666}
1667
1668static int init_gpio(struct fusb302_chip *chip)
1669{
1670        struct device_node *node;
1671        int ret = 0;
1672
1673        node = chip->dev->of_node;
1674        chip->gpio_int_n = of_get_named_gpio(node, "fcs,int_n", 0);
1675        if (!gpio_is_valid(chip->gpio_int_n)) {
1676                ret = chip->gpio_int_n;
1677                dev_err(chip->dev, "cannot get named GPIO Int_N, ret=%d", ret);
1678                return ret;
1679        }
1680        ret = devm_gpio_request(chip->dev, chip->gpio_int_n, "fcs,int_n");
1681        if (ret < 0) {
1682                dev_err(chip->dev, "cannot request GPIO Int_N, ret=%d", ret);
1683                return ret;
1684        }
1685        ret = gpio_direction_input(chip->gpio_int_n);
1686        if (ret < 0) {
1687                dev_err(chip->dev,
1688                        "cannot set GPIO Int_N to input, ret=%d", ret);
1689                return ret;
1690        }
1691        ret = gpio_to_irq(chip->gpio_int_n);
1692        if (ret < 0) {
1693                dev_err(chip->dev,
1694                        "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1695                return ret;
1696        }
1697        chip->gpio_int_n_irq = ret;
1698        return 0;
1699}
1700
1701static int fusb302_composite_snk_pdo_array(struct fusb302_chip *chip)
1702{
1703        struct device *dev = chip->dev;
1704        u32 max_uv, max_ua;
1705
1706        chip->snk_pdo[0] = PDO_FIXED(5000, 400, PDO_FIXED_FLAGS);
1707
1708        /*
1709         * As max_snk_ma/mv/mw is not needed for tcpc_config,
1710         * those settings should be passed in via sink PDO, so
1711         * "fcs, max-sink-*" properties will be deprecated, to
1712         * perserve compatibility with existing users of them,
1713         * we read those properties to convert them to be a var
1714         * PDO.
1715         */
1716        if (device_property_read_u32(dev, "fcs,max-sink-microvolt", &max_uv) ||
1717                device_property_read_u32(dev, "fcs,max-sink-microamp", &max_ua))
1718                return 1;
1719
1720        chip->snk_pdo[1] = PDO_VAR(5000, max_uv / 1000, max_ua / 1000);
1721        return 2;
1722}
1723
1724static int fusb302_probe(struct i2c_client *client,
1725                         const struct i2c_device_id *id)
1726{
1727        struct fusb302_chip *chip;
1728        struct i2c_adapter *adapter;
1729        struct device *dev = &client->dev;
1730        const char *name;
1731        int ret = 0;
1732        u32 v;
1733
1734        adapter = to_i2c_adapter(client->dev.parent);
1735        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1736                dev_err(&client->dev,
1737                        "I2C/SMBus block functionality not supported!\n");
1738                return -ENODEV;
1739        }
1740        chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1741        if (!chip)
1742                return -ENOMEM;
1743
1744        chip->i2c_client = client;
1745        i2c_set_clientdata(client, chip);
1746        chip->dev = &client->dev;
1747        chip->tcpc_config = fusb302_tcpc_config;
1748        chip->tcpc_dev.config = &chip->tcpc_config;
1749        mutex_init(&chip->lock);
1750
1751        if (!device_property_read_u32(dev, "fcs,operating-sink-microwatt", &v))
1752                chip->tcpc_config.operating_snk_mw = v / 1000;
1753
1754        /* Composite sink PDO */
1755        chip->tcpc_config.nr_snk_pdo = fusb302_composite_snk_pdo_array(chip);
1756        chip->tcpc_config.snk_pdo = chip->snk_pdo;
1757
1758        /*
1759         * Devicetree platforms should get extcon via phandle (not yet
1760         * supported). On ACPI platforms, we get the name from a device prop.
1761         * This device prop is for kernel internal use only and is expected
1762         * to be set by the platform code which also registers the i2c client
1763         * for the fusb302.
1764         */
1765        if (device_property_read_string(dev, "fcs,extcon-name", &name) == 0) {
1766                chip->extcon = extcon_get_extcon_dev(name);
1767                if (!chip->extcon)
1768                        return -EPROBE_DEFER;
1769        }
1770
1771        fusb302_debugfs_init(chip);
1772
1773        chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1774        if (!chip->wq) {
1775                ret = -ENOMEM;
1776                goto clear_client_data;
1777        }
1778        INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1779        init_tcpc_dev(&chip->tcpc_dev);
1780
1781        chip->vbus = devm_regulator_get(chip->dev, "vbus");
1782        if (IS_ERR(chip->vbus)) {
1783                ret = PTR_ERR(chip->vbus);
1784                goto destroy_workqueue;
1785        }
1786
1787        if (client->irq) {
1788                chip->gpio_int_n_irq = client->irq;
1789        } else {
1790                ret = init_gpio(chip);
1791                if (ret < 0)
1792                        goto destroy_workqueue;
1793        }
1794
1795        chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1796        if (IS_ERR(chip->tcpm_port)) {
1797                ret = PTR_ERR(chip->tcpm_port);
1798                if (ret != -EPROBE_DEFER)
1799                        dev_err(dev, "cannot register tcpm port, ret=%d", ret);
1800                goto destroy_workqueue;
1801        }
1802
1803        ret = devm_request_threaded_irq(chip->dev, chip->gpio_int_n_irq,
1804                                        NULL, fusb302_irq_intn,
1805                                        IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1806                                        "fsc_interrupt_int_n", chip);
1807        if (ret < 0) {
1808                dev_err(dev, "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1809                goto tcpm_unregister_port;
1810        }
1811        enable_irq_wake(chip->gpio_int_n_irq);
1812        return ret;
1813
1814tcpm_unregister_port:
1815        tcpm_unregister_port(chip->tcpm_port);
1816destroy_workqueue:
1817        destroy_workqueue(chip->wq);
1818clear_client_data:
1819        i2c_set_clientdata(client, NULL);
1820        fusb302_debugfs_exit(chip);
1821
1822        return ret;
1823}
1824
1825static int fusb302_remove(struct i2c_client *client)
1826{
1827        struct fusb302_chip *chip = i2c_get_clientdata(client);
1828
1829        tcpm_unregister_port(chip->tcpm_port);
1830        destroy_workqueue(chip->wq);
1831        i2c_set_clientdata(client, NULL);
1832        fusb302_debugfs_exit(chip);
1833
1834        return 0;
1835}
1836
1837static int fusb302_pm_suspend(struct device *dev)
1838{
1839        struct fusb302_chip *chip = dev->driver_data;
1840
1841        if (atomic_read(&chip->i2c_busy))
1842                return -EBUSY;
1843        atomic_set(&chip->pm_suspend, 1);
1844
1845        return 0;
1846}
1847
1848static int fusb302_pm_resume(struct device *dev)
1849{
1850        struct fusb302_chip *chip = dev->driver_data;
1851
1852        atomic_set(&chip->pm_suspend, 0);
1853
1854        return 0;
1855}
1856
1857static const struct of_device_id fusb302_dt_match[] = {
1858        {.compatible = "fcs,fusb302"},
1859        {},
1860};
1861MODULE_DEVICE_TABLE(of, fusb302_dt_match);
1862
1863static const struct i2c_device_id fusb302_i2c_device_id[] = {
1864        {"typec_fusb302", 0},
1865        {},
1866};
1867MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
1868
1869static const struct dev_pm_ops fusb302_pm_ops = {
1870        .suspend = fusb302_pm_suspend,
1871        .resume = fusb302_pm_resume,
1872};
1873
1874static struct i2c_driver fusb302_driver = {
1875        .driver = {
1876                   .name = "typec_fusb302",
1877                   .pm = &fusb302_pm_ops,
1878                   .of_match_table = of_match_ptr(fusb302_dt_match),
1879                   },
1880        .probe = fusb302_probe,
1881        .remove = fusb302_remove,
1882        .id_table = fusb302_i2c_device_id,
1883};
1884module_i2c_driver(fusb302_driver);
1885
1886MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1887MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1888MODULE_LICENSE("GPL");
1889