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