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.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of_device.h>
19 #include <linux/of_gpio.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/proc_fs.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/sched/clock.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
28 #include <linux/usb/typec.h>
29 #include <linux/usb/tcpm.h>
30 #include <linux/usb/pd.h>
31 #include <linux/workqueue.h>
33 #include "fusb302_reg.h"
36 * When the device is SNK, BC_LVL interrupt is used to monitor cc pins
37 * for the current capability offered by the SRC. As FUSB302 chip fires
38 * the BC_LVL interrupt on PD signalings, cc lvl should be handled after
39 * a delay to avoid measuring on PD activities. The delay is slightly
40 * longer than PD_T_PD_DEBPUNCE (10-20ms).
42 #define T_BC_LVL_DEBOUNCE_DELAY_MS 30
51 static const char * const toggling_mode_name[] = {
52 [TOGGLINE_MODE_OFF] = "toggling_OFF",
53 [TOGGLING_MODE_DRP] = "toggling_DRP",
54 [TOGGLING_MODE_SNK] = "toggling_SNK",
55 [TOGGLING_MODE_SRC] = "toggling_SRC",
58 enum src_current_status {
64 static const u8 ra_mda_value[] = {
65 [SRC_CURRENT_DEFAULT] = 4, /* 210mV */
66 [SRC_CURRENT_MEDIUM] = 9, /* 420mV */
67 [SRC_CURRENT_HIGH] = 18, /* 798mV */
70 static const u8 rd_mda_value[] = {
71 [SRC_CURRENT_DEFAULT] = 38, /* 1638mV */
72 [SRC_CURRENT_MEDIUM] = 38, /* 1638mV */
73 [SRC_CURRENT_HIGH] = 61, /* 2604mV */
76 #define LOG_BUFFER_ENTRIES 1024
77 #define LOG_BUFFER_ENTRY_SIZE 128
81 struct i2c_client *i2c_client;
82 struct tcpm_port *tcpm_port;
83 struct tcpc_dev tcpc_dev;
84 struct tcpc_config tcpc_config;
86 struct regulator *vbus;
90 struct extcon_dev *extcon;
92 struct workqueue_struct *wq;
93 struct delayed_work bc_lvl_handler;
98 /* lock for sharing chip states */
102 enum toggling_mode toggling_mode;
103 enum src_current_status src_current_status;
114 enum typec_cc_polarity cc_polarity;
115 enum typec_cc_status cc1;
116 enum typec_cc_status cc2;
117 u32 snk_pdo[PDO_MAX_OBJECTS];
119 #ifdef CONFIG_DEBUG_FS
120 struct dentry *dentry;
121 /* lock for log buffer access */
122 struct mutex logbuffer_lock;
125 u8 *logbuffer[LOG_BUFFER_ENTRIES];
133 #ifdef CONFIG_DEBUG_FS
135 static bool fusb302_log_full(struct fusb302_chip *chip)
137 return chip->logbuffer_tail ==
138 (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
141 static void _fusb302_log(struct fusb302_chip *chip, const char *fmt,
144 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
145 u64 ts_nsec = local_clock();
146 unsigned long rem_nsec;
148 if (!chip->logbuffer[chip->logbuffer_head]) {
149 chip->logbuffer[chip->logbuffer_head] =
150 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
151 if (!chip->logbuffer[chip->logbuffer_head])
155 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
157 mutex_lock(&chip->logbuffer_lock);
159 if (fusb302_log_full(chip)) {
160 chip->logbuffer_head = max(chip->logbuffer_head - 1, 0);
161 strlcpy(tmpbuffer, "overflow", sizeof(tmpbuffer));
164 if (chip->logbuffer_head < 0 ||
165 chip->logbuffer_head >= LOG_BUFFER_ENTRIES) {
167 "Bad log buffer index %d\n", chip->logbuffer_head);
171 if (!chip->logbuffer[chip->logbuffer_head]) {
173 "Log buffer index %d is NULL\n", chip->logbuffer_head);
177 rem_nsec = do_div(ts_nsec, 1000000000);
178 scnprintf(chip->logbuffer[chip->logbuffer_head],
179 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
180 (unsigned long)ts_nsec, rem_nsec / 1000,
182 chip->logbuffer_head = (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
185 mutex_unlock(&chip->logbuffer_lock);
188 static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
193 _fusb302_log(chip, fmt, args);
197 static int fusb302_debug_show(struct seq_file *s, void *v)
199 struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
202 mutex_lock(&chip->logbuffer_lock);
203 tail = chip->logbuffer_tail;
204 while (tail != chip->logbuffer_head) {
205 seq_printf(s, "%s\n", chip->logbuffer[tail]);
206 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
208 if (!seq_has_overflowed(s))
209 chip->logbuffer_tail = tail;
210 mutex_unlock(&chip->logbuffer_lock);
214 DEFINE_SHOW_ATTRIBUTE(fusb302_debug);
216 static struct dentry *rootdir;
218 static void fusb302_debugfs_init(struct fusb302_chip *chip)
220 mutex_init(&chip->logbuffer_lock);
222 rootdir = debugfs_create_dir("fusb302", NULL);
224 chip->dentry = debugfs_create_file(dev_name(chip->dev),
225 S_IFREG | 0444, rootdir,
226 chip, &fusb302_debug_fops);
229 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
231 debugfs_remove(chip->dentry);
232 debugfs_remove(rootdir);
237 static void fusb302_log(const struct fusb302_chip *chip,
238 const char *fmt, ...) { }
239 static void fusb302_debugfs_init(const struct fusb302_chip *chip) { }
240 static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
244 #define FUSB302_RESUME_RETRY 10
245 #define FUSB302_RESUME_RETRY_SLEEP 50
247 static bool fusb302_is_suspended(struct fusb302_chip *chip)
251 for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) {
252 if (atomic_read(&chip->pm_suspend)) {
253 dev_err(chip->dev, "i2c: pm suspend, retry %d/%d\n",
254 retry_cnt + 1, FUSB302_RESUME_RETRY);
255 msleep(FUSB302_RESUME_RETRY_SLEEP);
264 static int fusb302_i2c_write(struct fusb302_chip *chip,
269 atomic_set(&chip->i2c_busy, 1);
271 if (fusb302_is_suspended(chip)) {
272 atomic_set(&chip->i2c_busy, 0);
276 ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
278 fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
280 atomic_set(&chip->i2c_busy, 0);
285 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
286 u8 length, const u8 *data)
292 atomic_set(&chip->i2c_busy, 1);
294 if (fusb302_is_suspended(chip)) {
295 atomic_set(&chip->i2c_busy, 0);
299 ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
302 fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
303 address, length, ret);
304 atomic_set(&chip->i2c_busy, 0);
309 static int fusb302_i2c_read(struct fusb302_chip *chip,
310 u8 address, u8 *data)
314 atomic_set(&chip->i2c_busy, 1);
316 if (fusb302_is_suspended(chip)) {
317 atomic_set(&chip->i2c_busy, 0);
321 ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
324 fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
325 atomic_set(&chip->i2c_busy, 0);
330 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
337 atomic_set(&chip->i2c_busy, 1);
339 if (fusb302_is_suspended(chip)) {
340 atomic_set(&chip->i2c_busy, 0);
344 ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
347 fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
348 address, length, ret);
352 fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
353 ret, length, address);
358 atomic_set(&chip->i2c_busy, 0);
363 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
369 ret = fusb302_i2c_read(chip, address, &data);
374 ret = fusb302_i2c_write(chip, address, data);
381 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
384 return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
387 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
390 return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
393 static int fusb302_sw_reset(struct fusb302_chip *chip)
397 ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
398 FUSB_REG_RESET_SW_RESET);
400 fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
402 fusb302_log(chip, "sw reset");
407 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip)
411 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
412 FUSB_REG_CONTROL3_N_RETRIES_3 |
413 FUSB_REG_CONTROL3_AUTO_RETRY);
419 * initialize interrupt on the chip
420 * - unmasked interrupt: VBUS_OK
422 static int fusb302_init_interrupt(struct fusb302_chip *chip)
426 ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
427 0xFF & ~FUSB_REG_MASK_VBUSOK);
430 ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
433 ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
436 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
437 FUSB_REG_CONTROL0_INT_MASK);
444 static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
448 ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
453 static int tcpm_init(struct tcpc_dev *dev)
455 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
460 ret = fusb302_sw_reset(chip);
463 ret = fusb302_enable_tx_auto_retries(chip);
466 ret = fusb302_init_interrupt(chip);
469 ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
472 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
475 chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
476 ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
479 fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
484 static int tcpm_get_vbus(struct tcpc_dev *dev)
486 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
490 mutex_lock(&chip->lock);
491 ret = chip->vbus_present ? 1 : 0;
492 mutex_unlock(&chip->lock);
497 static int tcpm_get_current_limit(struct tcpc_dev *dev)
499 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
501 int current_limit = 0;
502 unsigned long timeout;
508 * USB2 Charger detection may still be in progress when we get here,
509 * this can take upto 600ms, wait 800ms max.
511 timeout = jiffies + msecs_to_jiffies(800);
513 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_SDP) == 1)
516 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_CDP) == 1 ||
517 extcon_get_state(chip->extcon, EXTCON_CHG_USB_ACA) == 1)
518 current_limit = 1500;
520 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_DCP) == 1)
521 current_limit = 2000;
524 } while (current_limit == 0 && time_before(jiffies, timeout));
526 return current_limit;
529 static int fusb302_set_cc_pull(struct fusb302_chip *chip,
530 bool pull_up, bool pull_down)
534 u8 mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
535 FUSB_REG_SWITCHES0_CC2_PU_EN |
536 FUSB_REG_SWITCHES0_CC1_PD_EN |
537 FUSB_REG_SWITCHES0_CC2_PD_EN;
540 data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
541 FUSB_REG_SWITCHES0_CC1_PU_EN :
542 FUSB_REG_SWITCHES0_CC2_PU_EN;
544 data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
545 FUSB_REG_SWITCHES0_CC2_PD_EN;
546 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
550 chip->pull_up = pull_up;
555 static int fusb302_set_src_current(struct fusb302_chip *chip,
556 enum src_current_status status)
560 chip->src_current_status = status;
562 case SRC_CURRENT_DEFAULT:
563 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
564 FUSB_REG_CONTROL0_HOST_CUR_MASK,
565 FUSB_REG_CONTROL0_HOST_CUR_DEF);
567 case SRC_CURRENT_MEDIUM:
568 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
569 FUSB_REG_CONTROL0_HOST_CUR_MASK,
570 FUSB_REG_CONTROL0_HOST_CUR_MED);
572 case SRC_CURRENT_HIGH:
573 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
574 FUSB_REG_CONTROL0_HOST_CUR_MASK,
575 FUSB_REG_CONTROL0_HOST_CUR_HIGH);
584 static int fusb302_set_toggling(struct fusb302_chip *chip,
585 enum toggling_mode mode)
589 /* first disable toggling */
590 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
591 FUSB_REG_CONTROL2_TOGGLE);
594 /* mask interrupts for SRC or SNK */
595 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
596 FUSB_REG_MASK_BC_LVL |
597 FUSB_REG_MASK_COMP_CHNG);
600 chip->intr_bc_lvl = false;
601 chip->intr_comp_chng = false;
602 /* configure toggling mode: none/snk/src/drp */
604 case TOGGLINE_MODE_OFF:
605 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
606 FUSB_REG_CONTROL2_MODE_MASK,
607 FUSB_REG_CONTROL2_MODE_NONE);
611 case TOGGLING_MODE_SNK:
612 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
613 FUSB_REG_CONTROL2_MODE_MASK,
614 FUSB_REG_CONTROL2_MODE_UFP);
618 case TOGGLING_MODE_SRC:
619 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
620 FUSB_REG_CONTROL2_MODE_MASK,
621 FUSB_REG_CONTROL2_MODE_DFP);
625 case TOGGLING_MODE_DRP:
626 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
627 FUSB_REG_CONTROL2_MODE_MASK,
628 FUSB_REG_CONTROL2_MODE_DRP);
636 if (mode == TOGGLINE_MODE_OFF) {
637 /* mask TOGDONE interrupt */
638 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
639 FUSB_REG_MASKA_TOGDONE);
642 chip->intr_togdone = false;
644 /* Datasheet says vconn MUST be off when toggling */
645 WARN(chip->vconn_on, "Vconn is on during toggle start");
646 /* unmask TOGDONE interrupt */
647 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
648 FUSB_REG_MASKA_TOGDONE);
651 chip->intr_togdone = true;
653 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
654 FUSB_REG_CONTROL2_TOGGLE);
657 /* during toggling, consider cc as Open */
658 chip->cc1 = TYPEC_CC_OPEN;
659 chip->cc2 = TYPEC_CC_OPEN;
661 chip->toggling_mode = mode;
666 static const char * const typec_cc_status_name[] = {
667 [TYPEC_CC_OPEN] = "Open",
668 [TYPEC_CC_RA] = "Ra",
669 [TYPEC_CC_RD] = "Rd",
670 [TYPEC_CC_RP_DEF] = "Rp-def",
671 [TYPEC_CC_RP_1_5] = "Rp-1.5",
672 [TYPEC_CC_RP_3_0] = "Rp-3.0",
675 static const enum src_current_status cc_src_current[] = {
676 [TYPEC_CC_OPEN] = SRC_CURRENT_DEFAULT,
677 [TYPEC_CC_RA] = SRC_CURRENT_DEFAULT,
678 [TYPEC_CC_RD] = SRC_CURRENT_DEFAULT,
679 [TYPEC_CC_RP_DEF] = SRC_CURRENT_DEFAULT,
680 [TYPEC_CC_RP_1_5] = SRC_CURRENT_MEDIUM,
681 [TYPEC_CC_RP_3_0] = SRC_CURRENT_HIGH,
684 static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
686 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
689 bool pull_up, pull_down;
692 mutex_lock(&chip->lock);
702 case TYPEC_CC_RP_DEF:
703 case TYPEC_CC_RP_1_5:
704 case TYPEC_CC_RP_3_0:
709 fusb302_log(chip, "unsupported cc value %s",
710 typec_cc_status_name[cc]);
714 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
716 fusb302_log(chip, "cannot stop toggling, ret=%d", ret);
719 ret = fusb302_set_cc_pull(chip, pull_up, pull_down);
722 "cannot set cc pulling up %s, down %s, ret = %d",
723 pull_up ? "True" : "False",
724 pull_down ? "True" : "False",
728 /* reset the cc status */
729 chip->cc1 = TYPEC_CC_OPEN;
730 chip->cc2 = TYPEC_CC_OPEN;
731 /* adjust current for SRC */
733 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
735 fusb302_log(chip, "cannot set src current %s, ret=%d",
736 typec_cc_status_name[cc], ret);
740 /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
742 rd_mda = rd_mda_value[cc_src_current[cc]];
743 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
746 "cannot set SRC measure value, ret=%d",
750 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
751 FUSB_REG_MASK_BC_LVL |
752 FUSB_REG_MASK_COMP_CHNG,
753 FUSB_REG_MASK_COMP_CHNG);
755 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
759 chip->intr_bc_lvl = false;
760 chip->intr_comp_chng = true;
763 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
764 FUSB_REG_MASK_BC_LVL |
765 FUSB_REG_MASK_COMP_CHNG,
766 FUSB_REG_MASK_BC_LVL);
768 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
772 chip->intr_bc_lvl = true;
773 chip->intr_comp_chng = false;
775 fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
777 mutex_unlock(&chip->lock);
782 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
783 enum typec_cc_status *cc2)
785 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
788 mutex_lock(&chip->lock);
791 fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
792 typec_cc_status_name[*cc2]);
793 mutex_unlock(&chip->lock);
798 static int tcpm_set_polarity(struct tcpc_dev *dev,
799 enum typec_cc_polarity polarity)
804 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
806 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
809 u8 switches0_data = 0x00;
810 u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
811 FUSB_REG_SWITCHES0_VCONN_CC2;
813 mutex_lock(&chip->lock);
814 if (chip->vconn_on == on) {
815 fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
819 switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
820 FUSB_REG_SWITCHES0_VCONN_CC2 :
821 FUSB_REG_SWITCHES0_VCONN_CC1;
823 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
824 switches0_mask, switches0_data);
828 fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
830 mutex_unlock(&chip->lock);
835 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
837 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
841 mutex_lock(&chip->lock);
842 if (chip->vbus_on == on) {
843 fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
846 ret = regulator_enable(chip->vbus);
848 ret = regulator_disable(chip->vbus);
850 fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
851 on ? "enable" : "disable", ret);
855 fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
857 if (chip->charge_on == charge)
858 fusb302_log(chip, "charge is already %s",
859 charge ? "On" : "Off");
861 chip->charge_on = charge;
864 mutex_unlock(&chip->lock);
869 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
871 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
872 FUSB_REG_CONTROL0_TX_FLUSH);
875 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
877 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
878 FUSB_REG_CONTROL1_RX_FLUSH);
881 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
884 return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
885 FUSB_REG_SWITCHES1_AUTO_GCRC);
886 return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
887 FUSB_REG_SWITCHES1_AUTO_GCRC);
890 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
893 u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
894 u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
895 FUSB_REG_MASKA_HARDSENT |
896 FUSB_REG_MASKA_TX_SUCCESS |
897 FUSB_REG_MASKA_HARDRESET;
898 u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
901 fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
902 fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
906 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
907 fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
911 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
912 fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
916 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
918 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
922 mutex_lock(&chip->lock);
923 ret = fusb302_pd_rx_flush(chip);
925 fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
928 ret = fusb302_pd_tx_flush(chip);
930 fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
933 ret = fusb302_pd_set_auto_goodcrc(chip, on);
935 fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
936 on ? "on" : "off", ret);
939 ret = fusb302_pd_set_interrupts(chip, on);
941 fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
942 on ? "on" : "off", ret);
945 fusb302_log(chip, "pd := %s", on ? "on" : "off");
947 mutex_unlock(&chip->lock);
952 static const char * const typec_role_name[] = {
953 [TYPEC_SINK] = "Sink",
954 [TYPEC_SOURCE] = "Source",
957 static const char * const typec_data_role_name[] = {
958 [TYPEC_DEVICE] = "Device",
959 [TYPEC_HOST] = "Host",
962 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
963 enum typec_role pwr, enum typec_data_role data)
965 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
968 u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
969 FUSB_REG_SWITCHES1_DATAROLE;
970 u8 switches1_data = 0x00;
972 mutex_lock(&chip->lock);
973 if (pwr == TYPEC_SOURCE)
974 switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
975 if (data == TYPEC_HOST)
976 switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
977 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
978 switches1_mask, switches1_data);
980 fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
981 typec_role_name[pwr], typec_data_role_name[data],
985 fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
986 typec_data_role_name[data]);
988 mutex_unlock(&chip->lock);
993 static int tcpm_start_toggling(struct tcpc_dev *dev,
994 enum typec_port_type port_type,
995 enum typec_cc_status cc)
997 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1001 if (port_type != TYPEC_PORT_DRP)
1004 mutex_lock(&chip->lock);
1005 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
1007 fusb302_log(chip, "unable to set src current %s, ret=%d",
1008 typec_cc_status_name[cc], ret);
1011 ret = fusb302_set_toggling(chip, TOGGLING_MODE_DRP);
1014 "unable to start drp toggling, ret=%d", ret);
1017 fusb302_log(chip, "start drp toggling");
1019 mutex_unlock(&chip->lock);
1024 static int fusb302_pd_send_message(struct fusb302_chip *chip,
1025 const struct pd_message *msg)
1033 buf[pos++] = FUSB302_TKN_SYNC1;
1034 buf[pos++] = FUSB302_TKN_SYNC1;
1035 buf[pos++] = FUSB302_TKN_SYNC1;
1036 buf[pos++] = FUSB302_TKN_SYNC2;
1038 len = pd_header_cnt_le(msg->header) * 4;
1039 /* plug 2 for header */
1043 "PD message too long %d (incl. header)", len);
1046 /* packsym tells the FUSB302 chip that the next X bytes are payload */
1047 buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
1048 memcpy(&buf[pos], &msg->header, sizeof(msg->header));
1049 pos += sizeof(msg->header);
1052 memcpy(&buf[pos], msg->payload, len);
1056 buf[pos++] = FUSB302_TKN_JAMCRC;
1058 buf[pos++] = FUSB302_TKN_EOP;
1059 /* turn tx off after sending message */
1060 buf[pos++] = FUSB302_TKN_TXOFF;
1061 /* start transmission */
1062 buf[pos++] = FUSB302_TKN_TXON;
1064 ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
1067 fusb302_log(chip, "sending PD message header: %x", msg->header);
1068 fusb302_log(chip, "sending PD message len: %d", len);
1073 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1075 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1076 FUSB_REG_CONTROL3_SEND_HARDRESET);
1079 static const char * const transmit_type_name[] = {
1080 [TCPC_TX_SOP] = "SOP",
1081 [TCPC_TX_SOP_PRIME] = "SOP'",
1082 [TCPC_TX_SOP_PRIME_PRIME] = "SOP''",
1083 [TCPC_TX_SOP_DEBUG_PRIME] = "DEBUG'",
1084 [TCPC_TX_SOP_DEBUG_PRIME_PRIME] = "DEBUG''",
1085 [TCPC_TX_HARD_RESET] = "HARD_RESET",
1086 [TCPC_TX_CABLE_RESET] = "CABLE_RESET",
1087 [TCPC_TX_BIST_MODE_2] = "BIST_MODE_2",
1090 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1091 const struct pd_message *msg)
1093 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1097 mutex_lock(&chip->lock);
1100 ret = fusb302_pd_send_message(chip, msg);
1103 "cannot send PD message, ret=%d", ret);
1105 case TCPC_TX_HARD_RESET:
1106 ret = fusb302_pd_send_hardreset(chip);
1109 "cannot send hardreset, ret=%d", ret);
1112 fusb302_log(chip, "type %s not supported",
1113 transmit_type_name[type]);
1116 mutex_unlock(&chip->lock);
1121 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1123 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1124 return TYPEC_CC_RP_3_0;
1125 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1126 return TYPEC_CC_RP_1_5;
1127 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1128 return TYPEC_CC_RP_DEF;
1129 return TYPEC_CC_OPEN;
1132 static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1134 struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1135 bc_lvl_handler.work);
1139 enum typec_cc_status cc_status;
1141 mutex_lock(&chip->lock);
1142 if (!chip->intr_bc_lvl) {
1143 fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1146 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1149 fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1150 if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1151 fusb302_log(chip, "CC activities detected, delay handling");
1152 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1153 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1156 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1157 cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1158 if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1159 if (chip->cc1 != cc_status) {
1160 fusb302_log(chip, "cc1: %s -> %s",
1161 typec_cc_status_name[chip->cc1],
1162 typec_cc_status_name[cc_status]);
1163 chip->cc1 = cc_status;
1164 tcpm_cc_change(chip->tcpm_port);
1167 if (chip->cc2 != cc_status) {
1168 fusb302_log(chip, "cc2: %s -> %s",
1169 typec_cc_status_name[chip->cc2],
1170 typec_cc_status_name[cc_status]);
1171 chip->cc2 = cc_status;
1172 tcpm_cc_change(chip->tcpm_port);
1177 mutex_unlock(&chip->lock);
1180 #define PDO_FIXED_FLAGS \
1181 (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1183 static const u32 src_pdo[] = {
1184 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1187 static const u32 snk_pdo[] = {
1188 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1191 static const struct tcpc_config fusb302_tcpc_config = {
1193 .nr_src_pdo = ARRAY_SIZE(src_pdo),
1194 .operating_snk_mw = 2500,
1195 .type = TYPEC_PORT_DRP,
1196 .data = TYPEC_PORT_DRD,
1197 .default_role = TYPEC_SINK,
1201 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1203 fusb302_tcpc_dev->init = tcpm_init;
1204 fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1205 fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
1206 fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1207 fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1208 fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1209 fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1210 fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1211 fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1212 fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1213 fusb302_tcpc_dev->start_toggling = tcpm_start_toggling;
1214 fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1217 static const char * const cc_polarity_name[] = {
1218 [TYPEC_POLARITY_CC1] = "Polarity_CC1",
1219 [TYPEC_POLARITY_CC2] = "Polarity_CC2",
1222 static int fusb302_set_cc_polarity(struct fusb302_chip *chip,
1223 enum typec_cc_polarity cc_polarity)
1226 u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
1227 FUSB_REG_SWITCHES0_CC2_PU_EN |
1228 FUSB_REG_SWITCHES0_VCONN_CC1 |
1229 FUSB_REG_SWITCHES0_VCONN_CC2 |
1230 FUSB_REG_SWITCHES0_MEAS_CC1 |
1231 FUSB_REG_SWITCHES0_MEAS_CC2;
1232 u8 switches0_data = 0x00;
1233 u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
1234 FUSB_REG_SWITCHES1_TXCC2_EN;
1235 u8 switches1_data = 0x00;
1237 if (cc_polarity == TYPEC_POLARITY_CC1) {
1238 switches0_data = FUSB_REG_SWITCHES0_MEAS_CC1;
1240 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1242 switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1243 switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1245 switches0_data = FUSB_REG_SWITCHES0_MEAS_CC2;
1247 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1249 switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1250 switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1252 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
1253 switches0_mask, switches0_data);
1256 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1257 switches1_mask, switches1_data);
1260 chip->cc_polarity = cc_polarity;
1265 static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1271 enum typec_cc_polarity cc_polarity;
1272 enum typec_cc_status cc_status_active, cc1, cc2;
1274 /* set pull_up, pull_down */
1275 ret = fusb302_set_cc_pull(chip, false, true);
1277 fusb302_log(chip, "cannot set cc to pull down, ret=%d", ret);
1281 cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1282 TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1283 ret = fusb302_set_cc_polarity(chip, cc_polarity);
1285 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1286 cc_polarity_name[cc_polarity], ret);
1289 /* fusb302_set_cc_polarity() has set the correct measure block */
1290 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1293 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1294 cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1295 /* restart toggling if the cc status on the active line is OPEN */
1296 if (cc_status_active == TYPEC_CC_OPEN) {
1297 fusb302_log(chip, "restart toggling as CC_OPEN detected");
1298 ret = fusb302_set_toggling(chip, chip->toggling_mode);
1301 /* update tcpm with the new cc value */
1302 cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1303 cc_status_active : TYPEC_CC_OPEN;
1304 cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1305 cc_status_active : TYPEC_CC_OPEN;
1306 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1309 tcpm_cc_change(chip->tcpm_port);
1311 /* turn off toggling */
1312 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
1315 "cannot set toggling mode off, ret=%d", ret);
1318 /* unmask bc_lvl interrupt */
1319 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1322 "cannot unmask bc_lcl interrupt, ret=%d", ret);
1325 chip->intr_bc_lvl = true;
1326 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1327 typec_cc_status_name[cc1],
1328 typec_cc_status_name[cc2]);
1333 static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1337 * - set polarity (measure cc, vconn, tx)
1338 * - set pull_up, pull_down
1339 * - set cc1, cc2, and update to tcpm_port
1340 * - set I_COMP interrupt on
1344 u8 ra_mda = ra_mda_value[chip->src_current_status];
1345 u8 rd_mda = rd_mda_value[chip->src_current_status];
1346 bool ra_comp, rd_comp;
1347 enum typec_cc_polarity cc_polarity;
1348 enum typec_cc_status cc_status_active, cc1, cc2;
1350 /* set pull_up, pull_down */
1351 ret = fusb302_set_cc_pull(chip, true, false);
1353 fusb302_log(chip, "cannot set cc to pull up, ret=%d", ret);
1357 cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1) ?
1358 TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1359 ret = fusb302_set_cc_polarity(chip, cc_polarity);
1361 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1362 cc_polarity_name[cc_polarity], ret);
1365 /* fusb302_set_cc_polarity() has set the correct measure block */
1366 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1369 usleep_range(50, 100);
1370 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1373 rd_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
1375 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1378 usleep_range(50, 100);
1379 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1382 ra_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
1385 cc_status_active = TYPEC_CC_OPEN;
1387 cc_status_active = TYPEC_CC_RD;
1389 /* Ra is not supported, report as Open */
1390 cc_status_active = TYPEC_CC_OPEN;
1391 /* restart toggling if the cc status on the active line is OPEN */
1392 if (cc_status_active == TYPEC_CC_OPEN) {
1393 fusb302_log(chip, "restart toggling as CC_OPEN detected");
1394 ret = fusb302_set_toggling(chip, chip->toggling_mode);
1397 /* update tcpm with the new cc value */
1398 cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1399 cc_status_active : TYPEC_CC_OPEN;
1400 cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1401 cc_status_active : TYPEC_CC_OPEN;
1402 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1405 tcpm_cc_change(chip->tcpm_port);
1407 /* turn off toggling */
1408 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
1411 "cannot set toggling mode off, ret=%d", ret);
1414 /* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1415 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1418 /* unmask comp_chng interrupt */
1419 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1420 FUSB_REG_MASK_COMP_CHNG);
1423 "cannot unmask bc_lcl interrupt, ret=%d", ret);
1426 chip->intr_comp_chng = true;
1427 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1428 typec_cc_status_name[cc1],
1429 typec_cc_status_name[cc2]);
1434 static int fusb302_handle_togdone(struct fusb302_chip *chip)
1440 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1443 togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1444 FUSB_REG_STATUS1A_TOGSS_MASK;
1445 switch (togdone_result) {
1446 case FUSB_REG_STATUS1A_TOGSS_SNK1:
1447 case FUSB_REG_STATUS1A_TOGSS_SNK2:
1448 return fusb302_handle_togdone_snk(chip, togdone_result);
1449 case FUSB_REG_STATUS1A_TOGSS_SRC1:
1450 case FUSB_REG_STATUS1A_TOGSS_SRC2:
1451 return fusb302_handle_togdone_src(chip, togdone_result);
1452 case FUSB_REG_STATUS1A_TOGSS_AA:
1453 /* doesn't support */
1454 fusb302_log(chip, "AudioAccessory not supported");
1455 fusb302_set_toggling(chip, chip->toggling_mode);
1458 fusb302_log(chip, "TOGDONE with an invalid state: %d",
1460 fusb302_set_toggling(chip, chip->toggling_mode);
1466 static int fusb302_pd_reset(struct fusb302_chip *chip)
1468 return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1469 FUSB_REG_RESET_PD_RESET);
1472 static int fusb302_pd_read_message(struct fusb302_chip *chip,
1473 struct pd_message *msg)
1480 /* first SOP token */
1481 ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1484 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1485 (u8 *)&msg->header);
1488 len = pd_header_cnt_le(msg->header) * 4;
1489 /* add 4 to length to include the CRC */
1490 if (len > PD_MAX_PAYLOAD * 4) {
1491 fusb302_log(chip, "PD message too long %d", len);
1495 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1496 (u8 *)msg->payload);
1500 /* another 4 bytes to read CRC out */
1501 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1504 fusb302_log(chip, "PD message header: %x", msg->header);
1505 fusb302_log(chip, "PD message len: %d", len);
1508 * Check if we've read off a GoodCRC message. If so then indicate to
1509 * TCPM that the previous transmission has completed. Otherwise we pass
1510 * the received message over to TCPM for processing.
1512 * We make this check here instead of basing the reporting decision on
1513 * the IRQ event type, as it's possible for the chip to report the
1514 * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
1515 * to check the message type to ensure correct reporting to TCPM.
1517 if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
1518 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1520 tcpm_pd_receive(chip->tcpm_port, msg);
1525 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1527 struct fusb302_chip *chip = dev_id;
1537 bool intr_comp_chng;
1538 struct pd_message pd_msg;
1540 mutex_lock(&chip->lock);
1541 /* grab a snapshot of intr flags */
1542 intr_togdone = chip->intr_togdone;
1543 intr_bc_lvl = chip->intr_bc_lvl;
1544 intr_comp_chng = chip->intr_comp_chng;
1546 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1549 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1552 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1555 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1559 "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1560 interrupt, interrupta, interruptb, status0);
1562 if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1563 vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1564 fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1565 vbus_present ? "On" : "Off");
1566 if (vbus_present != chip->vbus_present) {
1567 chip->vbus_present = vbus_present;
1568 tcpm_vbus_change(chip->tcpm_port);
1572 if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1573 fusb302_log(chip, "IRQ: TOGDONE");
1574 ret = fusb302_handle_togdone(chip);
1577 "handle togdone error, ret=%d", ret);
1582 if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1583 fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1585 * as BC_LVL interrupt can be affected by PD activity,
1586 * apply delay to for the handler to wait for the PD
1587 * signaling to finish.
1589 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1590 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1593 if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1594 comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1595 fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1596 comp_result ? "true" : "false");
1598 /* cc level > Rd_threashold, detach */
1599 if (chip->cc_polarity == TYPEC_POLARITY_CC1)
1600 chip->cc1 = TYPEC_CC_OPEN;
1602 chip->cc2 = TYPEC_CC_OPEN;
1603 tcpm_cc_change(chip->tcpm_port);
1607 if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1608 fusb302_log(chip, "IRQ: PD collision");
1609 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1612 if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1613 fusb302_log(chip, "IRQ: PD retry failed");
1614 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1617 if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1618 fusb302_log(chip, "IRQ: PD hardreset sent");
1619 ret = fusb302_pd_reset(chip);
1621 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1624 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1627 if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1628 fusb302_log(chip, "IRQ: PD tx success");
1629 ret = fusb302_pd_read_message(chip, &pd_msg);
1632 "cannot read in PD message, ret=%d", ret);
1637 if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1638 fusb302_log(chip, "IRQ: PD received hardreset");
1639 ret = fusb302_pd_reset(chip);
1641 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1644 tcpm_pd_hard_reset(chip->tcpm_port);
1647 if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1648 fusb302_log(chip, "IRQ: PD sent good CRC");
1649 ret = fusb302_pd_read_message(chip, &pd_msg);
1652 "cannot read in PD message, ret=%d", ret);
1657 mutex_unlock(&chip->lock);
1662 static int init_gpio(struct fusb302_chip *chip)
1664 struct device_node *node;
1667 node = chip->dev->of_node;
1668 chip->gpio_int_n = of_get_named_gpio(node, "fcs,int_n", 0);
1669 if (!gpio_is_valid(chip->gpio_int_n)) {
1670 ret = chip->gpio_int_n;
1671 dev_err(chip->dev, "cannot get named GPIO Int_N, ret=%d", ret);
1674 ret = devm_gpio_request(chip->dev, chip->gpio_int_n, "fcs,int_n");
1676 dev_err(chip->dev, "cannot request GPIO Int_N, ret=%d", ret);
1679 ret = gpio_direction_input(chip->gpio_int_n);
1682 "cannot set GPIO Int_N to input, ret=%d", ret);
1685 ret = gpio_to_irq(chip->gpio_int_n);
1688 "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1691 chip->gpio_int_n_irq = ret;
1695 static int fusb302_composite_snk_pdo_array(struct fusb302_chip *chip)
1697 struct device *dev = chip->dev;
1700 chip->snk_pdo[0] = PDO_FIXED(5000, 400, PDO_FIXED_FLAGS);
1703 * As max_snk_ma/mv/mw is not needed for tcpc_config,
1704 * those settings should be passed in via sink PDO, so
1705 * "fcs, max-sink-*" properties will be deprecated, to
1706 * perserve compatibility with existing users of them,
1707 * we read those properties to convert them to be a var
1710 if (device_property_read_u32(dev, "fcs,max-sink-microvolt", &max_uv) ||
1711 device_property_read_u32(dev, "fcs,max-sink-microamp", &max_ua))
1714 chip->snk_pdo[1] = PDO_VAR(5000, max_uv / 1000, max_ua / 1000);
1718 static int fusb302_probe(struct i2c_client *client,
1719 const struct i2c_device_id *id)
1721 struct fusb302_chip *chip;
1722 struct i2c_adapter *adapter;
1723 struct device *dev = &client->dev;
1728 adapter = to_i2c_adapter(client->dev.parent);
1729 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1730 dev_err(&client->dev,
1731 "I2C/SMBus block functionality not supported!\n");
1734 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1738 chip->i2c_client = client;
1739 i2c_set_clientdata(client, chip);
1740 chip->dev = &client->dev;
1741 chip->tcpc_config = fusb302_tcpc_config;
1742 chip->tcpc_dev.config = &chip->tcpc_config;
1743 mutex_init(&chip->lock);
1745 if (!device_property_read_u32(dev, "fcs,operating-sink-microwatt", &v))
1746 chip->tcpc_config.operating_snk_mw = v / 1000;
1748 /* Composite sink PDO */
1749 chip->tcpc_config.nr_snk_pdo = fusb302_composite_snk_pdo_array(chip);
1750 chip->tcpc_config.snk_pdo = chip->snk_pdo;
1753 * Devicetree platforms should get extcon via phandle (not yet
1754 * supported). On ACPI platforms, we get the name from a device prop.
1755 * This device prop is for kernel internal use only and is expected
1756 * to be set by the platform code which also registers the i2c client
1759 if (device_property_read_string(dev, "fcs,extcon-name", &name) == 0) {
1760 chip->extcon = extcon_get_extcon_dev(name);
1762 return -EPROBE_DEFER;
1765 fusb302_debugfs_init(chip);
1767 chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1770 goto clear_client_data;
1772 INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1773 init_tcpc_dev(&chip->tcpc_dev);
1775 chip->vbus = devm_regulator_get(chip->dev, "vbus");
1776 if (IS_ERR(chip->vbus)) {
1777 ret = PTR_ERR(chip->vbus);
1778 goto destroy_workqueue;
1782 chip->gpio_int_n_irq = client->irq;
1784 ret = init_gpio(chip);
1786 goto destroy_workqueue;
1789 chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1790 if (IS_ERR(chip->tcpm_port)) {
1791 ret = PTR_ERR(chip->tcpm_port);
1792 if (ret != -EPROBE_DEFER)
1793 dev_err(dev, "cannot register tcpm port, ret=%d", ret);
1794 goto destroy_workqueue;
1797 ret = devm_request_threaded_irq(chip->dev, chip->gpio_int_n_irq,
1798 NULL, fusb302_irq_intn,
1799 IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1800 "fsc_interrupt_int_n", chip);
1802 dev_err(dev, "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1803 goto tcpm_unregister_port;
1805 enable_irq_wake(chip->gpio_int_n_irq);
1808 tcpm_unregister_port:
1809 tcpm_unregister_port(chip->tcpm_port);
1811 destroy_workqueue(chip->wq);
1813 i2c_set_clientdata(client, NULL);
1814 fusb302_debugfs_exit(chip);
1819 static int fusb302_remove(struct i2c_client *client)
1821 struct fusb302_chip *chip = i2c_get_clientdata(client);
1823 tcpm_unregister_port(chip->tcpm_port);
1824 destroy_workqueue(chip->wq);
1825 i2c_set_clientdata(client, NULL);
1826 fusb302_debugfs_exit(chip);
1831 static int fusb302_pm_suspend(struct device *dev)
1833 struct fusb302_chip *chip = dev->driver_data;
1835 if (atomic_read(&chip->i2c_busy))
1837 atomic_set(&chip->pm_suspend, 1);
1842 static int fusb302_pm_resume(struct device *dev)
1844 struct fusb302_chip *chip = dev->driver_data;
1846 atomic_set(&chip->pm_suspend, 0);
1851 static const struct of_device_id fusb302_dt_match[] = {
1852 {.compatible = "fcs,fusb302"},
1855 MODULE_DEVICE_TABLE(of, fusb302_dt_match);
1857 static const struct i2c_device_id fusb302_i2c_device_id[] = {
1858 {"typec_fusb302", 0},
1861 MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
1863 static const struct dev_pm_ops fusb302_pm_ops = {
1864 .suspend = fusb302_pm_suspend,
1865 .resume = fusb302_pm_resume,
1868 static struct i2c_driver fusb302_driver = {
1870 .name = "typec_fusb302",
1871 .pm = &fusb302_pm_ops,
1872 .of_match_table = of_match_ptr(fusb302_dt_match),
1874 .probe = fusb302_probe,
1875 .remove = fusb302_remove,
1876 .id_table = fusb302_i2c_device_id,
1878 module_i2c_driver(fusb302_driver);
1880 MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1881 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1882 MODULE_LICENSE("GPL");