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_file(name, S_IFREG | 0444, usb_debug_root,
217 chip, &fusb302_debug_fops);
220 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
222 debugfs_remove(chip->dentry);
227 static void fusb302_log(const struct fusb302_chip *chip,
228 const char *fmt, ...) { }
229 static void fusb302_debugfs_init(const struct fusb302_chip *chip) { }
230 static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
234 static int fusb302_i2c_write(struct fusb302_chip *chip,
239 ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
241 fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
247 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
248 u8 length, const u8 *data)
255 ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
258 fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
259 address, length, ret);
264 static int fusb302_i2c_read(struct fusb302_chip *chip,
265 u8 address, u8 *data)
269 ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
272 fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
277 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
285 ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
288 fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
289 address, length, ret);
293 fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
294 ret, length, address);
302 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
308 ret = fusb302_i2c_read(chip, address, &data);
313 ret = fusb302_i2c_write(chip, address, data);
320 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
323 return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
326 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
329 return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
332 static int fusb302_sw_reset(struct fusb302_chip *chip)
336 ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
337 FUSB_REG_RESET_SW_RESET);
339 fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
341 fusb302_log(chip, "sw reset");
346 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip, u8 retry_count)
350 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3, retry_count |
351 FUSB_REG_CONTROL3_AUTO_RETRY);
357 * initialize interrupt on the chip
358 * - unmasked interrupt: VBUS_OK
360 static int fusb302_init_interrupt(struct fusb302_chip *chip)
364 ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
365 0xFF & ~FUSB_REG_MASK_VBUSOK);
368 ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
371 ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
374 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
375 FUSB_REG_CONTROL0_INT_MASK);
382 static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
386 ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
391 static int tcpm_init(struct tcpc_dev *dev)
393 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
398 ret = fusb302_sw_reset(chip);
401 ret = fusb302_enable_tx_auto_retries(chip, FUSB_REG_CONTROL3_N_RETRIES_3);
404 ret = fusb302_init_interrupt(chip);
407 ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
410 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
413 chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
414 ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
417 fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
422 static int tcpm_get_vbus(struct tcpc_dev *dev)
424 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
428 mutex_lock(&chip->lock);
429 ret = chip->vbus_present ? 1 : 0;
430 mutex_unlock(&chip->lock);
435 static int tcpm_get_current_limit(struct tcpc_dev *dev)
437 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
439 int current_limit = 0;
440 unsigned long timeout;
446 * USB2 Charger detection may still be in progress when we get here,
447 * this can take upto 600ms, wait 800ms max.
449 timeout = jiffies + msecs_to_jiffies(800);
451 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_SDP) == 1)
454 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_CDP) == 1 ||
455 extcon_get_state(chip->extcon, EXTCON_CHG_USB_ACA) == 1)
456 current_limit = 1500;
458 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_DCP) == 1)
459 current_limit = 2000;
462 } while (current_limit == 0 && time_before(jiffies, timeout));
464 return current_limit;
467 static int fusb302_set_src_current(struct fusb302_chip *chip,
468 enum src_current_status status)
472 chip->src_current_status = status;
474 case SRC_CURRENT_DEFAULT:
475 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
476 FUSB_REG_CONTROL0_HOST_CUR_MASK,
477 FUSB_REG_CONTROL0_HOST_CUR_DEF);
479 case SRC_CURRENT_MEDIUM:
480 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
481 FUSB_REG_CONTROL0_HOST_CUR_MASK,
482 FUSB_REG_CONTROL0_HOST_CUR_MED);
484 case SRC_CURRENT_HIGH:
485 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
486 FUSB_REG_CONTROL0_HOST_CUR_MASK,
487 FUSB_REG_CONTROL0_HOST_CUR_HIGH);
496 static int fusb302_set_toggling(struct fusb302_chip *chip,
497 enum toggling_mode mode)
501 /* first disable toggling */
502 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
503 FUSB_REG_CONTROL2_TOGGLE);
506 /* mask interrupts for SRC or SNK */
507 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
508 FUSB_REG_MASK_BC_LVL |
509 FUSB_REG_MASK_COMP_CHNG);
512 chip->intr_bc_lvl = false;
513 chip->intr_comp_chng = false;
514 /* configure toggling mode: none/snk/src/drp */
516 case TOGGLING_MODE_OFF:
517 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
518 FUSB_REG_CONTROL2_MODE_MASK,
519 FUSB_REG_CONTROL2_MODE_NONE);
523 case TOGGLING_MODE_SNK:
524 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
525 FUSB_REG_CONTROL2_MODE_MASK,
526 FUSB_REG_CONTROL2_MODE_UFP);
530 case TOGGLING_MODE_SRC:
531 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
532 FUSB_REG_CONTROL2_MODE_MASK,
533 FUSB_REG_CONTROL2_MODE_DFP);
537 case TOGGLING_MODE_DRP:
538 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
539 FUSB_REG_CONTROL2_MODE_MASK,
540 FUSB_REG_CONTROL2_MODE_DRP);
548 if (mode == TOGGLING_MODE_OFF) {
549 /* mask TOGDONE interrupt */
550 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
551 FUSB_REG_MASKA_TOGDONE);
554 chip->intr_togdone = false;
556 /* Datasheet says vconn MUST be off when toggling */
557 WARN(chip->vconn_on, "Vconn is on during toggle start");
558 /* unmask TOGDONE interrupt */
559 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
560 FUSB_REG_MASKA_TOGDONE);
563 chip->intr_togdone = true;
565 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
566 FUSB_REG_CONTROL2_TOGGLE);
569 /* during toggling, consider cc as Open */
570 chip->cc1 = TYPEC_CC_OPEN;
571 chip->cc2 = TYPEC_CC_OPEN;
573 chip->toggling_mode = mode;
578 static const char * const typec_cc_status_name[] = {
579 [TYPEC_CC_OPEN] = "Open",
580 [TYPEC_CC_RA] = "Ra",
581 [TYPEC_CC_RD] = "Rd",
582 [TYPEC_CC_RP_DEF] = "Rp-def",
583 [TYPEC_CC_RP_1_5] = "Rp-1.5",
584 [TYPEC_CC_RP_3_0] = "Rp-3.0",
587 static const enum src_current_status cc_src_current[] = {
588 [TYPEC_CC_OPEN] = SRC_CURRENT_DEFAULT,
589 [TYPEC_CC_RA] = SRC_CURRENT_DEFAULT,
590 [TYPEC_CC_RD] = SRC_CURRENT_DEFAULT,
591 [TYPEC_CC_RP_DEF] = SRC_CURRENT_DEFAULT,
592 [TYPEC_CC_RP_1_5] = SRC_CURRENT_MEDIUM,
593 [TYPEC_CC_RP_3_0] = SRC_CURRENT_HIGH,
596 static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
598 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
600 u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
601 FUSB_REG_SWITCHES0_CC2_PU_EN |
602 FUSB_REG_SWITCHES0_CC1_PD_EN |
603 FUSB_REG_SWITCHES0_CC2_PD_EN;
604 u8 rd_mda, switches0_data = 0x00;
607 mutex_lock(&chip->lock);
612 switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
613 FUSB_REG_SWITCHES0_CC2_PD_EN;
615 case TYPEC_CC_RP_DEF:
616 case TYPEC_CC_RP_1_5:
617 case TYPEC_CC_RP_3_0:
618 switches0_data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
619 FUSB_REG_SWITCHES0_CC1_PU_EN :
620 FUSB_REG_SWITCHES0_CC2_PU_EN;
623 fusb302_log(chip, "unsupported cc value %s",
624 typec_cc_status_name[cc]);
629 fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
631 ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
633 fusb302_log(chip, "cannot set toggling mode, ret=%d", ret);
637 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
638 switches0_mask, switches0_data);
640 fusb302_log(chip, "cannot set pull-up/-down, ret = %d", ret);
643 /* reset the cc status */
644 chip->cc1 = TYPEC_CC_OPEN;
645 chip->cc2 = TYPEC_CC_OPEN;
647 /* adjust current for SRC */
648 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
650 fusb302_log(chip, "cannot set src current %s, ret=%d",
651 typec_cc_status_name[cc], ret);
655 /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
657 case TYPEC_CC_RP_DEF:
658 case TYPEC_CC_RP_1_5:
659 case TYPEC_CC_RP_3_0:
660 rd_mda = rd_mda_value[cc_src_current[cc]];
661 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
664 "cannot set SRC measure value, ret=%d",
668 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
669 FUSB_REG_MASK_BC_LVL |
670 FUSB_REG_MASK_COMP_CHNG,
671 FUSB_REG_MASK_COMP_CHNG);
673 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
677 chip->intr_comp_chng = true;
680 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
681 FUSB_REG_MASK_BC_LVL |
682 FUSB_REG_MASK_COMP_CHNG,
683 FUSB_REG_MASK_BC_LVL);
685 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
689 chip->intr_bc_lvl = true;
695 mutex_unlock(&chip->lock);
700 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
701 enum typec_cc_status *cc2)
703 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
706 mutex_lock(&chip->lock);
709 fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
710 typec_cc_status_name[*cc2]);
711 mutex_unlock(&chip->lock);
716 static int tcpm_set_polarity(struct tcpc_dev *dev,
717 enum typec_cc_polarity polarity)
722 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
724 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
727 u8 switches0_data = 0x00;
728 u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
729 FUSB_REG_SWITCHES0_VCONN_CC2;
731 mutex_lock(&chip->lock);
732 if (chip->vconn_on == on) {
733 fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
737 switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
738 FUSB_REG_SWITCHES0_VCONN_CC2 :
739 FUSB_REG_SWITCHES0_VCONN_CC1;
741 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
742 switches0_mask, switches0_data);
746 fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
748 mutex_unlock(&chip->lock);
753 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
755 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
759 mutex_lock(&chip->lock);
760 if (chip->vbus_on == on) {
761 fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
764 ret = regulator_enable(chip->vbus);
766 ret = regulator_disable(chip->vbus);
768 fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
769 on ? "enable" : "disable", ret);
773 fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
775 if (chip->charge_on == charge)
776 fusb302_log(chip, "charge is already %s",
777 charge ? "On" : "Off");
779 chip->charge_on = charge;
782 mutex_unlock(&chip->lock);
787 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
789 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
790 FUSB_REG_CONTROL0_TX_FLUSH);
793 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
795 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
796 FUSB_REG_CONTROL1_RX_FLUSH);
799 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
802 return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
803 FUSB_REG_SWITCHES1_AUTO_GCRC);
804 return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
805 FUSB_REG_SWITCHES1_AUTO_GCRC);
808 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
811 u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
812 u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
813 FUSB_REG_MASKA_HARDSENT |
814 FUSB_REG_MASKA_TX_SUCCESS |
815 FUSB_REG_MASKA_HARDRESET;
816 u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
819 fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
820 fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
824 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
825 fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
829 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
830 fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
834 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
836 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
840 mutex_lock(&chip->lock);
841 ret = fusb302_pd_rx_flush(chip);
843 fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
846 ret = fusb302_pd_tx_flush(chip);
848 fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
851 ret = fusb302_pd_set_auto_goodcrc(chip, on);
853 fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
854 on ? "on" : "off", ret);
857 ret = fusb302_pd_set_interrupts(chip, on);
859 fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
860 on ? "on" : "off", ret);
863 fusb302_log(chip, "pd := %s", on ? "on" : "off");
865 mutex_unlock(&chip->lock);
870 static const char * const typec_role_name[] = {
871 [TYPEC_SINK] = "Sink",
872 [TYPEC_SOURCE] = "Source",
875 static const char * const typec_data_role_name[] = {
876 [TYPEC_DEVICE] = "Device",
877 [TYPEC_HOST] = "Host",
880 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
881 enum typec_role pwr, enum typec_data_role data)
883 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
886 u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
887 FUSB_REG_SWITCHES1_DATAROLE;
888 u8 switches1_data = 0x00;
890 mutex_lock(&chip->lock);
891 if (pwr == TYPEC_SOURCE)
892 switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
893 if (data == TYPEC_HOST)
894 switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
895 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
896 switches1_mask, switches1_data);
898 fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
899 typec_role_name[pwr], typec_data_role_name[data],
903 fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
904 typec_data_role_name[data]);
906 mutex_unlock(&chip->lock);
911 static int tcpm_start_toggling(struct tcpc_dev *dev,
912 enum typec_port_type port_type,
913 enum typec_cc_status cc)
915 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
917 enum toggling_mode mode = TOGGLING_MODE_OFF;
922 mode = TOGGLING_MODE_SRC;
925 mode = TOGGLING_MODE_SNK;
928 mode = TOGGLING_MODE_DRP;
932 mutex_lock(&chip->lock);
933 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
935 fusb302_log(chip, "unable to set src current %s, ret=%d",
936 typec_cc_status_name[cc], ret);
939 ret = fusb302_set_toggling(chip, mode);
942 "unable to start drp toggling, ret=%d", ret);
945 fusb302_log(chip, "start drp toggling");
947 mutex_unlock(&chip->lock);
952 static int fusb302_pd_send_message(struct fusb302_chip *chip,
953 const struct pd_message *msg)
961 buf[pos++] = FUSB302_TKN_SYNC1;
962 buf[pos++] = FUSB302_TKN_SYNC1;
963 buf[pos++] = FUSB302_TKN_SYNC1;
964 buf[pos++] = FUSB302_TKN_SYNC2;
966 len = pd_header_cnt_le(msg->header) * 4;
967 /* plug 2 for header */
971 "PD message too long %d (incl. header)", len);
974 /* packsym tells the FUSB302 chip that the next X bytes are payload */
975 buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
976 memcpy(&buf[pos], &msg->header, sizeof(msg->header));
977 pos += sizeof(msg->header);
980 memcpy(&buf[pos], msg->payload, len);
984 buf[pos++] = FUSB302_TKN_JAMCRC;
986 buf[pos++] = FUSB302_TKN_EOP;
987 /* turn tx off after sending message */
988 buf[pos++] = FUSB302_TKN_TXOFF;
989 /* start transmission */
990 buf[pos++] = FUSB302_TKN_TXON;
992 ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
995 fusb302_log(chip, "sending PD message header: %x", msg->header);
996 fusb302_log(chip, "sending PD message len: %d", len);
1001 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1003 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1004 FUSB_REG_CONTROL3_SEND_HARDRESET);
1007 static const char * const transmit_type_name[] = {
1008 [TCPC_TX_SOP] = "SOP",
1009 [TCPC_TX_SOP_PRIME] = "SOP'",
1010 [TCPC_TX_SOP_PRIME_PRIME] = "SOP''",
1011 [TCPC_TX_SOP_DEBUG_PRIME] = "DEBUG'",
1012 [TCPC_TX_SOP_DEBUG_PRIME_PRIME] = "DEBUG''",
1013 [TCPC_TX_HARD_RESET] = "HARD_RESET",
1014 [TCPC_TX_CABLE_RESET] = "CABLE_RESET",
1015 [TCPC_TX_BIST_MODE_2] = "BIST_MODE_2",
1018 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1019 const struct pd_message *msg, unsigned int negotiated_rev)
1021 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1025 mutex_lock(&chip->lock);
1028 /* nRetryCount 3 in P2.0 spec, whereas 2 in PD3.0 spec */
1029 ret = fusb302_enable_tx_auto_retries(chip, negotiated_rev > PD_REV20 ?
1030 FUSB_REG_CONTROL3_N_RETRIES_2 :
1031 FUSB_REG_CONTROL3_N_RETRIES_3);
1033 fusb302_log(chip, "Cannot update retry count ret=%d", ret);
1035 ret = fusb302_pd_send_message(chip, msg);
1038 "cannot send PD message, ret=%d", ret);
1040 case TCPC_TX_HARD_RESET:
1041 ret = fusb302_pd_send_hardreset(chip);
1044 "cannot send hardreset, ret=%d", ret);
1047 fusb302_log(chip, "type %s not supported",
1048 transmit_type_name[type]);
1051 mutex_unlock(&chip->lock);
1056 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1058 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1059 return TYPEC_CC_RP_3_0;
1060 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1061 return TYPEC_CC_RP_1_5;
1062 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1063 return TYPEC_CC_RP_DEF;
1064 return TYPEC_CC_OPEN;
1067 static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1069 struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1070 bc_lvl_handler.work);
1074 enum typec_cc_status cc_status;
1076 mutex_lock(&chip->lock);
1077 if (!chip->intr_bc_lvl) {
1078 fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1081 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1084 fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1085 if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1086 fusb302_log(chip, "CC activities detected, delay handling");
1087 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1088 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1091 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1092 cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1093 if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1094 if (chip->cc1 != cc_status) {
1095 fusb302_log(chip, "cc1: %s -> %s",
1096 typec_cc_status_name[chip->cc1],
1097 typec_cc_status_name[cc_status]);
1098 chip->cc1 = cc_status;
1099 tcpm_cc_change(chip->tcpm_port);
1102 if (chip->cc2 != cc_status) {
1103 fusb302_log(chip, "cc2: %s -> %s",
1104 typec_cc_status_name[chip->cc2],
1105 typec_cc_status_name[cc_status]);
1106 chip->cc2 = cc_status;
1107 tcpm_cc_change(chip->tcpm_port);
1112 mutex_unlock(&chip->lock);
1115 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1117 fusb302_tcpc_dev->init = tcpm_init;
1118 fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1119 fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
1120 fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1121 fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1122 fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1123 fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1124 fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1125 fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1126 fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1127 fusb302_tcpc_dev->start_toggling = tcpm_start_toggling;
1128 fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1131 static const char * const cc_polarity_name[] = {
1132 [TYPEC_POLARITY_CC1] = "Polarity_CC1",
1133 [TYPEC_POLARITY_CC2] = "Polarity_CC2",
1136 static int fusb302_set_cc_polarity_and_pull(struct fusb302_chip *chip,
1137 enum typec_cc_polarity cc_polarity,
1138 bool pull_up, bool pull_down)
1141 u8 switches0_data = 0x00;
1142 u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
1143 FUSB_REG_SWITCHES1_TXCC2_EN;
1144 u8 switches1_data = 0x00;
1147 switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
1148 FUSB_REG_SWITCHES0_CC2_PD_EN;
1150 if (cc_polarity == TYPEC_POLARITY_CC1) {
1151 switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC1;
1153 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1155 switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1156 switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1158 switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC2;
1160 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1162 switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1163 switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1165 ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1168 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1169 switches1_mask, switches1_data);
1172 chip->cc_polarity = cc_polarity;
1177 static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1183 enum typec_cc_polarity cc_polarity;
1184 enum typec_cc_status cc_status_active, cc1, cc2;
1186 /* set polarity and pull_up, pull_down */
1187 cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1188 TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1189 ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, false, true);
1191 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1192 cc_polarity_name[cc_polarity], ret);
1195 /* fusb302_set_cc_polarity() has set the correct measure block */
1196 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1199 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1200 cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1201 /* restart toggling if the cc status on the active line is OPEN */
1202 if (cc_status_active == TYPEC_CC_OPEN) {
1203 fusb302_log(chip, "restart toggling as CC_OPEN detected");
1204 ret = fusb302_set_toggling(chip, chip->toggling_mode);
1207 /* update tcpm with the new cc value */
1208 cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1209 cc_status_active : TYPEC_CC_OPEN;
1210 cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1211 cc_status_active : TYPEC_CC_OPEN;
1212 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1215 tcpm_cc_change(chip->tcpm_port);
1217 /* turn off toggling */
1218 ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1221 "cannot set toggling mode off, ret=%d", ret);
1224 /* unmask bc_lvl interrupt */
1225 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1228 "cannot unmask bc_lcl interrupt, ret=%d", ret);
1231 chip->intr_bc_lvl = true;
1232 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1233 typec_cc_status_name[cc1],
1234 typec_cc_status_name[cc2]);
1239 /* On error returns < 0, otherwise a typec_cc_status value */
1240 static int fusb302_get_src_cc_status(struct fusb302_chip *chip,
1241 enum typec_cc_polarity cc_polarity,
1242 enum typec_cc_status *cc)
1244 u8 ra_mda = ra_mda_value[chip->src_current_status];
1245 u8 rd_mda = rd_mda_value[chip->src_current_status];
1246 u8 switches0_data, status0;
1249 /* Step 1: Set switches so that we measure the right CC pin */
1250 switches0_data = (cc_polarity == TYPEC_POLARITY_CC1) ?
1251 FUSB_REG_SWITCHES0_CC1_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC1 :
1252 FUSB_REG_SWITCHES0_CC2_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC2;
1253 ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1257 fusb302_i2c_read(chip, FUSB_REG_SWITCHES0, &status0);
1258 fusb302_log(chip, "get_src_cc_status switches: 0x%0x", status0);
1260 /* Step 2: Set compararator volt to differentiate between Open and Rd */
1261 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1265 usleep_range(50, 100);
1266 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1270 fusb302_log(chip, "get_src_cc_status rd_mda status0: 0x%0x", status0);
1271 if (status0 & FUSB_REG_STATUS0_COMP) {
1272 *cc = TYPEC_CC_OPEN;
1276 /* Step 3: Set compararator input to differentiate between Rd and Ra. */
1277 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1281 usleep_range(50, 100);
1282 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1286 fusb302_log(chip, "get_src_cc_status ra_mda status0: 0x%0x", status0);
1287 if (status0 & FUSB_REG_STATUS0_COMP)
1295 static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1299 * - set polarity (measure cc, vconn, tx)
1300 * - set pull_up, pull_down
1301 * - set cc1, cc2, and update to tcpm_port
1302 * - set I_COMP interrupt on
1305 u8 rd_mda = rd_mda_value[chip->src_current_status];
1306 enum toggling_mode toggling_mode = chip->toggling_mode;
1307 enum typec_cc_polarity cc_polarity;
1308 enum typec_cc_status cc1, cc2;
1311 * The toggle-engine will stop in a src state if it sees either Ra or
1312 * Rd. Determine the status for both CC pins, starting with the one
1313 * where toggling stopped, as that is where the switches point now.
1315 if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1316 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1318 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1321 /* we must turn off toggling before we can measure the other pin */
1322 ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1324 fusb302_log(chip, "cannot set toggling mode off, ret=%d", ret);
1327 /* get the status of the other pin */
1328 if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1329 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1331 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1335 /* determine polarity based on the status of both pins */
1336 if (cc1 == TYPEC_CC_RD &&
1337 (cc2 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_RA)) {
1338 cc_polarity = TYPEC_POLARITY_CC1;
1339 } else if (cc2 == TYPEC_CC_RD &&
1340 (cc1 == TYPEC_CC_OPEN || cc1 == TYPEC_CC_RA)) {
1341 cc_polarity = TYPEC_POLARITY_CC2;
1343 fusb302_log(chip, "unexpected CC status cc1=%s, cc2=%s, restarting toggling",
1344 typec_cc_status_name[cc1],
1345 typec_cc_status_name[cc2]);
1346 return fusb302_set_toggling(chip, toggling_mode);
1348 /* set polarity and pull_up, pull_down */
1349 ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, true, false);
1351 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1352 cc_polarity_name[cc_polarity], ret);
1355 /* update tcpm with the new cc value */
1356 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1359 tcpm_cc_change(chip->tcpm_port);
1361 /* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1362 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1365 /* unmask comp_chng interrupt */
1366 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1367 FUSB_REG_MASK_COMP_CHNG);
1370 "cannot unmask comp_chng interrupt, ret=%d", ret);
1373 chip->intr_comp_chng = true;
1374 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1375 typec_cc_status_name[cc1],
1376 typec_cc_status_name[cc2]);
1381 static int fusb302_handle_togdone(struct fusb302_chip *chip)
1387 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1390 togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1391 FUSB_REG_STATUS1A_TOGSS_MASK;
1392 switch (togdone_result) {
1393 case FUSB_REG_STATUS1A_TOGSS_SNK1:
1394 case FUSB_REG_STATUS1A_TOGSS_SNK2:
1395 return fusb302_handle_togdone_snk(chip, togdone_result);
1396 case FUSB_REG_STATUS1A_TOGSS_SRC1:
1397 case FUSB_REG_STATUS1A_TOGSS_SRC2:
1398 return fusb302_handle_togdone_src(chip, togdone_result);
1399 case FUSB_REG_STATUS1A_TOGSS_AA:
1400 /* doesn't support */
1401 fusb302_log(chip, "AudioAccessory not supported");
1402 fusb302_set_toggling(chip, chip->toggling_mode);
1405 fusb302_log(chip, "TOGDONE with an invalid state: %d",
1407 fusb302_set_toggling(chip, chip->toggling_mode);
1413 static int fusb302_pd_reset(struct fusb302_chip *chip)
1415 return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1416 FUSB_REG_RESET_PD_RESET);
1419 static int fusb302_pd_read_message(struct fusb302_chip *chip,
1420 struct pd_message *msg)
1427 /* first SOP token */
1428 ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1431 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1432 (u8 *)&msg->header);
1435 len = pd_header_cnt_le(msg->header) * 4;
1436 /* add 4 to length to include the CRC */
1437 if (len > PD_MAX_PAYLOAD * 4) {
1438 fusb302_log(chip, "PD message too long %d", len);
1442 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1443 (u8 *)msg->payload);
1447 /* another 4 bytes to read CRC out */
1448 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1451 fusb302_log(chip, "PD message header: %x", msg->header);
1452 fusb302_log(chip, "PD message len: %d", len);
1455 * Check if we've read off a GoodCRC message. If so then indicate to
1456 * TCPM that the previous transmission has completed. Otherwise we pass
1457 * the received message over to TCPM for processing.
1459 * We make this check here instead of basing the reporting decision on
1460 * the IRQ event type, as it's possible for the chip to report the
1461 * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
1462 * to check the message type to ensure correct reporting to TCPM.
1464 if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
1465 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1467 tcpm_pd_receive(chip->tcpm_port, msg);
1472 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1474 struct fusb302_chip *chip = dev_id;
1475 unsigned long flags;
1477 /* Disable our level triggered IRQ until our irq_work has cleared it */
1478 disable_irq_nosync(chip->gpio_int_n_irq);
1480 spin_lock_irqsave(&chip->irq_lock, flags);
1481 if (chip->irq_suspended)
1482 chip->irq_while_suspended = true;
1484 schedule_work(&chip->irq_work);
1485 spin_unlock_irqrestore(&chip->irq_lock, flags);
1490 static void fusb302_irq_work(struct work_struct *work)
1492 struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1503 bool intr_comp_chng;
1504 struct pd_message pd_msg;
1506 mutex_lock(&chip->lock);
1507 /* grab a snapshot of intr flags */
1508 intr_togdone = chip->intr_togdone;
1509 intr_bc_lvl = chip->intr_bc_lvl;
1510 intr_comp_chng = chip->intr_comp_chng;
1512 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1515 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1518 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1521 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1525 "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1526 interrupt, interrupta, interruptb, status0);
1528 if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1529 vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1530 fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1531 vbus_present ? "On" : "Off");
1532 if (vbus_present != chip->vbus_present) {
1533 chip->vbus_present = vbus_present;
1534 tcpm_vbus_change(chip->tcpm_port);
1538 if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1539 fusb302_log(chip, "IRQ: TOGDONE");
1540 ret = fusb302_handle_togdone(chip);
1543 "handle togdone error, ret=%d", ret);
1548 if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1549 fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1551 * as BC_LVL interrupt can be affected by PD activity,
1552 * apply delay to for the handler to wait for the PD
1553 * signaling to finish.
1555 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1556 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1559 if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1560 comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1561 fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1562 comp_result ? "true" : "false");
1564 /* cc level > Rd_threshold, detach */
1565 chip->cc1 = TYPEC_CC_OPEN;
1566 chip->cc2 = TYPEC_CC_OPEN;
1567 tcpm_cc_change(chip->tcpm_port);
1571 if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1572 fusb302_log(chip, "IRQ: PD collision");
1573 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1576 if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1577 fusb302_log(chip, "IRQ: PD retry failed");
1578 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1581 if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1582 fusb302_log(chip, "IRQ: PD hardreset sent");
1583 ret = fusb302_pd_reset(chip);
1585 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1588 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1591 if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1592 fusb302_log(chip, "IRQ: PD tx success");
1593 ret = fusb302_pd_read_message(chip, &pd_msg);
1596 "cannot read in PD message, ret=%d", ret);
1601 if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1602 fusb302_log(chip, "IRQ: PD received hardreset");
1603 ret = fusb302_pd_reset(chip);
1605 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1608 tcpm_pd_hard_reset(chip->tcpm_port);
1611 if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1612 fusb302_log(chip, "IRQ: PD sent good CRC");
1613 ret = fusb302_pd_read_message(chip, &pd_msg);
1616 "cannot read in PD message, ret=%d", ret);
1621 mutex_unlock(&chip->lock);
1622 enable_irq(chip->gpio_int_n_irq);
1625 static int init_gpio(struct fusb302_chip *chip)
1627 struct device *dev = chip->dev;
1630 chip->gpio_int_n = devm_gpiod_get(dev, "fcs,int_n", GPIOD_IN);
1631 if (IS_ERR(chip->gpio_int_n)) {
1632 dev_err(dev, "failed to request gpio_int_n\n");
1633 return PTR_ERR(chip->gpio_int_n);
1635 ret = gpiod_to_irq(chip->gpio_int_n);
1638 "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1641 chip->gpio_int_n_irq = ret;
1645 #define PDO_FIXED_FLAGS \
1646 (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1648 static const u32 src_pdo[] = {
1649 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1652 static const u32 snk_pdo[] = {
1653 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1656 static const struct property_entry port_props[] = {
1657 PROPERTY_ENTRY_STRING("data-role", "dual"),
1658 PROPERTY_ENTRY_STRING("power-role", "dual"),
1659 PROPERTY_ENTRY_STRING("try-power-role", "sink"),
1660 PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
1661 PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
1662 PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
1666 static struct fwnode_handle *fusb302_fwnode_get(struct device *dev)
1668 struct fwnode_handle *fwnode;
1670 fwnode = device_get_named_child_node(dev, "connector");
1672 fwnode = fwnode_create_software_node(port_props, NULL);
1677 static int fusb302_probe(struct i2c_client *client,
1678 const struct i2c_device_id *id)
1680 struct fusb302_chip *chip;
1681 struct i2c_adapter *adapter = client->adapter;
1682 struct device *dev = &client->dev;
1686 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1687 dev_err(&client->dev,
1688 "I2C/SMBus block functionality not supported!\n");
1691 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1695 chip->i2c_client = client;
1696 chip->dev = &client->dev;
1697 mutex_init(&chip->lock);
1700 * Devicetree platforms should get extcon via phandle (not yet
1701 * supported). On ACPI platforms, we get the name from a device prop.
1702 * This device prop is for kernel internal use only and is expected
1703 * to be set by the platform code which also registers the i2c client
1706 if (device_property_read_string(dev, "linux,extcon-name", &name) == 0) {
1707 chip->extcon = extcon_get_extcon_dev(name);
1709 return -EPROBE_DEFER;
1712 chip->vbus = devm_regulator_get(chip->dev, "vbus");
1713 if (IS_ERR(chip->vbus))
1714 return PTR_ERR(chip->vbus);
1716 chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1720 spin_lock_init(&chip->irq_lock);
1721 INIT_WORK(&chip->irq_work, fusb302_irq_work);
1722 INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1723 init_tcpc_dev(&chip->tcpc_dev);
1724 fusb302_debugfs_init(chip);
1727 chip->gpio_int_n_irq = client->irq;
1729 ret = init_gpio(chip);
1731 goto destroy_workqueue;
1734 chip->tcpc_dev.fwnode = fusb302_fwnode_get(dev);
1735 if (IS_ERR(chip->tcpc_dev.fwnode)) {
1736 ret = PTR_ERR(chip->tcpc_dev.fwnode);
1737 goto destroy_workqueue;
1740 chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1741 if (IS_ERR(chip->tcpm_port)) {
1742 fwnode_handle_put(chip->tcpc_dev.fwnode);
1743 ret = PTR_ERR(chip->tcpm_port);
1744 if (ret != -EPROBE_DEFER)
1745 dev_err(dev, "cannot register tcpm port, ret=%d", ret);
1746 goto destroy_workqueue;
1749 ret = request_irq(chip->gpio_int_n_irq, fusb302_irq_intn,
1750 IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1751 "fsc_interrupt_int_n", chip);
1753 dev_err(dev, "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1754 goto tcpm_unregister_port;
1756 enable_irq_wake(chip->gpio_int_n_irq);
1757 i2c_set_clientdata(client, chip);
1761 tcpm_unregister_port:
1762 tcpm_unregister_port(chip->tcpm_port);
1763 fwnode_handle_put(chip->tcpc_dev.fwnode);
1765 fusb302_debugfs_exit(chip);
1766 destroy_workqueue(chip->wq);
1771 static int fusb302_remove(struct i2c_client *client)
1773 struct fusb302_chip *chip = i2c_get_clientdata(client);
1775 disable_irq_wake(chip->gpio_int_n_irq);
1776 free_irq(chip->gpio_int_n_irq, chip);
1777 cancel_work_sync(&chip->irq_work);
1778 cancel_delayed_work_sync(&chip->bc_lvl_handler);
1779 tcpm_unregister_port(chip->tcpm_port);
1780 fwnode_handle_put(chip->tcpc_dev.fwnode);
1781 destroy_workqueue(chip->wq);
1782 fusb302_debugfs_exit(chip);
1787 static int fusb302_pm_suspend(struct device *dev)
1789 struct fusb302_chip *chip = dev->driver_data;
1790 unsigned long flags;
1792 spin_lock_irqsave(&chip->irq_lock, flags);
1793 chip->irq_suspended = true;
1794 spin_unlock_irqrestore(&chip->irq_lock, flags);
1796 /* Make sure any pending irq_work is finished before the bus suspends */
1797 flush_work(&chip->irq_work);
1801 static int fusb302_pm_resume(struct device *dev)
1803 struct fusb302_chip *chip = dev->driver_data;
1804 unsigned long flags;
1806 spin_lock_irqsave(&chip->irq_lock, flags);
1807 if (chip->irq_while_suspended) {
1808 schedule_work(&chip->irq_work);
1809 chip->irq_while_suspended = false;
1811 chip->irq_suspended = false;
1812 spin_unlock_irqrestore(&chip->irq_lock, flags);
1817 static const struct of_device_id fusb302_dt_match[] = {
1818 {.compatible = "fcs,fusb302"},
1821 MODULE_DEVICE_TABLE(of, fusb302_dt_match);
1823 static const struct i2c_device_id fusb302_i2c_device_id[] = {
1824 {"typec_fusb302", 0},
1827 MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
1829 static const struct dev_pm_ops fusb302_pm_ops = {
1830 .suspend = fusb302_pm_suspend,
1831 .resume = fusb302_pm_resume,
1834 static struct i2c_driver fusb302_driver = {
1836 .name = "typec_fusb302",
1837 .pm = &fusb302_pm_ops,
1838 .of_match_table = of_match_ptr(fusb302_dt_match),
1840 .probe = fusb302_probe,
1841 .remove = fusb302_remove,
1842 .id_table = fusb302_i2c_device_id,
1844 module_i2c_driver(fusb302_driver);
1847 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1848 MODULE_LICENSE("GPL");