1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2016-2017 Google, Inc
5 * Fairchild FUSB302 Type-C Chip Driver
8 #include <linux/debugfs.h>
9 #include <linux/delay.h>
10 #include <linux/errno.h>
11 #include <linux/extcon.h>
12 #include <linux/gpio/consumer.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/pinctrl/consumer.h>
20 #include <linux/proc_fs.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/sched/clock.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
28 #include <linux/usb.h>
29 #include <linux/usb/typec.h>
30 #include <linux/usb/tcpm.h>
31 #include <linux/usb/pd.h>
32 #include <linux/workqueue.h>
34 #include "fusb302_reg.h"
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).
43 #define T_BC_LVL_DEBOUNCE_DELAY_MS 30
52 enum src_current_status {
58 static const u8 ra_mda_value[] = {
59 [SRC_CURRENT_DEFAULT] = 4, /* 210mV */
60 [SRC_CURRENT_MEDIUM] = 9, /* 420mV */
61 [SRC_CURRENT_HIGH] = 18, /* 798mV */
64 static const u8 rd_mda_value[] = {
65 [SRC_CURRENT_DEFAULT] = 38, /* 1638mV */
66 [SRC_CURRENT_MEDIUM] = 38, /* 1638mV */
67 [SRC_CURRENT_HIGH] = 61, /* 2604mV */
70 #define LOG_BUFFER_ENTRIES 1024
71 #define LOG_BUFFER_ENTRY_SIZE 128
75 struct i2c_client *i2c_client;
76 struct tcpm_port *tcpm_port;
77 struct tcpc_dev tcpc_dev;
79 struct regulator *vbus;
82 struct work_struct irq_work;
84 bool irq_while_suspended;
85 struct gpio_desc *gpio_int_n;
87 struct extcon_dev *extcon;
89 struct workqueue_struct *wq;
90 struct delayed_work bc_lvl_handler;
92 /* lock for sharing chip states */
96 enum toggling_mode toggling_mode;
97 enum src_current_status src_current_status;
107 enum typec_cc_polarity cc_polarity;
108 enum typec_cc_status cc1;
109 enum typec_cc_status cc2;
110 u32 snk_pdo[PDO_MAX_OBJECTS];
112 #ifdef CONFIG_DEBUG_FS
113 struct dentry *dentry;
114 /* lock for log buffer access */
115 struct mutex logbuffer_lock;
118 u8 *logbuffer[LOG_BUFFER_ENTRIES];
126 #ifdef CONFIG_DEBUG_FS
127 static bool fusb302_log_full(struct fusb302_chip *chip)
129 return chip->logbuffer_tail ==
130 (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
134 static void _fusb302_log(struct fusb302_chip *chip, const char *fmt,
137 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
138 u64 ts_nsec = local_clock();
139 unsigned long rem_nsec;
141 if (!chip->logbuffer[chip->logbuffer_head]) {
142 chip->logbuffer[chip->logbuffer_head] =
143 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
144 if (!chip->logbuffer[chip->logbuffer_head])
148 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
150 mutex_lock(&chip->logbuffer_lock);
152 if (fusb302_log_full(chip)) {
153 chip->logbuffer_head = max(chip->logbuffer_head - 1, 0);
154 strlcpy(tmpbuffer, "overflow", sizeof(tmpbuffer));
157 if (chip->logbuffer_head < 0 ||
158 chip->logbuffer_head >= LOG_BUFFER_ENTRIES) {
160 "Bad log buffer index %d\n", chip->logbuffer_head);
164 if (!chip->logbuffer[chip->logbuffer_head]) {
166 "Log buffer index %d is NULL\n", chip->logbuffer_head);
170 rem_nsec = do_div(ts_nsec, 1000000000);
171 scnprintf(chip->logbuffer[chip->logbuffer_head],
172 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
173 (unsigned long)ts_nsec, rem_nsec / 1000,
175 chip->logbuffer_head = (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
178 mutex_unlock(&chip->logbuffer_lock);
182 static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
187 _fusb302_log(chip, fmt, args);
191 static int fusb302_debug_show(struct seq_file *s, void *v)
193 struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
196 mutex_lock(&chip->logbuffer_lock);
197 tail = chip->logbuffer_tail;
198 while (tail != chip->logbuffer_head) {
199 seq_printf(s, "%s\n", chip->logbuffer[tail]);
200 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
202 if (!seq_has_overflowed(s))
203 chip->logbuffer_tail = tail;
204 mutex_unlock(&chip->logbuffer_lock);
208 DEFINE_SHOW_ATTRIBUTE(fusb302_debug);
210 static void fusb302_debugfs_init(struct fusb302_chip *chip)
214 mutex_init(&chip->logbuffer_lock);
215 snprintf(name, NAME_MAX, "fusb302-%s", dev_name(chip->dev));
216 chip->dentry = debugfs_create_dir(name, usb_debug_root);
217 debugfs_create_file("log", S_IFREG | 0444, chip->dentry, chip,
218 &fusb302_debug_fops);
221 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
223 debugfs_remove(chip->dentry);
228 static void fusb302_log(const struct fusb302_chip *chip,
229 const char *fmt, ...) { }
230 static void fusb302_debugfs_init(const struct fusb302_chip *chip) { }
231 static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
235 static int fusb302_i2c_write(struct fusb302_chip *chip,
240 ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
242 fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
248 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
249 u8 length, const u8 *data)
256 ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
259 fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
260 address, length, ret);
265 static int fusb302_i2c_read(struct fusb302_chip *chip,
266 u8 address, u8 *data)
270 ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
273 fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
278 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
286 ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
289 fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
290 address, length, ret);
294 fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
295 ret, length, address);
303 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
309 ret = fusb302_i2c_read(chip, address, &data);
314 ret = fusb302_i2c_write(chip, address, data);
321 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
324 return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
327 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
330 return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
333 static int fusb302_sw_reset(struct fusb302_chip *chip)
337 ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
338 FUSB_REG_RESET_SW_RESET);
340 fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
342 fusb302_log(chip, "sw reset");
347 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip, u8 retry_count)
351 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3, retry_count |
352 FUSB_REG_CONTROL3_AUTO_RETRY);
358 * initialize interrupt on the chip
359 * - unmasked interrupt: VBUS_OK
361 static int fusb302_init_interrupt(struct fusb302_chip *chip)
365 ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
366 0xFF & ~FUSB_REG_MASK_VBUSOK);
369 ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
372 ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
375 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
376 FUSB_REG_CONTROL0_INT_MASK);
383 static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
387 ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
392 static int tcpm_init(struct tcpc_dev *dev)
394 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
399 ret = fusb302_sw_reset(chip);
402 ret = fusb302_enable_tx_auto_retries(chip, FUSB_REG_CONTROL3_N_RETRIES_3);
405 ret = fusb302_init_interrupt(chip);
408 ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
411 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
414 chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
415 ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
418 fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
423 static int tcpm_get_vbus(struct tcpc_dev *dev)
425 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
429 mutex_lock(&chip->lock);
430 ret = chip->vbus_present ? 1 : 0;
431 mutex_unlock(&chip->lock);
436 static int tcpm_get_current_limit(struct tcpc_dev *dev)
438 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
440 int current_limit = 0;
441 unsigned long timeout;
447 * USB2 Charger detection may still be in progress when we get here,
448 * this can take upto 600ms, wait 800ms max.
450 timeout = jiffies + msecs_to_jiffies(800);
452 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_SDP) == 1)
455 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_CDP) == 1 ||
456 extcon_get_state(chip->extcon, EXTCON_CHG_USB_ACA) == 1)
457 current_limit = 1500;
459 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_DCP) == 1)
460 current_limit = 2000;
463 } while (current_limit == 0 && time_before(jiffies, timeout));
465 return current_limit;
468 static int fusb302_set_src_current(struct fusb302_chip *chip,
469 enum src_current_status status)
473 chip->src_current_status = status;
475 case SRC_CURRENT_DEFAULT:
476 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
477 FUSB_REG_CONTROL0_HOST_CUR_MASK,
478 FUSB_REG_CONTROL0_HOST_CUR_DEF);
480 case SRC_CURRENT_MEDIUM:
481 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
482 FUSB_REG_CONTROL0_HOST_CUR_MASK,
483 FUSB_REG_CONTROL0_HOST_CUR_MED);
485 case SRC_CURRENT_HIGH:
486 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
487 FUSB_REG_CONTROL0_HOST_CUR_MASK,
488 FUSB_REG_CONTROL0_HOST_CUR_HIGH);
497 static int fusb302_set_toggling(struct fusb302_chip *chip,
498 enum toggling_mode mode)
502 /* first disable toggling */
503 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
504 FUSB_REG_CONTROL2_TOGGLE);
507 /* mask interrupts for SRC or SNK */
508 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
509 FUSB_REG_MASK_BC_LVL |
510 FUSB_REG_MASK_COMP_CHNG);
513 chip->intr_bc_lvl = false;
514 chip->intr_comp_chng = false;
515 /* configure toggling mode: none/snk/src/drp */
517 case TOGGLING_MODE_OFF:
518 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
519 FUSB_REG_CONTROL2_MODE_MASK,
520 FUSB_REG_CONTROL2_MODE_NONE);
524 case TOGGLING_MODE_SNK:
525 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
526 FUSB_REG_CONTROL2_MODE_MASK,
527 FUSB_REG_CONTROL2_MODE_UFP);
531 case TOGGLING_MODE_SRC:
532 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
533 FUSB_REG_CONTROL2_MODE_MASK,
534 FUSB_REG_CONTROL2_MODE_DFP);
538 case TOGGLING_MODE_DRP:
539 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
540 FUSB_REG_CONTROL2_MODE_MASK,
541 FUSB_REG_CONTROL2_MODE_DRP);
549 if (mode == TOGGLING_MODE_OFF) {
550 /* mask TOGDONE interrupt */
551 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
552 FUSB_REG_MASKA_TOGDONE);
555 chip->intr_togdone = false;
557 /* Datasheet says vconn MUST be off when toggling */
558 WARN(chip->vconn_on, "Vconn is on during toggle start");
559 /* unmask TOGDONE interrupt */
560 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
561 FUSB_REG_MASKA_TOGDONE);
564 chip->intr_togdone = true;
566 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
567 FUSB_REG_CONTROL2_TOGGLE);
570 /* during toggling, consider cc as Open */
571 chip->cc1 = TYPEC_CC_OPEN;
572 chip->cc2 = TYPEC_CC_OPEN;
574 chip->toggling_mode = mode;
579 static const char * const typec_cc_status_name[] = {
580 [TYPEC_CC_OPEN] = "Open",
581 [TYPEC_CC_RA] = "Ra",
582 [TYPEC_CC_RD] = "Rd",
583 [TYPEC_CC_RP_DEF] = "Rp-def",
584 [TYPEC_CC_RP_1_5] = "Rp-1.5",
585 [TYPEC_CC_RP_3_0] = "Rp-3.0",
588 static const enum src_current_status cc_src_current[] = {
589 [TYPEC_CC_OPEN] = SRC_CURRENT_DEFAULT,
590 [TYPEC_CC_RA] = SRC_CURRENT_DEFAULT,
591 [TYPEC_CC_RD] = SRC_CURRENT_DEFAULT,
592 [TYPEC_CC_RP_DEF] = SRC_CURRENT_DEFAULT,
593 [TYPEC_CC_RP_1_5] = SRC_CURRENT_MEDIUM,
594 [TYPEC_CC_RP_3_0] = SRC_CURRENT_HIGH,
597 static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
599 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
601 u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
602 FUSB_REG_SWITCHES0_CC2_PU_EN |
603 FUSB_REG_SWITCHES0_CC1_PD_EN |
604 FUSB_REG_SWITCHES0_CC2_PD_EN;
605 u8 rd_mda, switches0_data = 0x00;
608 mutex_lock(&chip->lock);
613 switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
614 FUSB_REG_SWITCHES0_CC2_PD_EN;
616 case TYPEC_CC_RP_DEF:
617 case TYPEC_CC_RP_1_5:
618 case TYPEC_CC_RP_3_0:
619 switches0_data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
620 FUSB_REG_SWITCHES0_CC1_PU_EN :
621 FUSB_REG_SWITCHES0_CC2_PU_EN;
624 fusb302_log(chip, "unsupported cc value %s",
625 typec_cc_status_name[cc]);
630 fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
632 ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
634 fusb302_log(chip, "cannot set toggling mode, ret=%d", ret);
638 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
639 switches0_mask, switches0_data);
641 fusb302_log(chip, "cannot set pull-up/-down, ret = %d", ret);
644 /* reset the cc status */
645 chip->cc1 = TYPEC_CC_OPEN;
646 chip->cc2 = TYPEC_CC_OPEN;
648 /* adjust current for SRC */
649 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
651 fusb302_log(chip, "cannot set src current %s, ret=%d",
652 typec_cc_status_name[cc], ret);
656 /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
658 case TYPEC_CC_RP_DEF:
659 case TYPEC_CC_RP_1_5:
660 case TYPEC_CC_RP_3_0:
661 rd_mda = rd_mda_value[cc_src_current[cc]];
662 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
665 "cannot set SRC measure value, ret=%d",
669 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
670 FUSB_REG_MASK_BC_LVL |
671 FUSB_REG_MASK_COMP_CHNG,
672 FUSB_REG_MASK_COMP_CHNG);
674 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
678 chip->intr_comp_chng = true;
681 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
682 FUSB_REG_MASK_BC_LVL |
683 FUSB_REG_MASK_COMP_CHNG,
684 FUSB_REG_MASK_BC_LVL);
686 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
690 chip->intr_bc_lvl = true;
696 mutex_unlock(&chip->lock);
701 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
702 enum typec_cc_status *cc2)
704 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
707 mutex_lock(&chip->lock);
710 fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
711 typec_cc_status_name[*cc2]);
712 mutex_unlock(&chip->lock);
717 static int tcpm_set_polarity(struct tcpc_dev *dev,
718 enum typec_cc_polarity polarity)
723 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
725 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
728 u8 switches0_data = 0x00;
729 u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
730 FUSB_REG_SWITCHES0_VCONN_CC2;
732 mutex_lock(&chip->lock);
733 if (chip->vconn_on == on) {
734 fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
738 switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
739 FUSB_REG_SWITCHES0_VCONN_CC2 :
740 FUSB_REG_SWITCHES0_VCONN_CC1;
742 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
743 switches0_mask, switches0_data);
747 fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
749 mutex_unlock(&chip->lock);
754 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
756 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
760 mutex_lock(&chip->lock);
761 if (chip->vbus_on == on) {
762 fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
765 ret = regulator_enable(chip->vbus);
767 ret = regulator_disable(chip->vbus);
769 fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
770 on ? "enable" : "disable", ret);
774 fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
776 if (chip->charge_on == charge)
777 fusb302_log(chip, "charge is already %s",
778 charge ? "On" : "Off");
780 chip->charge_on = charge;
783 mutex_unlock(&chip->lock);
788 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
790 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
791 FUSB_REG_CONTROL0_TX_FLUSH);
794 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
796 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
797 FUSB_REG_CONTROL1_RX_FLUSH);
800 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
803 return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
804 FUSB_REG_SWITCHES1_AUTO_GCRC);
805 return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
806 FUSB_REG_SWITCHES1_AUTO_GCRC);
809 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
812 u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
813 u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
814 FUSB_REG_MASKA_HARDSENT |
815 FUSB_REG_MASKA_TX_SUCCESS |
816 FUSB_REG_MASKA_HARDRESET;
817 u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
820 fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
821 fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
825 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
826 fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
830 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
831 fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
835 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
837 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
841 mutex_lock(&chip->lock);
842 ret = fusb302_pd_rx_flush(chip);
844 fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
847 ret = fusb302_pd_tx_flush(chip);
849 fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
852 ret = fusb302_pd_set_auto_goodcrc(chip, on);
854 fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
855 on ? "on" : "off", ret);
858 ret = fusb302_pd_set_interrupts(chip, on);
860 fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
861 on ? "on" : "off", ret);
864 fusb302_log(chip, "pd := %s", on ? "on" : "off");
866 mutex_unlock(&chip->lock);
871 static const char * const typec_role_name[] = {
872 [TYPEC_SINK] = "Sink",
873 [TYPEC_SOURCE] = "Source",
876 static const char * const typec_data_role_name[] = {
877 [TYPEC_DEVICE] = "Device",
878 [TYPEC_HOST] = "Host",
881 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
882 enum typec_role pwr, enum typec_data_role data)
884 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
887 u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
888 FUSB_REG_SWITCHES1_DATAROLE;
889 u8 switches1_data = 0x00;
891 mutex_lock(&chip->lock);
892 if (pwr == TYPEC_SOURCE)
893 switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
894 if (data == TYPEC_HOST)
895 switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
896 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
897 switches1_mask, switches1_data);
899 fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
900 typec_role_name[pwr], typec_data_role_name[data],
904 fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
905 typec_data_role_name[data]);
907 mutex_unlock(&chip->lock);
912 static int tcpm_start_toggling(struct tcpc_dev *dev,
913 enum typec_port_type port_type,
914 enum typec_cc_status cc)
916 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
918 enum toggling_mode mode = TOGGLING_MODE_OFF;
923 mode = TOGGLING_MODE_SRC;
926 mode = TOGGLING_MODE_SNK;
929 mode = TOGGLING_MODE_DRP;
933 mutex_lock(&chip->lock);
934 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
936 fusb302_log(chip, "unable to set src current %s, ret=%d",
937 typec_cc_status_name[cc], ret);
940 ret = fusb302_set_toggling(chip, mode);
943 "unable to start drp toggling, ret=%d", ret);
946 fusb302_log(chip, "start drp toggling");
948 mutex_unlock(&chip->lock);
953 static int fusb302_pd_send_message(struct fusb302_chip *chip,
954 const struct pd_message *msg)
962 buf[pos++] = FUSB302_TKN_SYNC1;
963 buf[pos++] = FUSB302_TKN_SYNC1;
964 buf[pos++] = FUSB302_TKN_SYNC1;
965 buf[pos++] = FUSB302_TKN_SYNC2;
967 len = pd_header_cnt_le(msg->header) * 4;
968 /* plug 2 for header */
972 "PD message too long %d (incl. header)", len);
975 /* packsym tells the FUSB302 chip that the next X bytes are payload */
976 buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
977 memcpy(&buf[pos], &msg->header, sizeof(msg->header));
978 pos += sizeof(msg->header);
981 memcpy(&buf[pos], msg->payload, len);
985 buf[pos++] = FUSB302_TKN_JAMCRC;
987 buf[pos++] = FUSB302_TKN_EOP;
988 /* turn tx off after sending message */
989 buf[pos++] = FUSB302_TKN_TXOFF;
990 /* start transmission */
991 buf[pos++] = FUSB302_TKN_TXON;
993 ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
996 fusb302_log(chip, "sending PD message header: %x", msg->header);
997 fusb302_log(chip, "sending PD message len: %d", len);
1002 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1004 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1005 FUSB_REG_CONTROL3_SEND_HARDRESET);
1008 static const char * const transmit_type_name[] = {
1009 [TCPC_TX_SOP] = "SOP",
1010 [TCPC_TX_SOP_PRIME] = "SOP'",
1011 [TCPC_TX_SOP_PRIME_PRIME] = "SOP''",
1012 [TCPC_TX_SOP_DEBUG_PRIME] = "DEBUG'",
1013 [TCPC_TX_SOP_DEBUG_PRIME_PRIME] = "DEBUG''",
1014 [TCPC_TX_HARD_RESET] = "HARD_RESET",
1015 [TCPC_TX_CABLE_RESET] = "CABLE_RESET",
1016 [TCPC_TX_BIST_MODE_2] = "BIST_MODE_2",
1019 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1020 const struct pd_message *msg, unsigned int negotiated_rev)
1022 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1026 mutex_lock(&chip->lock);
1029 /* nRetryCount 3 in P2.0 spec, whereas 2 in PD3.0 spec */
1030 ret = fusb302_enable_tx_auto_retries(chip, negotiated_rev > PD_REV20 ?
1031 FUSB_REG_CONTROL3_N_RETRIES_2 :
1032 FUSB_REG_CONTROL3_N_RETRIES_3);
1034 fusb302_log(chip, "Cannot update retry count ret=%d", ret);
1036 ret = fusb302_pd_send_message(chip, msg);
1039 "cannot send PD message, ret=%d", ret);
1041 case TCPC_TX_HARD_RESET:
1042 ret = fusb302_pd_send_hardreset(chip);
1045 "cannot send hardreset, ret=%d", ret);
1048 fusb302_log(chip, "type %s not supported",
1049 transmit_type_name[type]);
1052 mutex_unlock(&chip->lock);
1057 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1059 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1060 return TYPEC_CC_RP_3_0;
1061 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1062 return TYPEC_CC_RP_1_5;
1063 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1064 return TYPEC_CC_RP_DEF;
1065 return TYPEC_CC_OPEN;
1068 static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1070 struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1071 bc_lvl_handler.work);
1075 enum typec_cc_status cc_status;
1077 mutex_lock(&chip->lock);
1078 if (!chip->intr_bc_lvl) {
1079 fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1082 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1085 fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1086 if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1087 fusb302_log(chip, "CC activities detected, delay handling");
1088 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1089 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1092 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1093 cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1094 if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1095 if (chip->cc1 != cc_status) {
1096 fusb302_log(chip, "cc1: %s -> %s",
1097 typec_cc_status_name[chip->cc1],
1098 typec_cc_status_name[cc_status]);
1099 chip->cc1 = cc_status;
1100 tcpm_cc_change(chip->tcpm_port);
1103 if (chip->cc2 != cc_status) {
1104 fusb302_log(chip, "cc2: %s -> %s",
1105 typec_cc_status_name[chip->cc2],
1106 typec_cc_status_name[cc_status]);
1107 chip->cc2 = cc_status;
1108 tcpm_cc_change(chip->tcpm_port);
1113 mutex_unlock(&chip->lock);
1116 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1118 fusb302_tcpc_dev->init = tcpm_init;
1119 fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1120 fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
1121 fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1122 fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1123 fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1124 fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1125 fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1126 fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1127 fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1128 fusb302_tcpc_dev->start_toggling = tcpm_start_toggling;
1129 fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1132 static const char * const cc_polarity_name[] = {
1133 [TYPEC_POLARITY_CC1] = "Polarity_CC1",
1134 [TYPEC_POLARITY_CC2] = "Polarity_CC2",
1137 static int fusb302_set_cc_polarity_and_pull(struct fusb302_chip *chip,
1138 enum typec_cc_polarity cc_polarity,
1139 bool pull_up, bool pull_down)
1142 u8 switches0_data = 0x00;
1143 u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
1144 FUSB_REG_SWITCHES1_TXCC2_EN;
1145 u8 switches1_data = 0x00;
1148 switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
1149 FUSB_REG_SWITCHES0_CC2_PD_EN;
1151 if (cc_polarity == TYPEC_POLARITY_CC1) {
1152 switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC1;
1154 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1156 switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1157 switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1159 switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC2;
1161 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1163 switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1164 switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1166 ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1169 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1170 switches1_mask, switches1_data);
1173 chip->cc_polarity = cc_polarity;
1178 static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1184 enum typec_cc_polarity cc_polarity;
1185 enum typec_cc_status cc_status_active, cc1, cc2;
1187 /* set polarity and pull_up, pull_down */
1188 cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1189 TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1190 ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, false, true);
1192 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1193 cc_polarity_name[cc_polarity], ret);
1196 /* fusb302_set_cc_polarity() has set the correct measure block */
1197 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1200 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1201 cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1202 /* restart toggling if the cc status on the active line is OPEN */
1203 if (cc_status_active == TYPEC_CC_OPEN) {
1204 fusb302_log(chip, "restart toggling as CC_OPEN detected");
1205 ret = fusb302_set_toggling(chip, chip->toggling_mode);
1208 /* update tcpm with the new cc value */
1209 cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1210 cc_status_active : TYPEC_CC_OPEN;
1211 cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1212 cc_status_active : TYPEC_CC_OPEN;
1213 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1216 tcpm_cc_change(chip->tcpm_port);
1218 /* turn off toggling */
1219 ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1222 "cannot set toggling mode off, ret=%d", ret);
1225 /* unmask bc_lvl interrupt */
1226 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1229 "cannot unmask bc_lcl interrupt, ret=%d", ret);
1232 chip->intr_bc_lvl = true;
1233 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1234 typec_cc_status_name[cc1],
1235 typec_cc_status_name[cc2]);
1240 /* On error returns < 0, otherwise a typec_cc_status value */
1241 static int fusb302_get_src_cc_status(struct fusb302_chip *chip,
1242 enum typec_cc_polarity cc_polarity,
1243 enum typec_cc_status *cc)
1245 u8 ra_mda = ra_mda_value[chip->src_current_status];
1246 u8 rd_mda = rd_mda_value[chip->src_current_status];
1247 u8 switches0_data, status0;
1250 /* Step 1: Set switches so that we measure the right CC pin */
1251 switches0_data = (cc_polarity == TYPEC_POLARITY_CC1) ?
1252 FUSB_REG_SWITCHES0_CC1_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC1 :
1253 FUSB_REG_SWITCHES0_CC2_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC2;
1254 ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1258 fusb302_i2c_read(chip, FUSB_REG_SWITCHES0, &status0);
1259 fusb302_log(chip, "get_src_cc_status switches: 0x%0x", status0);
1261 /* Step 2: Set compararator volt to differentiate between Open and Rd */
1262 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1266 usleep_range(50, 100);
1267 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1271 fusb302_log(chip, "get_src_cc_status rd_mda status0: 0x%0x", status0);
1272 if (status0 & FUSB_REG_STATUS0_COMP) {
1273 *cc = TYPEC_CC_OPEN;
1277 /* Step 3: Set compararator input to differentiate between Rd and Ra. */
1278 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1282 usleep_range(50, 100);
1283 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1287 fusb302_log(chip, "get_src_cc_status ra_mda status0: 0x%0x", status0);
1288 if (status0 & FUSB_REG_STATUS0_COMP)
1296 static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1300 * - set polarity (measure cc, vconn, tx)
1301 * - set pull_up, pull_down
1302 * - set cc1, cc2, and update to tcpm_port
1303 * - set I_COMP interrupt on
1306 u8 rd_mda = rd_mda_value[chip->src_current_status];
1307 enum toggling_mode toggling_mode = chip->toggling_mode;
1308 enum typec_cc_polarity cc_polarity;
1309 enum typec_cc_status cc1, cc2;
1312 * The toggle-engine will stop in a src state if it sees either Ra or
1313 * Rd. Determine the status for both CC pins, starting with the one
1314 * where toggling stopped, as that is where the switches point now.
1316 if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1317 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1319 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1322 /* we must turn off toggling before we can measure the other pin */
1323 ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1325 fusb302_log(chip, "cannot set toggling mode off, ret=%d", ret);
1328 /* get the status of the other pin */
1329 if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1330 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1332 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1336 /* determine polarity based on the status of both pins */
1337 if (cc1 == TYPEC_CC_RD &&
1338 (cc2 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_RA)) {
1339 cc_polarity = TYPEC_POLARITY_CC1;
1340 } else if (cc2 == TYPEC_CC_RD &&
1341 (cc1 == TYPEC_CC_OPEN || cc1 == TYPEC_CC_RA)) {
1342 cc_polarity = TYPEC_POLARITY_CC2;
1344 fusb302_log(chip, "unexpected CC status cc1=%s, cc2=%s, restarting toggling",
1345 typec_cc_status_name[cc1],
1346 typec_cc_status_name[cc2]);
1347 return fusb302_set_toggling(chip, toggling_mode);
1349 /* set polarity and pull_up, pull_down */
1350 ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, true, false);
1352 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1353 cc_polarity_name[cc_polarity], ret);
1356 /* update tcpm with the new cc value */
1357 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1360 tcpm_cc_change(chip->tcpm_port);
1362 /* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1363 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1366 /* unmask comp_chng interrupt */
1367 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1368 FUSB_REG_MASK_COMP_CHNG);
1371 "cannot unmask comp_chng interrupt, ret=%d", ret);
1374 chip->intr_comp_chng = true;
1375 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1376 typec_cc_status_name[cc1],
1377 typec_cc_status_name[cc2]);
1382 static int fusb302_handle_togdone(struct fusb302_chip *chip)
1388 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1391 togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1392 FUSB_REG_STATUS1A_TOGSS_MASK;
1393 switch (togdone_result) {
1394 case FUSB_REG_STATUS1A_TOGSS_SNK1:
1395 case FUSB_REG_STATUS1A_TOGSS_SNK2:
1396 return fusb302_handle_togdone_snk(chip, togdone_result);
1397 case FUSB_REG_STATUS1A_TOGSS_SRC1:
1398 case FUSB_REG_STATUS1A_TOGSS_SRC2:
1399 return fusb302_handle_togdone_src(chip, togdone_result);
1400 case FUSB_REG_STATUS1A_TOGSS_AA:
1401 /* doesn't support */
1402 fusb302_log(chip, "AudioAccessory not supported");
1403 fusb302_set_toggling(chip, chip->toggling_mode);
1406 fusb302_log(chip, "TOGDONE with an invalid state: %d",
1408 fusb302_set_toggling(chip, chip->toggling_mode);
1414 static int fusb302_pd_reset(struct fusb302_chip *chip)
1416 return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1417 FUSB_REG_RESET_PD_RESET);
1420 static int fusb302_pd_read_message(struct fusb302_chip *chip,
1421 struct pd_message *msg)
1428 /* first SOP token */
1429 ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1432 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1433 (u8 *)&msg->header);
1436 len = pd_header_cnt_le(msg->header) * 4;
1437 /* add 4 to length to include the CRC */
1438 if (len > PD_MAX_PAYLOAD * 4) {
1439 fusb302_log(chip, "PD message too long %d", len);
1443 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1444 (u8 *)msg->payload);
1448 /* another 4 bytes to read CRC out */
1449 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1452 fusb302_log(chip, "PD message header: %x", msg->header);
1453 fusb302_log(chip, "PD message len: %d", len);
1456 * Check if we've read off a GoodCRC message. If so then indicate to
1457 * TCPM that the previous transmission has completed. Otherwise we pass
1458 * the received message over to TCPM for processing.
1460 * We make this check here instead of basing the reporting decision on
1461 * the IRQ event type, as it's possible for the chip to report the
1462 * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
1463 * to check the message type to ensure correct reporting to TCPM.
1465 if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
1466 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1468 tcpm_pd_receive(chip->tcpm_port, msg);
1473 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1475 struct fusb302_chip *chip = dev_id;
1476 unsigned long flags;
1478 /* Disable our level triggered IRQ until our irq_work has cleared it */
1479 disable_irq_nosync(chip->gpio_int_n_irq);
1481 spin_lock_irqsave(&chip->irq_lock, flags);
1482 if (chip->irq_suspended)
1483 chip->irq_while_suspended = true;
1485 schedule_work(&chip->irq_work);
1486 spin_unlock_irqrestore(&chip->irq_lock, flags);
1491 static void fusb302_irq_work(struct work_struct *work)
1493 struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1504 bool intr_comp_chng;
1505 struct pd_message pd_msg;
1507 mutex_lock(&chip->lock);
1508 /* grab a snapshot of intr flags */
1509 intr_togdone = chip->intr_togdone;
1510 intr_bc_lvl = chip->intr_bc_lvl;
1511 intr_comp_chng = chip->intr_comp_chng;
1513 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1516 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1519 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1522 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1526 "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1527 interrupt, interrupta, interruptb, status0);
1529 if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1530 vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1531 fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1532 vbus_present ? "On" : "Off");
1533 if (vbus_present != chip->vbus_present) {
1534 chip->vbus_present = vbus_present;
1535 tcpm_vbus_change(chip->tcpm_port);
1539 if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1540 fusb302_log(chip, "IRQ: TOGDONE");
1541 ret = fusb302_handle_togdone(chip);
1544 "handle togdone error, ret=%d", ret);
1549 if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1550 fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1552 * as BC_LVL interrupt can be affected by PD activity,
1553 * apply delay to for the handler to wait for the PD
1554 * signaling to finish.
1556 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1557 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1560 if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1561 comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1562 fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1563 comp_result ? "true" : "false");
1565 /* cc level > Rd_threshold, detach */
1566 chip->cc1 = TYPEC_CC_OPEN;
1567 chip->cc2 = TYPEC_CC_OPEN;
1568 tcpm_cc_change(chip->tcpm_port);
1572 if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1573 fusb302_log(chip, "IRQ: PD collision");
1574 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1577 if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1578 fusb302_log(chip, "IRQ: PD retry failed");
1579 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1582 if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1583 fusb302_log(chip, "IRQ: PD hardreset sent");
1584 ret = fusb302_pd_reset(chip);
1586 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1589 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1592 if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1593 fusb302_log(chip, "IRQ: PD tx success");
1594 ret = fusb302_pd_read_message(chip, &pd_msg);
1597 "cannot read in PD message, ret=%d", ret);
1602 if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1603 fusb302_log(chip, "IRQ: PD received hardreset");
1604 ret = fusb302_pd_reset(chip);
1606 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1609 tcpm_pd_hard_reset(chip->tcpm_port);
1612 if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1613 fusb302_log(chip, "IRQ: PD sent good CRC");
1614 ret = fusb302_pd_read_message(chip, &pd_msg);
1617 "cannot read in PD message, ret=%d", ret);
1622 mutex_unlock(&chip->lock);
1623 enable_irq(chip->gpio_int_n_irq);
1626 static int init_gpio(struct fusb302_chip *chip)
1628 struct device *dev = chip->dev;
1631 chip->gpio_int_n = devm_gpiod_get(dev, "fcs,int_n", GPIOD_IN);
1632 if (IS_ERR(chip->gpio_int_n)) {
1633 dev_err(dev, "failed to request gpio_int_n\n");
1634 return PTR_ERR(chip->gpio_int_n);
1636 ret = gpiod_to_irq(chip->gpio_int_n);
1639 "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1642 chip->gpio_int_n_irq = ret;
1646 #define PDO_FIXED_FLAGS \
1647 (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1649 static const u32 src_pdo[] = {
1650 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1653 static const u32 snk_pdo[] = {
1654 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1657 static const struct property_entry port_props[] = {
1658 PROPERTY_ENTRY_STRING("data-role", "dual"),
1659 PROPERTY_ENTRY_STRING("power-role", "dual"),
1660 PROPERTY_ENTRY_STRING("try-power-role", "sink"),
1661 PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
1662 PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
1663 PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
1667 static struct fwnode_handle *fusb302_fwnode_get(struct device *dev)
1669 struct fwnode_handle *fwnode;
1671 fwnode = device_get_named_child_node(dev, "connector");
1673 fwnode = fwnode_create_software_node(port_props, NULL);
1678 static int fusb302_probe(struct i2c_client *client,
1679 const struct i2c_device_id *id)
1681 struct fusb302_chip *chip;
1682 struct i2c_adapter *adapter = client->adapter;
1683 struct device *dev = &client->dev;
1687 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1688 dev_err(&client->dev,
1689 "I2C/SMBus block functionality not supported!\n");
1692 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1696 chip->i2c_client = client;
1697 chip->dev = &client->dev;
1698 mutex_init(&chip->lock);
1701 * Devicetree platforms should get extcon via phandle (not yet
1702 * supported). On ACPI platforms, we get the name from a device prop.
1703 * This device prop is for kernel internal use only and is expected
1704 * to be set by the platform code which also registers the i2c client
1707 if (device_property_read_string(dev, "linux,extcon-name", &name) == 0) {
1708 chip->extcon = extcon_get_extcon_dev(name);
1710 return -EPROBE_DEFER;
1713 chip->vbus = devm_regulator_get(chip->dev, "vbus");
1714 if (IS_ERR(chip->vbus))
1715 return PTR_ERR(chip->vbus);
1717 chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1721 spin_lock_init(&chip->irq_lock);
1722 INIT_WORK(&chip->irq_work, fusb302_irq_work);
1723 INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1724 init_tcpc_dev(&chip->tcpc_dev);
1725 fusb302_debugfs_init(chip);
1728 chip->gpio_int_n_irq = client->irq;
1730 ret = init_gpio(chip);
1732 goto destroy_workqueue;
1735 chip->tcpc_dev.fwnode = fusb302_fwnode_get(dev);
1736 if (IS_ERR(chip->tcpc_dev.fwnode)) {
1737 ret = PTR_ERR(chip->tcpc_dev.fwnode);
1738 goto destroy_workqueue;
1741 chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1742 if (IS_ERR(chip->tcpm_port)) {
1743 fwnode_handle_put(chip->tcpc_dev.fwnode);
1744 ret = PTR_ERR(chip->tcpm_port);
1745 if (ret != -EPROBE_DEFER)
1746 dev_err(dev, "cannot register tcpm port, ret=%d", ret);
1747 goto destroy_workqueue;
1750 ret = request_irq(chip->gpio_int_n_irq, fusb302_irq_intn,
1751 IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1752 "fsc_interrupt_int_n", chip);
1754 dev_err(dev, "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1755 goto tcpm_unregister_port;
1757 enable_irq_wake(chip->gpio_int_n_irq);
1758 i2c_set_clientdata(client, chip);
1762 tcpm_unregister_port:
1763 tcpm_unregister_port(chip->tcpm_port);
1764 fwnode_handle_put(chip->tcpc_dev.fwnode);
1766 fusb302_debugfs_exit(chip);
1767 destroy_workqueue(chip->wq);
1772 static int fusb302_remove(struct i2c_client *client)
1774 struct fusb302_chip *chip = i2c_get_clientdata(client);
1776 disable_irq_wake(chip->gpio_int_n_irq);
1777 free_irq(chip->gpio_int_n_irq, chip);
1778 cancel_work_sync(&chip->irq_work);
1779 cancel_delayed_work_sync(&chip->bc_lvl_handler);
1780 tcpm_unregister_port(chip->tcpm_port);
1781 fwnode_handle_put(chip->tcpc_dev.fwnode);
1782 destroy_workqueue(chip->wq);
1783 fusb302_debugfs_exit(chip);
1788 static int fusb302_pm_suspend(struct device *dev)
1790 struct fusb302_chip *chip = dev->driver_data;
1791 unsigned long flags;
1793 spin_lock_irqsave(&chip->irq_lock, flags);
1794 chip->irq_suspended = true;
1795 spin_unlock_irqrestore(&chip->irq_lock, flags);
1797 /* Make sure any pending irq_work is finished before the bus suspends */
1798 flush_work(&chip->irq_work);
1802 static int fusb302_pm_resume(struct device *dev)
1804 struct fusb302_chip *chip = dev->driver_data;
1805 unsigned long flags;
1807 spin_lock_irqsave(&chip->irq_lock, flags);
1808 if (chip->irq_while_suspended) {
1809 schedule_work(&chip->irq_work);
1810 chip->irq_while_suspended = false;
1812 chip->irq_suspended = false;
1813 spin_unlock_irqrestore(&chip->irq_lock, flags);
1818 static const struct of_device_id fusb302_dt_match[] = {
1819 {.compatible = "fcs,fusb302"},
1822 MODULE_DEVICE_TABLE(of, fusb302_dt_match);
1824 static const struct i2c_device_id fusb302_i2c_device_id[] = {
1825 {"typec_fusb302", 0},
1828 MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
1830 static const struct dev_pm_ops fusb302_pm_ops = {
1831 .suspend = fusb302_pm_suspend,
1832 .resume = fusb302_pm_resume,
1835 static struct i2c_driver fusb302_driver = {
1837 .name = "typec_fusb302",
1838 .pm = &fusb302_pm_ops,
1839 .of_match_table = of_match_ptr(fusb302_dt_match),
1841 .probe = fusb302_probe,
1842 .remove = fusb302_remove,
1843 .id_table = fusb302_i2c_device_id,
1845 module_i2c_driver(fusb302_driver);
1847 MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1848 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1849 MODULE_LICENSE("GPL");