Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright 2016-2017 Google, Inc
0004  *
0005  * Fairchild FUSB302 Type-C Chip Driver
0006  */
0007 
0008 #include <linux/debugfs.h>
0009 #include <linux/delay.h>
0010 #include <linux/errno.h>
0011 #include <linux/extcon.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/i2c.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/mutex.h>
0018 #include <linux/of_device.h>
0019 #include <linux/pinctrl/consumer.h>
0020 #include <linux/proc_fs.h>
0021 #include <linux/regulator/consumer.h>
0022 #include <linux/sched/clock.h>
0023 #include <linux/seq_file.h>
0024 #include <linux/slab.h>
0025 #include <linux/spinlock.h>
0026 #include <linux/string.h>
0027 #include <linux/types.h>
0028 #include <linux/usb.h>
0029 #include <linux/usb/typec.h>
0030 #include <linux/usb/tcpm.h>
0031 #include <linux/usb/pd.h>
0032 #include <linux/workqueue.h>
0033 
0034 #include "fusb302_reg.h"
0035 
0036 /*
0037  * When the device is SNK, BC_LVL interrupt is used to monitor cc pins
0038  * for the current capability offered by the SRC. As FUSB302 chip fires
0039  * the BC_LVL interrupt on PD signalings, cc lvl should be handled after
0040  * a delay to avoid measuring on PD activities. The delay is slightly
0041  * longer than PD_T_PD_DEBPUNCE (10-20ms).
0042  */
0043 #define T_BC_LVL_DEBOUNCE_DELAY_MS 30
0044 
0045 enum toggling_mode {
0046     TOGGLING_MODE_OFF,
0047     TOGGLING_MODE_DRP,
0048     TOGGLING_MODE_SNK,
0049     TOGGLING_MODE_SRC,
0050 };
0051 
0052 enum src_current_status {
0053     SRC_CURRENT_DEFAULT,
0054     SRC_CURRENT_MEDIUM,
0055     SRC_CURRENT_HIGH,
0056 };
0057 
0058 static const u8 ra_mda_value[] = {
0059     [SRC_CURRENT_DEFAULT] = 4,  /* 210mV */
0060     [SRC_CURRENT_MEDIUM] = 9,   /* 420mV */
0061     [SRC_CURRENT_HIGH] = 18,    /* 798mV */
0062 };
0063 
0064 static const u8 rd_mda_value[] = {
0065     [SRC_CURRENT_DEFAULT] = 38, /* 1638mV */
0066     [SRC_CURRENT_MEDIUM] = 38,  /* 1638mV */
0067     [SRC_CURRENT_HIGH] = 61,    /* 2604mV */
0068 };
0069 
0070 #define LOG_BUFFER_ENTRIES  1024
0071 #define LOG_BUFFER_ENTRY_SIZE   128
0072 
0073 struct fusb302_chip {
0074     struct device *dev;
0075     struct i2c_client *i2c_client;
0076     struct tcpm_port *tcpm_port;
0077     struct tcpc_dev tcpc_dev;
0078 
0079     struct regulator *vbus;
0080 
0081     spinlock_t irq_lock;
0082     struct work_struct irq_work;
0083     bool irq_suspended;
0084     bool irq_while_suspended;
0085     struct gpio_desc *gpio_int_n;
0086     int gpio_int_n_irq;
0087     struct extcon_dev *extcon;
0088 
0089     struct workqueue_struct *wq;
0090     struct delayed_work bc_lvl_handler;
0091 
0092     /* lock for sharing chip states */
0093     struct mutex lock;
0094 
0095     /* chip status */
0096     enum toggling_mode toggling_mode;
0097     enum src_current_status src_current_status;
0098     bool intr_togdone;
0099     bool intr_bc_lvl;
0100     bool intr_comp_chng;
0101 
0102     /* port status */
0103     bool vconn_on;
0104     bool vbus_on;
0105     bool charge_on;
0106     bool vbus_present;
0107     enum typec_cc_polarity cc_polarity;
0108     enum typec_cc_status cc1;
0109     enum typec_cc_status cc2;
0110     u32 snk_pdo[PDO_MAX_OBJECTS];
0111 
0112 #ifdef CONFIG_DEBUG_FS
0113     struct dentry *dentry;
0114     /* lock for log buffer access */
0115     struct mutex logbuffer_lock;
0116     int logbuffer_head;
0117     int logbuffer_tail;
0118     u8 *logbuffer[LOG_BUFFER_ENTRIES];
0119 #endif
0120 };
0121 
0122 /*
0123  * Logging
0124  */
0125 
0126 #ifdef CONFIG_DEBUG_FS
0127 static bool fusb302_log_full(struct fusb302_chip *chip)
0128 {
0129     return chip->logbuffer_tail ==
0130         (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
0131 }
0132 
0133 __printf(2, 0)
0134 static void _fusb302_log(struct fusb302_chip *chip, const char *fmt,
0135              va_list args)
0136 {
0137     char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
0138     u64 ts_nsec = local_clock();
0139     unsigned long rem_nsec;
0140 
0141     if (!chip->logbuffer[chip->logbuffer_head]) {
0142         chip->logbuffer[chip->logbuffer_head] =
0143                 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
0144         if (!chip->logbuffer[chip->logbuffer_head])
0145             return;
0146     }
0147 
0148     vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
0149 
0150     mutex_lock(&chip->logbuffer_lock);
0151 
0152     if (fusb302_log_full(chip)) {
0153         chip->logbuffer_head = max(chip->logbuffer_head - 1, 0);
0154         strlcpy(tmpbuffer, "overflow", sizeof(tmpbuffer));
0155     }
0156 
0157     if (chip->logbuffer_head < 0 ||
0158         chip->logbuffer_head >= LOG_BUFFER_ENTRIES) {
0159         dev_warn(chip->dev,
0160              "Bad log buffer index %d\n", chip->logbuffer_head);
0161         goto abort;
0162     }
0163 
0164     if (!chip->logbuffer[chip->logbuffer_head]) {
0165         dev_warn(chip->dev,
0166              "Log buffer index %d is NULL\n", chip->logbuffer_head);
0167         goto abort;
0168     }
0169 
0170     rem_nsec = do_div(ts_nsec, 1000000000);
0171     scnprintf(chip->logbuffer[chip->logbuffer_head],
0172           LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
0173           (unsigned long)ts_nsec, rem_nsec / 1000,
0174           tmpbuffer);
0175     chip->logbuffer_head = (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
0176 
0177 abort:
0178     mutex_unlock(&chip->logbuffer_lock);
0179 }
0180 
0181 __printf(2, 3)
0182 static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
0183 {
0184     va_list args;
0185 
0186     va_start(args, fmt);
0187     _fusb302_log(chip, fmt, args);
0188     va_end(args);
0189 }
0190 
0191 static int fusb302_debug_show(struct seq_file *s, void *v)
0192 {
0193     struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
0194     int tail;
0195 
0196     mutex_lock(&chip->logbuffer_lock);
0197     tail = chip->logbuffer_tail;
0198     while (tail != chip->logbuffer_head) {
0199         seq_printf(s, "%s\n", chip->logbuffer[tail]);
0200         tail = (tail + 1) % LOG_BUFFER_ENTRIES;
0201     }
0202     if (!seq_has_overflowed(s))
0203         chip->logbuffer_tail = tail;
0204     mutex_unlock(&chip->logbuffer_lock);
0205 
0206     return 0;
0207 }
0208 DEFINE_SHOW_ATTRIBUTE(fusb302_debug);
0209 
0210 static void fusb302_debugfs_init(struct fusb302_chip *chip)
0211 {
0212     char name[NAME_MAX];
0213 
0214     mutex_init(&chip->logbuffer_lock);
0215     snprintf(name, NAME_MAX, "fusb302-%s", dev_name(chip->dev));
0216     chip->dentry = debugfs_create_dir(name, usb_debug_root);
0217     debugfs_create_file("log", S_IFREG | 0444, chip->dentry, chip,
0218                 &fusb302_debug_fops);
0219 }
0220 
0221 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
0222 {
0223     debugfs_remove(chip->dentry);
0224 }
0225 
0226 #else
0227 
0228 static void fusb302_log(const struct fusb302_chip *chip,
0229             const char *fmt, ...) { }
0230 static void fusb302_debugfs_init(const struct fusb302_chip *chip) { }
0231 static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
0232 
0233 #endif
0234 
0235 static int fusb302_i2c_write(struct fusb302_chip *chip,
0236                  u8 address, u8 data)
0237 {
0238     int ret = 0;
0239 
0240     ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
0241     if (ret < 0)
0242         fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
0243                 data, address, ret);
0244 
0245     return ret;
0246 }
0247 
0248 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
0249                    u8 length, const u8 *data)
0250 {
0251     int ret = 0;
0252 
0253     if (length <= 0)
0254         return ret;
0255 
0256     ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
0257                          length, data);
0258     if (ret < 0)
0259         fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
0260                 address, length, ret);
0261 
0262     return ret;
0263 }
0264 
0265 static int fusb302_i2c_read(struct fusb302_chip *chip,
0266                 u8 address, u8 *data)
0267 {
0268     int ret = 0;
0269 
0270     ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
0271     *data = (u8)ret;
0272     if (ret < 0)
0273         fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
0274 
0275     return ret;
0276 }
0277 
0278 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
0279                   u8 length, u8 *data)
0280 {
0281     int ret = 0;
0282 
0283     if (length <= 0)
0284         return ret;
0285 
0286     ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
0287                         length, data);
0288     if (ret < 0) {
0289         fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
0290                 address, length, ret);
0291         goto done;
0292     }
0293     if (ret != length) {
0294         fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
0295                 ret, length, address);
0296         ret = -EIO;
0297     }
0298 
0299 done:
0300     return ret;
0301 }
0302 
0303 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
0304                   u8 mask, u8 value)
0305 {
0306     int ret = 0;
0307     u8 data;
0308 
0309     ret = fusb302_i2c_read(chip, address, &data);
0310     if (ret < 0)
0311         return ret;
0312     data &= ~mask;
0313     data |= value;
0314     ret = fusb302_i2c_write(chip, address, data);
0315     if (ret < 0)
0316         return ret;
0317 
0318     return ret;
0319 }
0320 
0321 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
0322                 u8 set_bits)
0323 {
0324     return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
0325 }
0326 
0327 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
0328                   u8 clear_bits)
0329 {
0330     return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
0331 }
0332 
0333 static int fusb302_sw_reset(struct fusb302_chip *chip)
0334 {
0335     int ret = 0;
0336 
0337     ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
0338                 FUSB_REG_RESET_SW_RESET);
0339     if (ret < 0)
0340         fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
0341     else
0342         fusb302_log(chip, "sw reset");
0343 
0344     return ret;
0345 }
0346 
0347 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip, u8 retry_count)
0348 {
0349     int ret = 0;
0350 
0351     ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3, retry_count |
0352                    FUSB_REG_CONTROL3_AUTO_RETRY);
0353 
0354     return ret;
0355 }
0356 
0357 /*
0358  * initialize interrupt on the chip
0359  * - unmasked interrupt: VBUS_OK
0360  */
0361 static int fusb302_init_interrupt(struct fusb302_chip *chip)
0362 {
0363     int ret = 0;
0364 
0365     ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
0366                 0xFF & ~FUSB_REG_MASK_VBUSOK);
0367     if (ret < 0)
0368         return ret;
0369     ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
0370     if (ret < 0)
0371         return ret;
0372     ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
0373     if (ret < 0)
0374         return ret;
0375     ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
0376                      FUSB_REG_CONTROL0_INT_MASK);
0377     if (ret < 0)
0378         return ret;
0379 
0380     return ret;
0381 }
0382 
0383 static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
0384 {
0385     int ret = 0;
0386 
0387     ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
0388 
0389     return ret;
0390 }
0391 
0392 static int tcpm_init(struct tcpc_dev *dev)
0393 {
0394     struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
0395                          tcpc_dev);
0396     int ret = 0;
0397     u8 data;
0398 
0399     ret = fusb302_sw_reset(chip);
0400     if (ret < 0)
0401         return ret;
0402     ret = fusb302_enable_tx_auto_retries(chip, FUSB_REG_CONTROL3_N_RETRIES_3);
0403     if (ret < 0)
0404         return ret;
0405     ret = fusb302_init_interrupt(chip);
0406     if (ret < 0)
0407         return ret;
0408     ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
0409     if (ret < 0)
0410         return ret;
0411     ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
0412     if (ret < 0)
0413         return ret;
0414     chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
0415     ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
0416     if (ret < 0)
0417         return ret;
0418     fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
0419 
0420     return ret;
0421 }
0422 
0423 static int tcpm_get_vbus(struct tcpc_dev *dev)
0424 {
0425     struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
0426                          tcpc_dev);
0427     int ret = 0;
0428 
0429     mutex_lock(&chip->lock);
0430     ret = chip->vbus_present ? 1 : 0;
0431     mutex_unlock(&chip->lock);
0432 
0433     return ret;
0434 }
0435 
0436 static int tcpm_get_current_limit(struct tcpc_dev *dev)
0437 {
0438     struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
0439                          tcpc_dev);
0440     int current_limit = 0;
0441     unsigned long timeout;
0442 
0443     if (!chip->extcon)
0444         return 0;
0445 
0446     /*
0447      * USB2 Charger detection may still be in progress when we get here,
0448      * this can take upto 600ms, wait 800ms max.
0449      */
0450     timeout = jiffies + msecs_to_jiffies(800);
0451     do {
0452         if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_SDP) == 1)
0453             current_limit = 500;
0454 
0455         if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_CDP) == 1 ||
0456             extcon_get_state(chip->extcon, EXTCON_CHG_USB_ACA) == 1)
0457             current_limit = 1500;
0458 
0459         if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_DCP) == 1)
0460             current_limit = 2000;
0461 
0462         msleep(50);
0463     } while (current_limit == 0 && time_before(jiffies, timeout));
0464 
0465     return current_limit;
0466 }
0467 
0468 static int fusb302_set_src_current(struct fusb302_chip *chip,
0469                    enum src_current_status status)
0470 {
0471     int ret = 0;
0472 
0473     chip->src_current_status = status;
0474     switch (status) {
0475     case SRC_CURRENT_DEFAULT:
0476         ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
0477                          FUSB_REG_CONTROL0_HOST_CUR_MASK,
0478                          FUSB_REG_CONTROL0_HOST_CUR_DEF);
0479         break;
0480     case SRC_CURRENT_MEDIUM:
0481         ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
0482                          FUSB_REG_CONTROL0_HOST_CUR_MASK,
0483                          FUSB_REG_CONTROL0_HOST_CUR_MED);
0484         break;
0485     case SRC_CURRENT_HIGH:
0486         ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
0487                          FUSB_REG_CONTROL0_HOST_CUR_MASK,
0488                          FUSB_REG_CONTROL0_HOST_CUR_HIGH);
0489         break;
0490     default:
0491         break;
0492     }
0493 
0494     return ret;
0495 }
0496 
0497 static int fusb302_set_toggling(struct fusb302_chip *chip,
0498                 enum toggling_mode mode)
0499 {
0500     int ret = 0;
0501 
0502     /* first disable toggling */
0503     ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
0504                      FUSB_REG_CONTROL2_TOGGLE);
0505     if (ret < 0)
0506         return ret;
0507     /* mask interrupts for SRC or SNK */
0508     ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
0509                    FUSB_REG_MASK_BC_LVL |
0510                    FUSB_REG_MASK_COMP_CHNG);
0511     if (ret < 0)
0512         return ret;
0513     chip->intr_bc_lvl = false;
0514     chip->intr_comp_chng = false;
0515     /* configure toggling mode: none/snk/src/drp */
0516     switch (mode) {
0517     case TOGGLING_MODE_OFF:
0518         ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
0519                          FUSB_REG_CONTROL2_MODE_MASK,
0520                          FUSB_REG_CONTROL2_MODE_NONE);
0521         if (ret < 0)
0522             return ret;
0523         break;
0524     case TOGGLING_MODE_SNK:
0525         ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
0526                          FUSB_REG_CONTROL2_MODE_MASK,
0527                          FUSB_REG_CONTROL2_MODE_UFP);
0528         if (ret < 0)
0529             return ret;
0530         break;
0531     case TOGGLING_MODE_SRC:
0532         ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
0533                          FUSB_REG_CONTROL2_MODE_MASK,
0534                          FUSB_REG_CONTROL2_MODE_DFP);
0535         if (ret < 0)
0536             return ret;
0537         break;
0538     case TOGGLING_MODE_DRP:
0539         ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
0540                          FUSB_REG_CONTROL2_MODE_MASK,
0541                          FUSB_REG_CONTROL2_MODE_DRP);
0542         if (ret < 0)
0543             return ret;
0544         break;
0545     default:
0546         break;
0547     }
0548 
0549     if (mode == TOGGLING_MODE_OFF) {
0550         /* mask TOGDONE interrupt */
0551         ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
0552                        FUSB_REG_MASKA_TOGDONE);
0553         if (ret < 0)
0554             return ret;
0555         chip->intr_togdone = false;
0556     } else {
0557         /* Datasheet says vconn MUST be off when toggling */
0558         WARN(chip->vconn_on, "Vconn is on during toggle start");
0559         /* unmask TOGDONE interrupt */
0560         ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
0561                          FUSB_REG_MASKA_TOGDONE);
0562         if (ret < 0)
0563             return ret;
0564         chip->intr_togdone = true;
0565         /* start toggling */
0566         ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
0567                        FUSB_REG_CONTROL2_TOGGLE);
0568         if (ret < 0)
0569             return ret;
0570         /* during toggling, consider cc as Open */
0571         chip->cc1 = TYPEC_CC_OPEN;
0572         chip->cc2 = TYPEC_CC_OPEN;
0573     }
0574     chip->toggling_mode = mode;
0575 
0576     return ret;
0577 }
0578 
0579 static const char * const typec_cc_status_name[] = {
0580     [TYPEC_CC_OPEN]     = "Open",
0581     [TYPEC_CC_RA]       = "Ra",
0582     [TYPEC_CC_RD]       = "Rd",
0583     [TYPEC_CC_RP_DEF]   = "Rp-def",
0584     [TYPEC_CC_RP_1_5]   = "Rp-1.5",
0585     [TYPEC_CC_RP_3_0]   = "Rp-3.0",
0586 };
0587 
0588 static const enum src_current_status cc_src_current[] = {
0589     [TYPEC_CC_OPEN]     = SRC_CURRENT_DEFAULT,
0590     [TYPEC_CC_RA]       = SRC_CURRENT_DEFAULT,
0591     [TYPEC_CC_RD]       = SRC_CURRENT_DEFAULT,
0592     [TYPEC_CC_RP_DEF]   = SRC_CURRENT_DEFAULT,
0593     [TYPEC_CC_RP_1_5]   = SRC_CURRENT_MEDIUM,
0594     [TYPEC_CC_RP_3_0]   = SRC_CURRENT_HIGH,
0595 };
0596 
0597 static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
0598 {
0599     struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
0600                          tcpc_dev);
0601     u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
0602                 FUSB_REG_SWITCHES0_CC2_PU_EN |
0603                 FUSB_REG_SWITCHES0_CC1_PD_EN |
0604                 FUSB_REG_SWITCHES0_CC2_PD_EN;
0605     u8 rd_mda, switches0_data = 0x00;
0606     int ret = 0;
0607 
0608     mutex_lock(&chip->lock);
0609     switch (cc) {
0610     case TYPEC_CC_OPEN:
0611         break;
0612     case TYPEC_CC_RD:
0613         switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
0614                   FUSB_REG_SWITCHES0_CC2_PD_EN;
0615         break;
0616     case TYPEC_CC_RP_DEF:
0617     case TYPEC_CC_RP_1_5:
0618     case TYPEC_CC_RP_3_0:
0619         switches0_data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
0620                   FUSB_REG_SWITCHES0_CC1_PU_EN :
0621                   FUSB_REG_SWITCHES0_CC2_PU_EN;
0622         break;
0623     default:
0624         fusb302_log(chip, "unsupported cc value %s",
0625                 typec_cc_status_name[cc]);
0626         ret = -EINVAL;
0627         goto done;
0628     }
0629 
0630     fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
0631 
0632     ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
0633     if (ret < 0) {
0634         fusb302_log(chip, "cannot set toggling mode, ret=%d", ret);
0635         goto done;
0636     }
0637 
0638     ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
0639                      switches0_mask, switches0_data);
0640     if (ret < 0) {
0641         fusb302_log(chip, "cannot set pull-up/-down, ret = %d", ret);
0642         goto done;
0643     }
0644     /* reset the cc status */
0645     chip->cc1 = TYPEC_CC_OPEN;
0646     chip->cc2 = TYPEC_CC_OPEN;
0647 
0648     /* adjust current for SRC */
0649     ret = fusb302_set_src_current(chip, cc_src_current[cc]);
0650     if (ret < 0) {
0651         fusb302_log(chip, "cannot set src current %s, ret=%d",
0652                 typec_cc_status_name[cc], ret);
0653         goto done;
0654     }
0655 
0656     /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
0657     switch (cc) {
0658     case TYPEC_CC_RP_DEF:
0659     case TYPEC_CC_RP_1_5:
0660     case TYPEC_CC_RP_3_0:
0661         rd_mda = rd_mda_value[cc_src_current[cc]];
0662         ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
0663         if (ret < 0) {
0664             fusb302_log(chip,
0665                     "cannot set SRC measure value, ret=%d",
0666                     ret);
0667             goto done;
0668         }
0669         ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
0670                          FUSB_REG_MASK_BC_LVL |
0671                          FUSB_REG_MASK_COMP_CHNG,
0672                          FUSB_REG_MASK_BC_LVL);
0673         if (ret < 0) {
0674             fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
0675                     ret);
0676             goto done;
0677         }
0678         chip->intr_comp_chng = true;
0679         chip->intr_bc_lvl = false;
0680         break;
0681     case TYPEC_CC_RD:
0682         ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
0683                          FUSB_REG_MASK_BC_LVL |
0684                          FUSB_REG_MASK_COMP_CHNG,
0685                          FUSB_REG_MASK_COMP_CHNG);
0686         if (ret < 0) {
0687             fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
0688                     ret);
0689             goto done;
0690         }
0691         chip->intr_bc_lvl = true;
0692         chip->intr_comp_chng = false;
0693         break;
0694     default:
0695         break;
0696     }
0697 done:
0698     mutex_unlock(&chip->lock);
0699 
0700     return ret;
0701 }
0702 
0703 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
0704                enum typec_cc_status *cc2)
0705 {
0706     struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
0707                          tcpc_dev);
0708 
0709     mutex_lock(&chip->lock);
0710     *cc1 = chip->cc1;
0711     *cc2 = chip->cc2;
0712     fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
0713             typec_cc_status_name[*cc2]);
0714     mutex_unlock(&chip->lock);
0715 
0716     return 0;
0717 }
0718 
0719 static int tcpm_set_polarity(struct tcpc_dev *dev,
0720                  enum typec_cc_polarity polarity)
0721 {
0722     return 0;
0723 }
0724 
0725 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
0726 {
0727     struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
0728                          tcpc_dev);
0729     int ret = 0;
0730     u8 switches0_data = 0x00;
0731     u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
0732                 FUSB_REG_SWITCHES0_VCONN_CC2;
0733 
0734     mutex_lock(&chip->lock);
0735     if (chip->vconn_on == on) {
0736         fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
0737         goto done;
0738     }
0739     if (on) {
0740         switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
0741                  FUSB_REG_SWITCHES0_VCONN_CC2 :
0742                  FUSB_REG_SWITCHES0_VCONN_CC1;
0743     }
0744     ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
0745                      switches0_mask, switches0_data);
0746     if (ret < 0)
0747         goto done;
0748     chip->vconn_on = on;
0749     fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
0750 done:
0751     mutex_unlock(&chip->lock);
0752 
0753     return ret;
0754 }
0755 
0756 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
0757 {
0758     struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
0759                          tcpc_dev);
0760     int ret = 0;
0761 
0762     mutex_lock(&chip->lock);
0763     if (chip->vbus_on == on) {
0764         fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
0765     } else {
0766         if (on)
0767             ret = regulator_enable(chip->vbus);
0768         else
0769             ret = regulator_disable(chip->vbus);
0770         if (ret < 0) {
0771             fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
0772                     on ? "enable" : "disable", ret);
0773             goto done;
0774         }
0775         chip->vbus_on = on;
0776         fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
0777     }
0778     if (chip->charge_on == charge)
0779         fusb302_log(chip, "charge is already %s",
0780                 charge ? "On" : "Off");
0781     else
0782         chip->charge_on = charge;
0783 
0784 done:
0785     mutex_unlock(&chip->lock);
0786 
0787     return ret;
0788 }
0789 
0790 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
0791 {
0792     return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
0793                     FUSB_REG_CONTROL0_TX_FLUSH);
0794 }
0795 
0796 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
0797 {
0798     return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
0799                     FUSB_REG_CONTROL1_RX_FLUSH);
0800 }
0801 
0802 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
0803 {
0804     if (on)
0805         return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
0806                         FUSB_REG_SWITCHES1_AUTO_GCRC);
0807     return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
0808                         FUSB_REG_SWITCHES1_AUTO_GCRC);
0809 }
0810 
0811 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
0812 {
0813     int ret = 0;
0814     u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
0815     u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
0816                   FUSB_REG_MASKA_HARDSENT |
0817                   FUSB_REG_MASKA_TX_SUCCESS |
0818                   FUSB_REG_MASKA_HARDRESET;
0819     u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
0820 
0821     ret = on ?
0822         fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
0823         fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
0824     if (ret < 0)
0825         return ret;
0826     ret = on ?
0827         fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
0828         fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
0829     if (ret < 0)
0830         return ret;
0831     ret = on ?
0832         fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
0833         fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
0834     return ret;
0835 }
0836 
0837 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
0838 {
0839     struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
0840                          tcpc_dev);
0841     int ret = 0;
0842 
0843     mutex_lock(&chip->lock);
0844     ret = fusb302_pd_rx_flush(chip);
0845     if (ret < 0) {
0846         fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
0847         goto done;
0848     }
0849     ret = fusb302_pd_tx_flush(chip);
0850     if (ret < 0) {
0851         fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
0852         goto done;
0853     }
0854     ret = fusb302_pd_set_auto_goodcrc(chip, on);
0855     if (ret < 0) {
0856         fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
0857                 on ? "on" : "off", ret);
0858         goto done;
0859     }
0860     ret = fusb302_pd_set_interrupts(chip, on);
0861     if (ret < 0) {
0862         fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
0863                 on ? "on" : "off", ret);
0864         goto done;
0865     }
0866     fusb302_log(chip, "pd := %s", on ? "on" : "off");
0867 done:
0868     mutex_unlock(&chip->lock);
0869 
0870     return ret;
0871 }
0872 
0873 static const char * const typec_role_name[] = {
0874     [TYPEC_SINK]        = "Sink",
0875     [TYPEC_SOURCE]      = "Source",
0876 };
0877 
0878 static const char * const typec_data_role_name[] = {
0879     [TYPEC_DEVICE]      = "Device",
0880     [TYPEC_HOST]        = "Host",
0881 };
0882 
0883 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
0884               enum typec_role pwr, enum typec_data_role data)
0885 {
0886     struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
0887                          tcpc_dev);
0888     int ret = 0;
0889     u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
0890                 FUSB_REG_SWITCHES1_DATAROLE;
0891     u8 switches1_data = 0x00;
0892 
0893     mutex_lock(&chip->lock);
0894     if (pwr == TYPEC_SOURCE)
0895         switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
0896     if (data == TYPEC_HOST)
0897         switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
0898     ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
0899                      switches1_mask, switches1_data);
0900     if (ret < 0) {
0901         fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
0902                 typec_role_name[pwr], typec_data_role_name[data],
0903                 ret);
0904         goto done;
0905     }
0906     fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
0907             typec_data_role_name[data]);
0908 done:
0909     mutex_unlock(&chip->lock);
0910 
0911     return ret;
0912 }
0913 
0914 static int tcpm_start_toggling(struct tcpc_dev *dev,
0915                    enum typec_port_type port_type,
0916                    enum typec_cc_status cc)
0917 {
0918     struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
0919                          tcpc_dev);
0920     enum toggling_mode mode = TOGGLING_MODE_OFF;
0921     int ret = 0;
0922 
0923     switch (port_type) {
0924     case TYPEC_PORT_SRC:
0925         mode = TOGGLING_MODE_SRC;
0926         break;
0927     case TYPEC_PORT_SNK:
0928         mode = TOGGLING_MODE_SNK;
0929         break;
0930     case TYPEC_PORT_DRP:
0931         mode = TOGGLING_MODE_DRP;
0932         break;
0933     }
0934 
0935     mutex_lock(&chip->lock);
0936     ret = fusb302_set_src_current(chip, cc_src_current[cc]);
0937     if (ret < 0) {
0938         fusb302_log(chip, "unable to set src current %s, ret=%d",
0939                 typec_cc_status_name[cc], ret);
0940         goto done;
0941     }
0942     ret = fusb302_set_toggling(chip, mode);
0943     if (ret < 0) {
0944         fusb302_log(chip,
0945                 "unable to start drp toggling, ret=%d", ret);
0946         goto done;
0947     }
0948     fusb302_log(chip, "start drp toggling");
0949 done:
0950     mutex_unlock(&chip->lock);
0951 
0952     return ret;
0953 }
0954 
0955 static int fusb302_pd_send_message(struct fusb302_chip *chip,
0956                    const struct pd_message *msg)
0957 {
0958     int ret = 0;
0959     u8 buf[40];
0960     u8 pos = 0;
0961     int len;
0962 
0963     /* SOP tokens */
0964     buf[pos++] = FUSB302_TKN_SYNC1;
0965     buf[pos++] = FUSB302_TKN_SYNC1;
0966     buf[pos++] = FUSB302_TKN_SYNC1;
0967     buf[pos++] = FUSB302_TKN_SYNC2;
0968 
0969     len = pd_header_cnt_le(msg->header) * 4;
0970     /* plug 2 for header */
0971     len += 2;
0972     if (len > 0x1F) {
0973         fusb302_log(chip,
0974                 "PD message too long %d (incl. header)", len);
0975         return -EINVAL;
0976     }
0977     /* packsym tells the FUSB302 chip that the next X bytes are payload */
0978     buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
0979     memcpy(&buf[pos], &msg->header, sizeof(msg->header));
0980     pos += sizeof(msg->header);
0981 
0982     len -= 2;
0983     memcpy(&buf[pos], msg->payload, len);
0984     pos += len;
0985 
0986     /* CRC */
0987     buf[pos++] = FUSB302_TKN_JAMCRC;
0988     /* EOP */
0989     buf[pos++] = FUSB302_TKN_EOP;
0990     /* turn tx off after sending message */
0991     buf[pos++] = FUSB302_TKN_TXOFF;
0992     /* start transmission */
0993     buf[pos++] = FUSB302_TKN_TXON;
0994 
0995     ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
0996     if (ret < 0)
0997         return ret;
0998     fusb302_log(chip, "sending PD message header: %x", msg->header);
0999     fusb302_log(chip, "sending PD message len: %d", len);
1000 
1001     return ret;
1002 }
1003 
1004 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1005 {
1006     return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1007                     FUSB_REG_CONTROL3_SEND_HARDRESET);
1008 }
1009 
1010 static const char * const transmit_type_name[] = {
1011     [TCPC_TX_SOP]           = "SOP",
1012     [TCPC_TX_SOP_PRIME]     = "SOP'",
1013     [TCPC_TX_SOP_PRIME_PRIME]   = "SOP''",
1014     [TCPC_TX_SOP_DEBUG_PRIME]   = "DEBUG'",
1015     [TCPC_TX_SOP_DEBUG_PRIME_PRIME] = "DEBUG''",
1016     [TCPC_TX_HARD_RESET]        = "HARD_RESET",
1017     [TCPC_TX_CABLE_RESET]       = "CABLE_RESET",
1018     [TCPC_TX_BIST_MODE_2]       = "BIST_MODE_2",
1019 };
1020 
1021 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1022                 const struct pd_message *msg, unsigned int negotiated_rev)
1023 {
1024     struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1025                          tcpc_dev);
1026     int ret = 0;
1027 
1028     mutex_lock(&chip->lock);
1029     switch (type) {
1030     case TCPC_TX_SOP:
1031         /* nRetryCount 3 in P2.0 spec, whereas 2 in PD3.0 spec */
1032         ret = fusb302_enable_tx_auto_retries(chip, negotiated_rev > PD_REV20 ?
1033                              FUSB_REG_CONTROL3_N_RETRIES_2 :
1034                              FUSB_REG_CONTROL3_N_RETRIES_3);
1035         if (ret < 0)
1036             fusb302_log(chip, "Cannot update retry count ret=%d", ret);
1037 
1038         ret = fusb302_pd_send_message(chip, msg);
1039         if (ret < 0)
1040             fusb302_log(chip,
1041                     "cannot send PD message, ret=%d", ret);
1042         break;
1043     case TCPC_TX_HARD_RESET:
1044         ret = fusb302_pd_send_hardreset(chip);
1045         if (ret < 0)
1046             fusb302_log(chip,
1047                     "cannot send hardreset, ret=%d", ret);
1048         break;
1049     default:
1050         fusb302_log(chip, "type %s not supported",
1051                 transmit_type_name[type]);
1052         ret = -EINVAL;
1053     }
1054     mutex_unlock(&chip->lock);
1055 
1056     return ret;
1057 }
1058 
1059 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1060 {
1061     if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1062         return TYPEC_CC_RP_3_0;
1063     if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1064         return TYPEC_CC_RP_1_5;
1065     if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1066         return TYPEC_CC_RP_DEF;
1067     return TYPEC_CC_OPEN;
1068 }
1069 
1070 static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1071 {
1072     struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1073                          bc_lvl_handler.work);
1074     int ret = 0;
1075     u8 status0;
1076     u8 bc_lvl;
1077     enum typec_cc_status cc_status;
1078 
1079     mutex_lock(&chip->lock);
1080     if (!chip->intr_bc_lvl) {
1081         fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1082         goto done;
1083     }
1084     ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1085     if (ret < 0)
1086         goto done;
1087     fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1088     if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1089         fusb302_log(chip, "CC activities detected, delay handling");
1090         mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1091                  msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1092         goto done;
1093     }
1094     bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1095     cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1096     if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1097         if (chip->cc1 != cc_status) {
1098             fusb302_log(chip, "cc1: %s -> %s",
1099                     typec_cc_status_name[chip->cc1],
1100                     typec_cc_status_name[cc_status]);
1101             chip->cc1 = cc_status;
1102             tcpm_cc_change(chip->tcpm_port);
1103         }
1104     } else {
1105         if (chip->cc2 != cc_status) {
1106             fusb302_log(chip, "cc2: %s -> %s",
1107                     typec_cc_status_name[chip->cc2],
1108                     typec_cc_status_name[cc_status]);
1109             chip->cc2 = cc_status;
1110             tcpm_cc_change(chip->tcpm_port);
1111         }
1112     }
1113 
1114 done:
1115     mutex_unlock(&chip->lock);
1116 }
1117 
1118 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1119 {
1120     fusb302_tcpc_dev->init = tcpm_init;
1121     fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1122     fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
1123     fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1124     fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1125     fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1126     fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1127     fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1128     fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1129     fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1130     fusb302_tcpc_dev->start_toggling = tcpm_start_toggling;
1131     fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1132 }
1133 
1134 static const char * const cc_polarity_name[] = {
1135     [TYPEC_POLARITY_CC1]    = "Polarity_CC1",
1136     [TYPEC_POLARITY_CC2]    = "Polarity_CC2",
1137 };
1138 
1139 static int fusb302_set_cc_polarity_and_pull(struct fusb302_chip *chip,
1140                         enum typec_cc_polarity cc_polarity,
1141                         bool pull_up, bool pull_down)
1142 {
1143     int ret = 0;
1144     u8 switches0_data = 0x00;
1145     u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
1146                 FUSB_REG_SWITCHES1_TXCC2_EN;
1147     u8 switches1_data = 0x00;
1148 
1149     if (pull_down)
1150         switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
1151                   FUSB_REG_SWITCHES0_CC2_PD_EN;
1152 
1153     if (cc_polarity == TYPEC_POLARITY_CC1) {
1154         switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC1;
1155         if (chip->vconn_on)
1156             switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1157         if (pull_up)
1158             switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1159         switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1160     } else {
1161         switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC2;
1162         if (chip->vconn_on)
1163             switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1164         if (pull_up)
1165             switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1166         switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1167     }
1168     ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1169     if (ret < 0)
1170         return ret;
1171     ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1172                      switches1_mask, switches1_data);
1173     if (ret < 0)
1174         return ret;
1175     chip->cc_polarity = cc_polarity;
1176 
1177     return ret;
1178 }
1179 
1180 static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1181                       u8 togdone_result)
1182 {
1183     int ret = 0;
1184     u8 status0;
1185     u8 bc_lvl;
1186     enum typec_cc_polarity cc_polarity;
1187     enum typec_cc_status cc_status_active, cc1, cc2;
1188 
1189     /* set polarity and pull_up, pull_down */
1190     cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1191               TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1192     ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, false, true);
1193     if (ret < 0) {
1194         fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1195                 cc_polarity_name[cc_polarity], ret);
1196         return ret;
1197     }
1198     /* fusb302_set_cc_polarity() has set the correct measure block */
1199     ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1200     if (ret < 0)
1201         return ret;
1202     bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1203     cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1204     /* restart toggling if the cc status on the active line is OPEN */
1205     if (cc_status_active == TYPEC_CC_OPEN) {
1206         fusb302_log(chip, "restart toggling as CC_OPEN detected");
1207         ret = fusb302_set_toggling(chip, chip->toggling_mode);
1208         return ret;
1209     }
1210     /* update tcpm with the new cc value */
1211     cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1212           cc_status_active : TYPEC_CC_OPEN;
1213     cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1214           cc_status_active : TYPEC_CC_OPEN;
1215     if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1216         chip->cc1 = cc1;
1217         chip->cc2 = cc2;
1218         tcpm_cc_change(chip->tcpm_port);
1219     }
1220     /* turn off toggling */
1221     ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1222     if (ret < 0) {
1223         fusb302_log(chip,
1224                 "cannot set toggling mode off, ret=%d", ret);
1225         return ret;
1226     }
1227     /* unmask bc_lvl interrupt */
1228     ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1229     if (ret < 0) {
1230         fusb302_log(chip,
1231                 "cannot unmask bc_lcl interrupt, ret=%d", ret);
1232         return ret;
1233     }
1234     chip->intr_bc_lvl = true;
1235     fusb302_log(chip, "detected cc1=%s, cc2=%s",
1236             typec_cc_status_name[cc1],
1237             typec_cc_status_name[cc2]);
1238 
1239     return ret;
1240 }
1241 
1242 /* On error returns < 0, otherwise a typec_cc_status value */
1243 static int fusb302_get_src_cc_status(struct fusb302_chip *chip,
1244                      enum typec_cc_polarity cc_polarity,
1245                      enum typec_cc_status *cc)
1246 {
1247     u8 ra_mda = ra_mda_value[chip->src_current_status];
1248     u8 rd_mda = rd_mda_value[chip->src_current_status];
1249     u8 switches0_data, status0;
1250     int ret;
1251 
1252     /* Step 1: Set switches so that we measure the right CC pin */
1253     switches0_data = (cc_polarity == TYPEC_POLARITY_CC1) ?
1254         FUSB_REG_SWITCHES0_CC1_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC1 :
1255         FUSB_REG_SWITCHES0_CC2_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC2;
1256     ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1257     if (ret < 0)
1258         return ret;
1259 
1260     fusb302_i2c_read(chip, FUSB_REG_SWITCHES0, &status0);
1261     fusb302_log(chip, "get_src_cc_status switches: 0x%0x", status0);
1262 
1263     /* Step 2: Set compararator volt to differentiate between Open and Rd */
1264     ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1265     if (ret < 0)
1266         return ret;
1267 
1268     usleep_range(50, 100);
1269     ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1270     if (ret < 0)
1271         return ret;
1272 
1273     fusb302_log(chip, "get_src_cc_status rd_mda status0: 0x%0x", status0);
1274     if (status0 & FUSB_REG_STATUS0_COMP) {
1275         *cc = TYPEC_CC_OPEN;
1276         return 0;
1277     }
1278 
1279     /* Step 3: Set compararator input to differentiate between Rd and Ra. */
1280     ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1281     if (ret < 0)
1282         return ret;
1283 
1284     usleep_range(50, 100);
1285     ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1286     if (ret < 0)
1287         return ret;
1288 
1289     fusb302_log(chip, "get_src_cc_status ra_mda status0: 0x%0x", status0);
1290     if (status0 & FUSB_REG_STATUS0_COMP)
1291         *cc = TYPEC_CC_RD;
1292     else
1293         *cc = TYPEC_CC_RA;
1294 
1295     return 0;
1296 }
1297 
1298 static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1299                       u8 togdone_result)
1300 {
1301     /*
1302      * - set polarity (measure cc, vconn, tx)
1303      * - set pull_up, pull_down
1304      * - set cc1, cc2, and update to tcpm_port
1305      * - set I_COMP interrupt on
1306      */
1307     int ret = 0;
1308     u8 rd_mda = rd_mda_value[chip->src_current_status];
1309     enum toggling_mode toggling_mode = chip->toggling_mode;
1310     enum typec_cc_polarity cc_polarity;
1311     enum typec_cc_status cc1, cc2;
1312 
1313     /*
1314      * The toggle-engine will stop in a src state if it sees either Ra or
1315      * Rd. Determine the status for both CC pins, starting with the one
1316      * where toggling stopped, as that is where the switches point now.
1317      */
1318     if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1319         ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1320     else
1321         ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1322     if (ret < 0)
1323         return ret;
1324     /* we must turn off toggling before we can measure the other pin */
1325     ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1326     if (ret < 0) {
1327         fusb302_log(chip, "cannot set toggling mode off, ret=%d", ret);
1328         return ret;
1329     }
1330     /* get the status of the other pin */
1331     if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1332         ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1333     else
1334         ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1335     if (ret < 0)
1336         return ret;
1337 
1338     /* determine polarity based on the status of both pins */
1339     if (cc1 == TYPEC_CC_RD &&
1340             (cc2 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_RA)) {
1341         cc_polarity = TYPEC_POLARITY_CC1;
1342     } else if (cc2 == TYPEC_CC_RD &&
1343             (cc1 == TYPEC_CC_OPEN || cc1 == TYPEC_CC_RA)) {
1344         cc_polarity = TYPEC_POLARITY_CC2;
1345     } else {
1346         fusb302_log(chip, "unexpected CC status cc1=%s, cc2=%s, restarting toggling",
1347                 typec_cc_status_name[cc1],
1348                 typec_cc_status_name[cc2]);
1349         return fusb302_set_toggling(chip, toggling_mode);
1350     }
1351     /* set polarity and pull_up, pull_down */
1352     ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, true, false);
1353     if (ret < 0) {
1354         fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1355                 cc_polarity_name[cc_polarity], ret);
1356         return ret;
1357     }
1358     /* update tcpm with the new cc value */
1359     if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1360         chip->cc1 = cc1;
1361         chip->cc2 = cc2;
1362         tcpm_cc_change(chip->tcpm_port);
1363     }
1364     /* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1365     ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1366     if (ret < 0)
1367         return ret;
1368     /* unmask comp_chng interrupt */
1369     ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1370                      FUSB_REG_MASK_COMP_CHNG);
1371     if (ret < 0) {
1372         fusb302_log(chip,
1373                 "cannot unmask comp_chng interrupt, ret=%d", ret);
1374         return ret;
1375     }
1376     chip->intr_comp_chng = true;
1377     fusb302_log(chip, "detected cc1=%s, cc2=%s",
1378             typec_cc_status_name[cc1],
1379             typec_cc_status_name[cc2]);
1380 
1381     return ret;
1382 }
1383 
1384 static int fusb302_handle_togdone(struct fusb302_chip *chip)
1385 {
1386     int ret = 0;
1387     u8 status1a;
1388     u8 togdone_result;
1389 
1390     ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1391     if (ret < 0)
1392         return ret;
1393     togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1394              FUSB_REG_STATUS1A_TOGSS_MASK;
1395     switch (togdone_result) {
1396     case FUSB_REG_STATUS1A_TOGSS_SNK1:
1397     case FUSB_REG_STATUS1A_TOGSS_SNK2:
1398         return fusb302_handle_togdone_snk(chip, togdone_result);
1399     case FUSB_REG_STATUS1A_TOGSS_SRC1:
1400     case FUSB_REG_STATUS1A_TOGSS_SRC2:
1401         return fusb302_handle_togdone_src(chip, togdone_result);
1402     case FUSB_REG_STATUS1A_TOGSS_AA:
1403         /* doesn't support */
1404         fusb302_log(chip, "AudioAccessory not supported");
1405         fusb302_set_toggling(chip, chip->toggling_mode);
1406         break;
1407     default:
1408         fusb302_log(chip, "TOGDONE with an invalid state: %d",
1409                 togdone_result);
1410         fusb302_set_toggling(chip, chip->toggling_mode);
1411         break;
1412     }
1413     return ret;
1414 }
1415 
1416 static int fusb302_pd_reset(struct fusb302_chip *chip)
1417 {
1418     return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1419                     FUSB_REG_RESET_PD_RESET);
1420 }
1421 
1422 static int fusb302_pd_read_message(struct fusb302_chip *chip,
1423                    struct pd_message *msg)
1424 {
1425     int ret = 0;
1426     u8 token;
1427     u8 crc[4];
1428     int len;
1429 
1430     /* first SOP token */
1431     ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1432     if (ret < 0)
1433         return ret;
1434     ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1435                      (u8 *)&msg->header);
1436     if (ret < 0)
1437         return ret;
1438     len = pd_header_cnt_le(msg->header) * 4;
1439     /* add 4 to length to include the CRC */
1440     if (len > PD_MAX_PAYLOAD * 4) {
1441         fusb302_log(chip, "PD message too long %d", len);
1442         return -EINVAL;
1443     }
1444     if (len > 0) {
1445         ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1446                          (u8 *)msg->payload);
1447         if (ret < 0)
1448             return ret;
1449     }
1450     /* another 4 bytes to read CRC out */
1451     ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1452     if (ret < 0)
1453         return ret;
1454     fusb302_log(chip, "PD message header: %x", msg->header);
1455     fusb302_log(chip, "PD message len: %d", len);
1456 
1457     /*
1458      * Check if we've read off a GoodCRC message. If so then indicate to
1459      * TCPM that the previous transmission has completed. Otherwise we pass
1460      * the received message over to TCPM for processing.
1461      *
1462      * We make this check here instead of basing the reporting decision on
1463      * the IRQ event type, as it's possible for the chip to report the
1464      * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
1465      * to check the message type to ensure correct reporting to TCPM.
1466      */
1467     if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
1468         tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1469     else
1470         tcpm_pd_receive(chip->tcpm_port, msg);
1471 
1472     return ret;
1473 }
1474 
1475 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1476 {
1477     struct fusb302_chip *chip = dev_id;
1478     unsigned long flags;
1479 
1480     /* Disable our level triggered IRQ until our irq_work has cleared it */
1481     disable_irq_nosync(chip->gpio_int_n_irq);
1482 
1483     spin_lock_irqsave(&chip->irq_lock, flags);
1484     if (chip->irq_suspended)
1485         chip->irq_while_suspended = true;
1486     else
1487         schedule_work(&chip->irq_work);
1488     spin_unlock_irqrestore(&chip->irq_lock, flags);
1489 
1490     return IRQ_HANDLED;
1491 }
1492 
1493 static void fusb302_irq_work(struct work_struct *work)
1494 {
1495     struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1496                          irq_work);
1497     int ret = 0;
1498     u8 interrupt;
1499     u8 interrupta;
1500     u8 interruptb;
1501     u8 status0;
1502     bool vbus_present;
1503     bool comp_result;
1504     bool intr_togdone;
1505     bool intr_bc_lvl;
1506     bool intr_comp_chng;
1507     struct pd_message pd_msg;
1508 
1509     mutex_lock(&chip->lock);
1510     /* grab a snapshot of intr flags */
1511     intr_togdone = chip->intr_togdone;
1512     intr_bc_lvl = chip->intr_bc_lvl;
1513     intr_comp_chng = chip->intr_comp_chng;
1514 
1515     ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1516     if (ret < 0)
1517         goto done;
1518     ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1519     if (ret < 0)
1520         goto done;
1521     ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1522     if (ret < 0)
1523         goto done;
1524     ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1525     if (ret < 0)
1526         goto done;
1527     fusb302_log(chip,
1528             "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1529             interrupt, interrupta, interruptb, status0);
1530 
1531     if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1532         vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1533         fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1534                 vbus_present ? "On" : "Off");
1535         if (vbus_present != chip->vbus_present) {
1536             chip->vbus_present = vbus_present;
1537             tcpm_vbus_change(chip->tcpm_port);
1538         }
1539     }
1540 
1541     if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1542         fusb302_log(chip, "IRQ: TOGDONE");
1543         ret = fusb302_handle_togdone(chip);
1544         if (ret < 0) {
1545             fusb302_log(chip,
1546                     "handle togdone error, ret=%d", ret);
1547             goto done;
1548         }
1549     }
1550 
1551     if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1552         fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1553         /*
1554          * as BC_LVL interrupt can be affected by PD activity,
1555          * apply delay to for the handler to wait for the PD
1556          * signaling to finish.
1557          */
1558         mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1559                  msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1560     }
1561 
1562     if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1563         comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1564         fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1565                 comp_result ? "true" : "false");
1566         if (comp_result) {
1567             /* cc level > Rd_threshold, detach */
1568             chip->cc1 = TYPEC_CC_OPEN;
1569             chip->cc2 = TYPEC_CC_OPEN;
1570             tcpm_cc_change(chip->tcpm_port);
1571         }
1572     }
1573 
1574     if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1575         fusb302_log(chip, "IRQ: PD collision");
1576         tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1577     }
1578 
1579     if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1580         fusb302_log(chip, "IRQ: PD retry failed");
1581         tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1582     }
1583 
1584     if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1585         fusb302_log(chip, "IRQ: PD hardreset sent");
1586         ret = fusb302_pd_reset(chip);
1587         if (ret < 0) {
1588             fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1589             goto done;
1590         }
1591         tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1592     }
1593 
1594     if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1595         fusb302_log(chip, "IRQ: PD tx success");
1596         ret = fusb302_pd_read_message(chip, &pd_msg);
1597         if (ret < 0) {
1598             fusb302_log(chip,
1599                     "cannot read in PD message, ret=%d", ret);
1600             goto done;
1601         }
1602     }
1603 
1604     if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1605         fusb302_log(chip, "IRQ: PD received hardreset");
1606         ret = fusb302_pd_reset(chip);
1607         if (ret < 0) {
1608             fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1609             goto done;
1610         }
1611         tcpm_pd_hard_reset(chip->tcpm_port);
1612     }
1613 
1614     if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1615         fusb302_log(chip, "IRQ: PD sent good CRC");
1616         ret = fusb302_pd_read_message(chip, &pd_msg);
1617         if (ret < 0) {
1618             fusb302_log(chip,
1619                     "cannot read in PD message, ret=%d", ret);
1620             goto done;
1621         }
1622     }
1623 done:
1624     mutex_unlock(&chip->lock);
1625     enable_irq(chip->gpio_int_n_irq);
1626 }
1627 
1628 static int init_gpio(struct fusb302_chip *chip)
1629 {
1630     struct device *dev = chip->dev;
1631     int ret = 0;
1632 
1633     chip->gpio_int_n = devm_gpiod_get(dev, "fcs,int_n", GPIOD_IN);
1634     if (IS_ERR(chip->gpio_int_n)) {
1635         dev_err(dev, "failed to request gpio_int_n\n");
1636         return PTR_ERR(chip->gpio_int_n);
1637     }
1638     ret = gpiod_to_irq(chip->gpio_int_n);
1639     if (ret < 0) {
1640         dev_err(dev,
1641             "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1642         return ret;
1643     }
1644     chip->gpio_int_n_irq = ret;
1645     return 0;
1646 }
1647 
1648 #define PDO_FIXED_FLAGS \
1649     (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1650 
1651 static const u32 src_pdo[] = {
1652     PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1653 };
1654 
1655 static const u32 snk_pdo[] = {
1656     PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1657 };
1658 
1659 static const struct property_entry port_props[] = {
1660     PROPERTY_ENTRY_STRING("data-role", "dual"),
1661     PROPERTY_ENTRY_STRING("power-role", "dual"),
1662     PROPERTY_ENTRY_STRING("try-power-role", "sink"),
1663     PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
1664     PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
1665     PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
1666     { }
1667 };
1668 
1669 static struct fwnode_handle *fusb302_fwnode_get(struct device *dev)
1670 {
1671     struct fwnode_handle *fwnode;
1672 
1673     fwnode = device_get_named_child_node(dev, "connector");
1674     if (!fwnode)
1675         fwnode = fwnode_create_software_node(port_props, NULL);
1676 
1677     return fwnode;
1678 }
1679 
1680 static int fusb302_probe(struct i2c_client *client,
1681              const struct i2c_device_id *id)
1682 {
1683     struct fusb302_chip *chip;
1684     struct i2c_adapter *adapter = client->adapter;
1685     struct device *dev = &client->dev;
1686     const char *name;
1687     int ret = 0;
1688 
1689     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1690         dev_err(&client->dev,
1691             "I2C/SMBus block functionality not supported!\n");
1692         return -ENODEV;
1693     }
1694     chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1695     if (!chip)
1696         return -ENOMEM;
1697 
1698     chip->i2c_client = client;
1699     chip->dev = &client->dev;
1700     mutex_init(&chip->lock);
1701 
1702     /*
1703      * Devicetree platforms should get extcon via phandle (not yet
1704      * supported). On ACPI platforms, we get the name from a device prop.
1705      * This device prop is for kernel internal use only and is expected
1706      * to be set by the platform code which also registers the i2c client
1707      * for the fusb302.
1708      */
1709     if (device_property_read_string(dev, "linux,extcon-name", &name) == 0) {
1710         chip->extcon = extcon_get_extcon_dev(name);
1711         if (IS_ERR(chip->extcon))
1712             return PTR_ERR(chip->extcon);
1713     }
1714 
1715     chip->vbus = devm_regulator_get(chip->dev, "vbus");
1716     if (IS_ERR(chip->vbus))
1717         return PTR_ERR(chip->vbus);
1718 
1719     chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1720     if (!chip->wq)
1721         return -ENOMEM;
1722 
1723     spin_lock_init(&chip->irq_lock);
1724     INIT_WORK(&chip->irq_work, fusb302_irq_work);
1725     INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1726     init_tcpc_dev(&chip->tcpc_dev);
1727     fusb302_debugfs_init(chip);
1728 
1729     if (client->irq) {
1730         chip->gpio_int_n_irq = client->irq;
1731     } else {
1732         ret = init_gpio(chip);
1733         if (ret < 0)
1734             goto destroy_workqueue;
1735     }
1736 
1737     chip->tcpc_dev.fwnode = fusb302_fwnode_get(dev);
1738     if (IS_ERR(chip->tcpc_dev.fwnode)) {
1739         ret = PTR_ERR(chip->tcpc_dev.fwnode);
1740         goto destroy_workqueue;
1741     }
1742 
1743     chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1744     if (IS_ERR(chip->tcpm_port)) {
1745         fwnode_handle_put(chip->tcpc_dev.fwnode);
1746         ret = PTR_ERR(chip->tcpm_port);
1747         if (ret != -EPROBE_DEFER)
1748             dev_err(dev, "cannot register tcpm port, ret=%d", ret);
1749         goto destroy_workqueue;
1750     }
1751 
1752     ret = request_irq(chip->gpio_int_n_irq, fusb302_irq_intn,
1753               IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1754               "fsc_interrupt_int_n", chip);
1755     if (ret < 0) {
1756         dev_err(dev, "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1757         goto tcpm_unregister_port;
1758     }
1759     enable_irq_wake(chip->gpio_int_n_irq);
1760     i2c_set_clientdata(client, chip);
1761 
1762     return ret;
1763 
1764 tcpm_unregister_port:
1765     tcpm_unregister_port(chip->tcpm_port);
1766     fwnode_handle_put(chip->tcpc_dev.fwnode);
1767 destroy_workqueue:
1768     fusb302_debugfs_exit(chip);
1769     destroy_workqueue(chip->wq);
1770 
1771     return ret;
1772 }
1773 
1774 static int fusb302_remove(struct i2c_client *client)
1775 {
1776     struct fusb302_chip *chip = i2c_get_clientdata(client);
1777 
1778     disable_irq_wake(chip->gpio_int_n_irq);
1779     free_irq(chip->gpio_int_n_irq, chip);
1780     cancel_work_sync(&chip->irq_work);
1781     cancel_delayed_work_sync(&chip->bc_lvl_handler);
1782     tcpm_unregister_port(chip->tcpm_port);
1783     fwnode_handle_put(chip->tcpc_dev.fwnode);
1784     destroy_workqueue(chip->wq);
1785     fusb302_debugfs_exit(chip);
1786 
1787     return 0;
1788 }
1789 
1790 static int fusb302_pm_suspend(struct device *dev)
1791 {
1792     struct fusb302_chip *chip = dev->driver_data;
1793     unsigned long flags;
1794 
1795     spin_lock_irqsave(&chip->irq_lock, flags);
1796     chip->irq_suspended = true;
1797     spin_unlock_irqrestore(&chip->irq_lock, flags);
1798 
1799     /* Make sure any pending irq_work is finished before the bus suspends */
1800     flush_work(&chip->irq_work);
1801     return 0;
1802 }
1803 
1804 static int fusb302_pm_resume(struct device *dev)
1805 {
1806     struct fusb302_chip *chip = dev->driver_data;
1807     unsigned long flags;
1808 
1809     spin_lock_irqsave(&chip->irq_lock, flags);
1810     if (chip->irq_while_suspended) {
1811         schedule_work(&chip->irq_work);
1812         chip->irq_while_suspended = false;
1813     }
1814     chip->irq_suspended = false;
1815     spin_unlock_irqrestore(&chip->irq_lock, flags);
1816 
1817     return 0;
1818 }
1819 
1820 static const struct of_device_id fusb302_dt_match[] = {
1821     {.compatible = "fcs,fusb302"},
1822     {},
1823 };
1824 MODULE_DEVICE_TABLE(of, fusb302_dt_match);
1825 
1826 static const struct i2c_device_id fusb302_i2c_device_id[] = {
1827     {"typec_fusb302", 0},
1828     {},
1829 };
1830 MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
1831 
1832 static const struct dev_pm_ops fusb302_pm_ops = {
1833     .suspend = fusb302_pm_suspend,
1834     .resume = fusb302_pm_resume,
1835 };
1836 
1837 static struct i2c_driver fusb302_driver = {
1838     .driver = {
1839            .name = "typec_fusb302",
1840            .pm = &fusb302_pm_ops,
1841            .of_match_table = of_match_ptr(fusb302_dt_match),
1842            },
1843     .probe = fusb302_probe,
1844     .remove = fusb302_remove,
1845     .id_table = fusb302_i2c_device_id,
1846 };
1847 module_i2c_driver(fusb302_driver);
1848 
1849 MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1850 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1851 MODULE_LICENSE("GPL");