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/power_supply.h>
22 #include <linux/proc_fs.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/sched/clock.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
29 #include <linux/usb/typec.h>
30 #include <linux/usb/tcpm.h>
31 #include <linux/usb/pd.h>
32 #include <linux/workqueue.h>
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 static const char * const toggling_mode_name[] = {
53 [TOGGLINE_MODE_OFF] = "toggling_OFF",
54 [TOGGLING_MODE_DRP] = "toggling_DRP",
55 [TOGGLING_MODE_SNK] = "toggling_SNK",
56 [TOGGLING_MODE_SRC] = "toggling_SRC",
59 enum src_current_status {
65 static const u8 ra_mda_value[] = {
66 [SRC_CURRENT_DEFAULT] = 4, /* 210mV */
67 [SRC_CURRENT_MEDIUM] = 9, /* 420mV */
68 [SRC_CURRENT_HIGH] = 18, /* 798mV */
71 static const u8 rd_mda_value[] = {
72 [SRC_CURRENT_DEFAULT] = 38, /* 1638mV */
73 [SRC_CURRENT_MEDIUM] = 38, /* 1638mV */
74 [SRC_CURRENT_HIGH] = 61, /* 2604mV */
77 #define LOG_BUFFER_ENTRIES 1024
78 #define LOG_BUFFER_ENTRY_SIZE 128
82 struct i2c_client *i2c_client;
83 struct tcpm_port *tcpm_port;
84 struct tcpc_dev tcpc_dev;
85 struct tcpc_config tcpc_config;
87 struct regulator *vbus;
91 struct extcon_dev *extcon;
93 struct workqueue_struct *wq;
94 struct delayed_work bc_lvl_handler;
99 /* lock for sharing chip states */
102 /* psy + psy status */
103 struct power_supply *psy;
108 enum toggling_mode toggling_mode;
109 enum src_current_status src_current_status;
120 enum typec_cc_polarity cc_polarity;
121 enum typec_cc_status cc1;
122 enum typec_cc_status cc2;
124 #ifdef CONFIG_DEBUG_FS
125 struct dentry *dentry;
126 /* lock for log buffer access */
127 struct mutex logbuffer_lock;
130 u8 *logbuffer[LOG_BUFFER_ENTRIES];
138 #ifdef CONFIG_DEBUG_FS
140 static bool fusb302_log_full(struct fusb302_chip *chip)
142 return chip->logbuffer_tail ==
143 (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
146 static void _fusb302_log(struct fusb302_chip *chip, const char *fmt,
149 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
150 u64 ts_nsec = local_clock();
151 unsigned long rem_nsec;
153 if (!chip->logbuffer[chip->logbuffer_head]) {
154 chip->logbuffer[chip->logbuffer_head] =
155 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
156 if (!chip->logbuffer[chip->logbuffer_head])
160 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
162 mutex_lock(&chip->logbuffer_lock);
164 if (fusb302_log_full(chip)) {
165 chip->logbuffer_head = max(chip->logbuffer_head - 1, 0);
166 strlcpy(tmpbuffer, "overflow", sizeof(tmpbuffer));
169 if (chip->logbuffer_head < 0 ||
170 chip->logbuffer_head >= LOG_BUFFER_ENTRIES) {
172 "Bad log buffer index %d\n", chip->logbuffer_head);
176 if (!chip->logbuffer[chip->logbuffer_head]) {
178 "Log buffer index %d is NULL\n", chip->logbuffer_head);
182 rem_nsec = do_div(ts_nsec, 1000000000);
183 scnprintf(chip->logbuffer[chip->logbuffer_head],
184 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
185 (unsigned long)ts_nsec, rem_nsec / 1000,
187 chip->logbuffer_head = (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
190 mutex_unlock(&chip->logbuffer_lock);
193 static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
198 _fusb302_log(chip, fmt, args);
202 static int fusb302_seq_show(struct seq_file *s, void *v)
204 struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
207 mutex_lock(&chip->logbuffer_lock);
208 tail = chip->logbuffer_tail;
209 while (tail != chip->logbuffer_head) {
210 seq_printf(s, "%s\n", chip->logbuffer[tail]);
211 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
213 if (!seq_has_overflowed(s))
214 chip->logbuffer_tail = tail;
215 mutex_unlock(&chip->logbuffer_lock);
220 static int fusb302_debug_open(struct inode *inode, struct file *file)
222 return single_open(file, fusb302_seq_show, inode->i_private);
225 static const struct file_operations fusb302_debug_operations = {
226 .open = fusb302_debug_open,
229 .release = single_release,
232 static struct dentry *rootdir;
234 static int fusb302_debugfs_init(struct fusb302_chip *chip)
236 mutex_init(&chip->logbuffer_lock);
238 rootdir = debugfs_create_dir("fusb302", NULL);
243 chip->dentry = debugfs_create_file(dev_name(chip->dev),
244 S_IFREG | 0444, rootdir,
245 chip, &fusb302_debug_operations);
250 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
252 debugfs_remove(chip->dentry);
257 static void fusb302_log(const struct fusb302_chip *chip,
258 const char *fmt, ...) { }
259 static int fusb302_debugfs_init(const struct fusb302_chip *chip) { return 0; }
260 static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
264 #define FUSB302_RESUME_RETRY 10
265 #define FUSB302_RESUME_RETRY_SLEEP 50
267 static bool fusb302_is_suspended(struct fusb302_chip *chip)
271 for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) {
272 if (atomic_read(&chip->pm_suspend)) {
273 dev_err(chip->dev, "i2c: pm suspend, retry %d/%d\n",
274 retry_cnt + 1, FUSB302_RESUME_RETRY);
275 msleep(FUSB302_RESUME_RETRY_SLEEP);
284 static int fusb302_i2c_write(struct fusb302_chip *chip,
289 atomic_set(&chip->i2c_busy, 1);
291 if (fusb302_is_suspended(chip)) {
292 atomic_set(&chip->i2c_busy, 0);
296 ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
298 fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
300 atomic_set(&chip->i2c_busy, 0);
305 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
306 u8 length, const u8 *data)
312 atomic_set(&chip->i2c_busy, 1);
314 if (fusb302_is_suspended(chip)) {
315 atomic_set(&chip->i2c_busy, 0);
319 ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
322 fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
323 address, length, ret);
324 atomic_set(&chip->i2c_busy, 0);
329 static int fusb302_i2c_read(struct fusb302_chip *chip,
330 u8 address, u8 *data)
334 atomic_set(&chip->i2c_busy, 1);
336 if (fusb302_is_suspended(chip)) {
337 atomic_set(&chip->i2c_busy, 0);
341 ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
344 fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
345 atomic_set(&chip->i2c_busy, 0);
350 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
357 atomic_set(&chip->i2c_busy, 1);
359 if (fusb302_is_suspended(chip)) {
360 atomic_set(&chip->i2c_busy, 0);
364 ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
367 fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
368 address, length, ret);
372 fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
373 ret, length, address);
378 atomic_set(&chip->i2c_busy, 0);
383 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
389 ret = fusb302_i2c_read(chip, address, &data);
394 ret = fusb302_i2c_write(chip, address, data);
401 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
404 return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
407 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
410 return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
413 static int fusb302_sw_reset(struct fusb302_chip *chip)
417 ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
418 FUSB_REG_RESET_SW_RESET);
420 fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
422 fusb302_log(chip, "sw reset");
427 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip)
431 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
432 FUSB_REG_CONTROL3_N_RETRIES_3 |
433 FUSB_REG_CONTROL3_AUTO_RETRY);
439 * initialize interrupt on the chip
440 * - unmasked interrupt: VBUS_OK
442 static int fusb302_init_interrupt(struct fusb302_chip *chip)
446 ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
447 0xFF & ~FUSB_REG_MASK_VBUSOK);
450 ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
453 ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
456 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
457 FUSB_REG_CONTROL0_INT_MASK);
464 static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
468 ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
473 static int tcpm_init(struct tcpc_dev *dev)
475 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
480 ret = fusb302_sw_reset(chip);
483 ret = fusb302_enable_tx_auto_retries(chip);
486 ret = fusb302_init_interrupt(chip);
489 ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
492 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
495 chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
496 ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
499 fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
504 static int tcpm_get_vbus(struct tcpc_dev *dev)
506 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
510 mutex_lock(&chip->lock);
511 ret = chip->vbus_present ? 1 : 0;
512 mutex_unlock(&chip->lock);
517 static int tcpm_get_current_limit(struct tcpc_dev *dev)
519 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
521 int current_limit = 0;
522 unsigned long timeout;
528 * USB2 Charger detection may still be in progress when we get here,
529 * this can take upto 600ms, wait 800ms max.
531 timeout = jiffies + msecs_to_jiffies(800);
533 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_SDP) == 1)
536 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_CDP) == 1 ||
537 extcon_get_state(chip->extcon, EXTCON_CHG_USB_ACA) == 1)
538 current_limit = 1500;
540 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_DCP) == 1)
541 current_limit = 2000;
544 } while (current_limit == 0 && time_before(jiffies, timeout));
546 return current_limit;
549 static int fusb302_set_cc_pull(struct fusb302_chip *chip,
550 bool pull_up, bool pull_down)
554 u8 mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
555 FUSB_REG_SWITCHES0_CC2_PU_EN |
556 FUSB_REG_SWITCHES0_CC1_PD_EN |
557 FUSB_REG_SWITCHES0_CC2_PD_EN;
560 data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
561 FUSB_REG_SWITCHES0_CC1_PU_EN :
562 FUSB_REG_SWITCHES0_CC2_PU_EN;
564 data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
565 FUSB_REG_SWITCHES0_CC2_PD_EN;
566 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
570 chip->pull_up = pull_up;
575 static int fusb302_set_src_current(struct fusb302_chip *chip,
576 enum src_current_status status)
580 chip->src_current_status = status;
582 case SRC_CURRENT_DEFAULT:
583 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
584 FUSB_REG_CONTROL0_HOST_CUR_MASK,
585 FUSB_REG_CONTROL0_HOST_CUR_DEF);
587 case SRC_CURRENT_MEDIUM:
588 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
589 FUSB_REG_CONTROL0_HOST_CUR_MASK,
590 FUSB_REG_CONTROL0_HOST_CUR_MED);
592 case SRC_CURRENT_HIGH:
593 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
594 FUSB_REG_CONTROL0_HOST_CUR_MASK,
595 FUSB_REG_CONTROL0_HOST_CUR_HIGH);
604 static int fusb302_set_toggling(struct fusb302_chip *chip,
605 enum toggling_mode mode)
609 /* first disable toggling */
610 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
611 FUSB_REG_CONTROL2_TOGGLE);
614 /* mask interrupts for SRC or SNK */
615 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
616 FUSB_REG_MASK_BC_LVL |
617 FUSB_REG_MASK_COMP_CHNG);
620 chip->intr_bc_lvl = false;
621 chip->intr_comp_chng = false;
622 /* configure toggling mode: none/snk/src/drp */
624 case TOGGLINE_MODE_OFF:
625 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
626 FUSB_REG_CONTROL2_MODE_MASK,
627 FUSB_REG_CONTROL2_MODE_NONE);
631 case TOGGLING_MODE_SNK:
632 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
633 FUSB_REG_CONTROL2_MODE_MASK,
634 FUSB_REG_CONTROL2_MODE_UFP);
638 case TOGGLING_MODE_SRC:
639 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
640 FUSB_REG_CONTROL2_MODE_MASK,
641 FUSB_REG_CONTROL2_MODE_DFP);
645 case TOGGLING_MODE_DRP:
646 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
647 FUSB_REG_CONTROL2_MODE_MASK,
648 FUSB_REG_CONTROL2_MODE_DRP);
656 if (mode == TOGGLINE_MODE_OFF) {
657 /* mask TOGDONE interrupt */
658 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
659 FUSB_REG_MASKA_TOGDONE);
662 chip->intr_togdone = false;
664 /* unmask TOGDONE interrupt */
665 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
666 FUSB_REG_MASKA_TOGDONE);
669 chip->intr_togdone = true;
671 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
672 FUSB_REG_CONTROL2_TOGGLE);
675 /* during toggling, consider cc as Open */
676 chip->cc1 = TYPEC_CC_OPEN;
677 chip->cc2 = TYPEC_CC_OPEN;
679 chip->toggling_mode = mode;
684 static const char * const typec_cc_status_name[] = {
685 [TYPEC_CC_OPEN] = "Open",
686 [TYPEC_CC_RA] = "Ra",
687 [TYPEC_CC_RD] = "Rd",
688 [TYPEC_CC_RP_DEF] = "Rp-def",
689 [TYPEC_CC_RP_1_5] = "Rp-1.5",
690 [TYPEC_CC_RP_3_0] = "Rp-3.0",
693 static const enum src_current_status cc_src_current[] = {
694 [TYPEC_CC_OPEN] = SRC_CURRENT_DEFAULT,
695 [TYPEC_CC_RA] = SRC_CURRENT_DEFAULT,
696 [TYPEC_CC_RD] = SRC_CURRENT_DEFAULT,
697 [TYPEC_CC_RP_DEF] = SRC_CURRENT_DEFAULT,
698 [TYPEC_CC_RP_1_5] = SRC_CURRENT_MEDIUM,
699 [TYPEC_CC_RP_3_0] = SRC_CURRENT_HIGH,
702 static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
704 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
707 bool pull_up, pull_down;
710 mutex_lock(&chip->lock);
720 case TYPEC_CC_RP_DEF:
721 case TYPEC_CC_RP_1_5:
722 case TYPEC_CC_RP_3_0:
727 fusb302_log(chip, "unsupported cc value %s",
728 typec_cc_status_name[cc]);
732 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
734 fusb302_log(chip, "cannot stop toggling, ret=%d", ret);
737 ret = fusb302_set_cc_pull(chip, pull_up, pull_down);
740 "cannot set cc pulling up %s, down %s, ret = %d",
741 pull_up ? "True" : "False",
742 pull_down ? "True" : "False",
746 /* reset the cc status */
747 chip->cc1 = TYPEC_CC_OPEN;
748 chip->cc2 = TYPEC_CC_OPEN;
749 /* adjust current for SRC */
751 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
753 fusb302_log(chip, "cannot set src current %s, ret=%d",
754 typec_cc_status_name[cc], ret);
758 /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
760 rd_mda = rd_mda_value[cc_src_current[cc]];
761 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
764 "cannot set SRC measure value, ret=%d",
768 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
769 FUSB_REG_MASK_BC_LVL |
770 FUSB_REG_MASK_COMP_CHNG,
771 FUSB_REG_MASK_COMP_CHNG);
773 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
777 chip->intr_bc_lvl = false;
778 chip->intr_comp_chng = true;
781 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
782 FUSB_REG_MASK_BC_LVL |
783 FUSB_REG_MASK_COMP_CHNG,
784 FUSB_REG_MASK_BC_LVL);
786 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
790 chip->intr_bc_lvl = true;
791 chip->intr_comp_chng = false;
793 fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
795 mutex_unlock(&chip->lock);
800 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
801 enum typec_cc_status *cc2)
803 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
806 mutex_lock(&chip->lock);
809 fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
810 typec_cc_status_name[*cc2]);
811 mutex_unlock(&chip->lock);
816 static int tcpm_set_polarity(struct tcpc_dev *dev,
817 enum typec_cc_polarity polarity)
822 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
824 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
827 u8 switches0_data = 0x00;
828 u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
829 FUSB_REG_SWITCHES0_VCONN_CC2;
831 mutex_lock(&chip->lock);
832 if (chip->vconn_on == on) {
833 fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
837 switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
838 FUSB_REG_SWITCHES0_VCONN_CC2 :
839 FUSB_REG_SWITCHES0_VCONN_CC1;
841 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
842 switches0_mask, switches0_data);
846 fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
848 mutex_unlock(&chip->lock);
853 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
855 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
859 mutex_lock(&chip->lock);
860 if (chip->vbus_on == on) {
861 fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
864 ret = regulator_enable(chip->vbus);
866 ret = regulator_disable(chip->vbus);
868 fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
869 on ? "enable" : "disable", ret);
873 fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
875 if (chip->charge_on == charge) {
876 fusb302_log(chip, "charge is already %s",
877 charge ? "On" : "Off");
879 chip->charge_on = charge;
880 power_supply_changed(chip->psy);
884 mutex_unlock(&chip->lock);
889 static int tcpm_set_current_limit(struct tcpc_dev *dev, u32 max_ma, u32 mv)
891 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
894 fusb302_log(chip, "current limit: %d ma, %d mv (not implemented)",
897 chip->supply_voltage = mv;
898 chip->current_limit = max_ma;
900 power_supply_changed(chip->psy);
905 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
907 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
908 FUSB_REG_CONTROL0_TX_FLUSH);
911 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
913 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
914 FUSB_REG_CONTROL1_RX_FLUSH);
917 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
920 return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
921 FUSB_REG_SWITCHES1_AUTO_GCRC);
922 return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
923 FUSB_REG_SWITCHES1_AUTO_GCRC);
926 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
929 u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
930 u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
931 FUSB_REG_MASKA_HARDSENT |
932 FUSB_REG_MASKA_TX_SUCCESS |
933 FUSB_REG_MASKA_HARDRESET;
934 u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
937 fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
938 fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
942 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
943 fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
947 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
948 fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
952 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
954 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
958 mutex_lock(&chip->lock);
959 ret = fusb302_pd_rx_flush(chip);
961 fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
964 ret = fusb302_pd_tx_flush(chip);
966 fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
969 ret = fusb302_pd_set_auto_goodcrc(chip, on);
971 fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
972 on ? "on" : "off", ret);
975 ret = fusb302_pd_set_interrupts(chip, on);
977 fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
978 on ? "on" : "off", ret);
981 fusb302_log(chip, "pd := %s", on ? "on" : "off");
983 mutex_unlock(&chip->lock);
988 static const char * const typec_role_name[] = {
989 [TYPEC_SINK] = "Sink",
990 [TYPEC_SOURCE] = "Source",
993 static const char * const typec_data_role_name[] = {
994 [TYPEC_DEVICE] = "Device",
995 [TYPEC_HOST] = "Host",
998 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
999 enum typec_role pwr, enum typec_data_role data)
1001 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1004 u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
1005 FUSB_REG_SWITCHES1_DATAROLE;
1006 u8 switches1_data = 0x00;
1008 mutex_lock(&chip->lock);
1009 if (pwr == TYPEC_SOURCE)
1010 switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
1011 if (data == TYPEC_HOST)
1012 switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
1013 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1014 switches1_mask, switches1_data);
1016 fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
1017 typec_role_name[pwr], typec_data_role_name[data],
1021 fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
1022 typec_data_role_name[data]);
1024 mutex_unlock(&chip->lock);
1029 static int tcpm_start_drp_toggling(struct tcpc_dev *dev,
1030 enum typec_cc_status cc)
1032 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1036 mutex_lock(&chip->lock);
1037 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
1039 fusb302_log(chip, "unable to set src current %s, ret=%d",
1040 typec_cc_status_name[cc], ret);
1043 ret = fusb302_set_toggling(chip, TOGGLING_MODE_DRP);
1046 "unable to start drp toggling, ret=%d", ret);
1049 fusb302_log(chip, "start drp toggling");
1051 mutex_unlock(&chip->lock);
1056 static int fusb302_pd_send_message(struct fusb302_chip *chip,
1057 const struct pd_message *msg)
1065 buf[pos++] = FUSB302_TKN_SYNC1;
1066 buf[pos++] = FUSB302_TKN_SYNC1;
1067 buf[pos++] = FUSB302_TKN_SYNC1;
1068 buf[pos++] = FUSB302_TKN_SYNC2;
1070 len = pd_header_cnt_le(msg->header) * 4;
1071 /* plug 2 for header */
1075 "PD message too long %d (incl. header)", len);
1078 /* packsym tells the FUSB302 chip that the next X bytes are payload */
1079 buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
1080 memcpy(&buf[pos], &msg->header, sizeof(msg->header));
1081 pos += sizeof(msg->header);
1084 memcpy(&buf[pos], msg->payload, len);
1088 buf[pos++] = FUSB302_TKN_JAMCRC;
1090 buf[pos++] = FUSB302_TKN_EOP;
1091 /* turn tx off after sending message */
1092 buf[pos++] = FUSB302_TKN_TXOFF;
1093 /* start transmission */
1094 buf[pos++] = FUSB302_TKN_TXON;
1096 ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
1099 fusb302_log(chip, "sending PD message header: %x", msg->header);
1100 fusb302_log(chip, "sending PD message len: %d", len);
1105 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1107 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1108 FUSB_REG_CONTROL3_SEND_HARDRESET);
1111 static const char * const transmit_type_name[] = {
1112 [TCPC_TX_SOP] = "SOP",
1113 [TCPC_TX_SOP_PRIME] = "SOP'",
1114 [TCPC_TX_SOP_PRIME_PRIME] = "SOP''",
1115 [TCPC_TX_SOP_DEBUG_PRIME] = "DEBUG'",
1116 [TCPC_TX_SOP_DEBUG_PRIME_PRIME] = "DEBUG''",
1117 [TCPC_TX_HARD_RESET] = "HARD_RESET",
1118 [TCPC_TX_CABLE_RESET] = "CABLE_RESET",
1119 [TCPC_TX_BIST_MODE_2] = "BIST_MODE_2",
1122 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1123 const struct pd_message *msg)
1125 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1129 mutex_lock(&chip->lock);
1132 ret = fusb302_pd_send_message(chip, msg);
1135 "cannot send PD message, ret=%d", ret);
1137 case TCPC_TX_HARD_RESET:
1138 ret = fusb302_pd_send_hardreset(chip);
1141 "cannot send hardreset, ret=%d", ret);
1144 fusb302_log(chip, "type %s not supported",
1145 transmit_type_name[type]);
1148 mutex_unlock(&chip->lock);
1153 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1155 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1156 return TYPEC_CC_RP_3_0;
1157 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1158 return TYPEC_CC_RP_1_5;
1159 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1160 return TYPEC_CC_RP_DEF;
1161 return TYPEC_CC_OPEN;
1164 static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1166 struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1167 bc_lvl_handler.work);
1171 enum typec_cc_status cc_status;
1173 mutex_lock(&chip->lock);
1174 if (!chip->intr_bc_lvl) {
1175 fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1178 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1181 fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1182 if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1183 fusb302_log(chip, "CC activities detected, delay handling");
1184 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1185 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1188 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1189 cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1190 if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1191 if (chip->cc1 != cc_status) {
1192 fusb302_log(chip, "cc1: %s -> %s",
1193 typec_cc_status_name[chip->cc1],
1194 typec_cc_status_name[cc_status]);
1195 chip->cc1 = cc_status;
1196 tcpm_cc_change(chip->tcpm_port);
1199 if (chip->cc2 != cc_status) {
1200 fusb302_log(chip, "cc2: %s -> %s",
1201 typec_cc_status_name[chip->cc2],
1202 typec_cc_status_name[cc_status]);
1203 chip->cc2 = cc_status;
1204 tcpm_cc_change(chip->tcpm_port);
1209 mutex_unlock(&chip->lock);
1212 #define PDO_FIXED_FLAGS \
1213 (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1215 static const u32 src_pdo[] = {
1216 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1219 static const u32 snk_pdo[] = {
1220 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1223 static const struct tcpc_config fusb302_tcpc_config = {
1225 .nr_src_pdo = ARRAY_SIZE(src_pdo),
1227 .nr_snk_pdo = ARRAY_SIZE(snk_pdo),
1230 .max_snk_mw = 15000,
1231 .operating_snk_mw = 2500,
1232 .type = TYPEC_PORT_DRP,
1233 .default_role = TYPEC_SINK,
1237 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1239 fusb302_tcpc_dev->init = tcpm_init;
1240 fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1241 fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
1242 fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1243 fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1244 fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1245 fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1246 fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1247 fusb302_tcpc_dev->set_current_limit = tcpm_set_current_limit;
1248 fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1249 fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1250 fusb302_tcpc_dev->start_drp_toggling = tcpm_start_drp_toggling;
1251 fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1252 fusb302_tcpc_dev->mux = NULL;
1255 static const char * const cc_polarity_name[] = {
1256 [TYPEC_POLARITY_CC1] = "Polarity_CC1",
1257 [TYPEC_POLARITY_CC2] = "Polarity_CC2",
1260 static int fusb302_set_cc_polarity(struct fusb302_chip *chip,
1261 enum typec_cc_polarity cc_polarity)
1264 u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
1265 FUSB_REG_SWITCHES0_CC2_PU_EN |
1266 FUSB_REG_SWITCHES0_VCONN_CC1 |
1267 FUSB_REG_SWITCHES0_VCONN_CC2 |
1268 FUSB_REG_SWITCHES0_MEAS_CC1 |
1269 FUSB_REG_SWITCHES0_MEAS_CC2;
1270 u8 switches0_data = 0x00;
1271 u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
1272 FUSB_REG_SWITCHES1_TXCC2_EN;
1273 u8 switches1_data = 0x00;
1275 if (cc_polarity == TYPEC_POLARITY_CC1) {
1276 switches0_data = FUSB_REG_SWITCHES0_MEAS_CC1;
1278 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1280 switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1281 switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1283 switches0_data = FUSB_REG_SWITCHES0_MEAS_CC2;
1285 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1287 switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1288 switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1290 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
1291 switches0_mask, switches0_data);
1294 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1295 switches1_mask, switches1_data);
1298 chip->cc_polarity = cc_polarity;
1303 static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1309 enum typec_cc_polarity cc_polarity;
1310 enum typec_cc_status cc_status_active, cc1, cc2;
1312 /* set pull_up, pull_down */
1313 ret = fusb302_set_cc_pull(chip, false, true);
1315 fusb302_log(chip, "cannot set cc to pull down, ret=%d", ret);
1319 cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1320 TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1321 ret = fusb302_set_cc_polarity(chip, cc_polarity);
1323 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1324 cc_polarity_name[cc_polarity], ret);
1327 /* fusb302_set_cc_polarity() has set the correct measure block */
1328 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1331 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1332 cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1333 /* restart toggling if the cc status on the active line is OPEN */
1334 if (cc_status_active == TYPEC_CC_OPEN) {
1335 fusb302_log(chip, "restart toggling as CC_OPEN detected");
1336 ret = fusb302_set_toggling(chip, chip->toggling_mode);
1339 /* update tcpm with the new cc value */
1340 cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1341 cc_status_active : TYPEC_CC_OPEN;
1342 cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1343 cc_status_active : TYPEC_CC_OPEN;
1344 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1347 tcpm_cc_change(chip->tcpm_port);
1349 /* turn off toggling */
1350 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
1353 "cannot set toggling mode off, ret=%d", ret);
1356 /* unmask bc_lvl interrupt */
1357 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1360 "cannot unmask bc_lcl interrupt, ret=%d", ret);
1363 chip->intr_bc_lvl = true;
1364 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1365 typec_cc_status_name[cc1],
1366 typec_cc_status_name[cc2]);
1371 static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1375 * - set polarity (measure cc, vconn, tx)
1376 * - set pull_up, pull_down
1377 * - set cc1, cc2, and update to tcpm_port
1378 * - set I_COMP interrupt on
1382 u8 ra_mda = ra_mda_value[chip->src_current_status];
1383 u8 rd_mda = rd_mda_value[chip->src_current_status];
1384 bool ra_comp, rd_comp;
1385 enum typec_cc_polarity cc_polarity;
1386 enum typec_cc_status cc_status_active, cc1, cc2;
1388 /* set pull_up, pull_down */
1389 ret = fusb302_set_cc_pull(chip, true, false);
1391 fusb302_log(chip, "cannot set cc to pull up, ret=%d", ret);
1395 cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1) ?
1396 TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1397 ret = fusb302_set_cc_polarity(chip, cc_polarity);
1399 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1400 cc_polarity_name[cc_polarity], ret);
1403 /* fusb302_set_cc_polarity() has set the correct measure block */
1404 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1407 usleep_range(50, 100);
1408 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1411 rd_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
1413 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1416 usleep_range(50, 100);
1417 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1420 ra_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
1423 cc_status_active = TYPEC_CC_OPEN;
1425 cc_status_active = TYPEC_CC_RD;
1427 /* Ra is not supported, report as Open */
1428 cc_status_active = TYPEC_CC_OPEN;
1429 /* restart toggling if the cc status on the active line is OPEN */
1430 if (cc_status_active == TYPEC_CC_OPEN) {
1431 fusb302_log(chip, "restart toggling as CC_OPEN detected");
1432 ret = fusb302_set_toggling(chip, chip->toggling_mode);
1435 /* update tcpm with the new cc value */
1436 cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1437 cc_status_active : TYPEC_CC_OPEN;
1438 cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1439 cc_status_active : TYPEC_CC_OPEN;
1440 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1443 tcpm_cc_change(chip->tcpm_port);
1445 /* turn off toggling */
1446 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
1449 "cannot set toggling mode off, ret=%d", ret);
1452 /* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1453 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1456 /* unmask comp_chng interrupt */
1457 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1458 FUSB_REG_MASK_COMP_CHNG);
1461 "cannot unmask bc_lcl interrupt, ret=%d", ret);
1464 chip->intr_comp_chng = true;
1465 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1466 typec_cc_status_name[cc1],
1467 typec_cc_status_name[cc2]);
1472 static int fusb302_handle_togdone(struct fusb302_chip *chip)
1478 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1481 togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1482 FUSB_REG_STATUS1A_TOGSS_MASK;
1483 switch (togdone_result) {
1484 case FUSB_REG_STATUS1A_TOGSS_SNK1:
1485 case FUSB_REG_STATUS1A_TOGSS_SNK2:
1486 return fusb302_handle_togdone_snk(chip, togdone_result);
1487 case FUSB_REG_STATUS1A_TOGSS_SRC1:
1488 case FUSB_REG_STATUS1A_TOGSS_SRC2:
1489 return fusb302_handle_togdone_src(chip, togdone_result);
1490 case FUSB_REG_STATUS1A_TOGSS_AA:
1491 /* doesn't support */
1492 fusb302_log(chip, "AudioAccessory not supported");
1493 fusb302_set_toggling(chip, chip->toggling_mode);
1496 fusb302_log(chip, "TOGDONE with an invalid state: %d",
1498 fusb302_set_toggling(chip, chip->toggling_mode);
1504 static int fusb302_pd_reset(struct fusb302_chip *chip)
1506 return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1507 FUSB_REG_RESET_PD_RESET);
1510 static int fusb302_pd_read_message(struct fusb302_chip *chip,
1511 struct pd_message *msg)
1518 /* first SOP token */
1519 ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1522 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1523 (u8 *)&msg->header);
1526 len = pd_header_cnt_le(msg->header) * 4;
1527 /* add 4 to length to include the CRC */
1528 if (len > PD_MAX_PAYLOAD * 4) {
1529 fusb302_log(chip, "PD message too long %d", len);
1533 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1534 (u8 *)msg->payload);
1538 /* another 4 bytes to read CRC out */
1539 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1542 fusb302_log(chip, "PD message header: %x", msg->header);
1543 fusb302_log(chip, "PD message len: %d", len);
1546 * Check if we've read off a GoodCRC message. If so then indicate to
1547 * TCPM that the previous transmission has completed. Otherwise we pass
1548 * the received message over to TCPM for processing.
1550 * We make this check here instead of basing the reporting decision on
1551 * the IRQ event type, as it's possible for the chip to report the
1552 * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
1553 * to check the message type to ensure correct reporting to TCPM.
1555 if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
1556 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1558 tcpm_pd_receive(chip->tcpm_port, msg);
1563 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1565 struct fusb302_chip *chip = dev_id;
1575 bool intr_comp_chng;
1576 struct pd_message pd_msg;
1578 mutex_lock(&chip->lock);
1579 /* grab a snapshot of intr flags */
1580 intr_togdone = chip->intr_togdone;
1581 intr_bc_lvl = chip->intr_bc_lvl;
1582 intr_comp_chng = chip->intr_comp_chng;
1584 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1587 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1590 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1593 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1597 "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1598 interrupt, interrupta, interruptb, status0);
1600 if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1601 vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1602 fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1603 vbus_present ? "On" : "Off");
1604 if (vbus_present != chip->vbus_present) {
1605 chip->vbus_present = vbus_present;
1606 tcpm_vbus_change(chip->tcpm_port);
1610 if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1611 fusb302_log(chip, "IRQ: TOGDONE");
1612 ret = fusb302_handle_togdone(chip);
1615 "handle togdone error, ret=%d", ret);
1620 if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1621 fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1623 * as BC_LVL interrupt can be affected by PD activity,
1624 * apply delay to for the handler to wait for the PD
1625 * signaling to finish.
1627 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1628 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1631 if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1632 comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1633 fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1634 comp_result ? "true" : "false");
1636 /* cc level > Rd_threashold, detach */
1637 if (chip->cc_polarity == TYPEC_POLARITY_CC1)
1638 chip->cc1 = TYPEC_CC_OPEN;
1640 chip->cc2 = TYPEC_CC_OPEN;
1641 tcpm_cc_change(chip->tcpm_port);
1645 if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1646 fusb302_log(chip, "IRQ: PD collision");
1647 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1650 if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1651 fusb302_log(chip, "IRQ: PD retry failed");
1652 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1655 if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1656 fusb302_log(chip, "IRQ: PD hardreset sent");
1657 ret = fusb302_pd_reset(chip);
1659 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1662 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1665 if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1666 fusb302_log(chip, "IRQ: PD tx success");
1667 ret = fusb302_pd_read_message(chip, &pd_msg);
1670 "cannot read in PD message, ret=%d", ret);
1675 if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1676 fusb302_log(chip, "IRQ: PD received hardreset");
1677 ret = fusb302_pd_reset(chip);
1679 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1682 tcpm_pd_hard_reset(chip->tcpm_port);
1685 if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1686 fusb302_log(chip, "IRQ: PD sent good CRC");
1687 ret = fusb302_pd_read_message(chip, &pd_msg);
1690 "cannot read in PD message, ret=%d", ret);
1695 mutex_unlock(&chip->lock);
1700 static int fusb302_psy_get_property(struct power_supply *psy,
1701 enum power_supply_property psp,
1702 union power_supply_propval *val)
1704 struct fusb302_chip *chip = power_supply_get_drvdata(psy);
1707 case POWER_SUPPLY_PROP_ONLINE:
1708 val->intval = chip->charge_on;
1710 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1711 val->intval = chip->supply_voltage * 1000; /* mV -> µV */
1713 case POWER_SUPPLY_PROP_CURRENT_MAX:
1714 val->intval = chip->current_limit * 1000; /* mA -> µA */
1723 static enum power_supply_property fusb302_psy_properties[] = {
1724 POWER_SUPPLY_PROP_ONLINE,
1725 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1726 POWER_SUPPLY_PROP_CURRENT_MAX,
1729 static const struct power_supply_desc fusb302_psy_desc = {
1730 .name = "fusb302-typec-source",
1731 .type = POWER_SUPPLY_TYPE_USB_TYPE_C,
1732 .properties = fusb302_psy_properties,
1733 .num_properties = ARRAY_SIZE(fusb302_psy_properties),
1734 .get_property = fusb302_psy_get_property,
1737 static int init_gpio(struct fusb302_chip *chip)
1739 struct device_node *node;
1742 node = chip->dev->of_node;
1743 chip->gpio_int_n = of_get_named_gpio(node, "fcs,int_n", 0);
1744 if (!gpio_is_valid(chip->gpio_int_n)) {
1745 ret = chip->gpio_int_n;
1746 dev_err(chip->dev, "cannot get named GPIO Int_N, ret=%d", ret);
1749 ret = devm_gpio_request(chip->dev, chip->gpio_int_n, "fcs,int_n");
1751 dev_err(chip->dev, "cannot request GPIO Int_N, ret=%d", ret);
1754 ret = gpio_direction_input(chip->gpio_int_n);
1757 "cannot set GPIO Int_N to input, ret=%d", ret);
1760 ret = gpio_to_irq(chip->gpio_int_n);
1763 "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1766 chip->gpio_int_n_irq = ret;
1770 static int fusb302_probe(struct i2c_client *client,
1771 const struct i2c_device_id *id)
1773 struct fusb302_chip *chip;
1774 struct i2c_adapter *adapter;
1775 struct device *dev = &client->dev;
1776 struct power_supply_config cfg = {};
1781 adapter = to_i2c_adapter(client->dev.parent);
1782 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1783 dev_err(&client->dev,
1784 "I2C/SMBus block functionality not supported!\n");
1787 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1791 chip->i2c_client = client;
1792 i2c_set_clientdata(client, chip);
1793 chip->dev = &client->dev;
1794 chip->tcpc_config = fusb302_tcpc_config;
1795 chip->tcpc_dev.config = &chip->tcpc_config;
1796 mutex_init(&chip->lock);
1798 if (!device_property_read_u32(dev, "fcs,max-sink-microvolt", &v))
1799 chip->tcpc_config.max_snk_mv = v / 1000;
1801 if (!device_property_read_u32(dev, "fcs,max-sink-microamp", &v))
1802 chip->tcpc_config.max_snk_ma = v / 1000;
1804 if (!device_property_read_u32(dev, "fcs,max-sink-microwatt", &v))
1805 chip->tcpc_config.max_snk_mw = v / 1000;
1807 if (!device_property_read_u32(dev, "fcs,operating-sink-microwatt", &v))
1808 chip->tcpc_config.operating_snk_mw = v / 1000;
1811 * Devicetree platforms should get extcon via phandle (not yet
1812 * supported). On ACPI platforms, we get the name from a device prop.
1813 * This device prop is for kernel internal use only and is expected
1814 * to be set by the platform code which also registers the i2c client
1817 if (device_property_read_string(dev, "fcs,extcon-name", &name) == 0) {
1818 chip->extcon = extcon_get_extcon_dev(name);
1820 return -EPROBE_DEFER;
1823 cfg.drv_data = chip;
1824 chip->psy = devm_power_supply_register(dev, &fusb302_psy_desc, &cfg);
1825 if (IS_ERR(chip->psy)) {
1826 ret = PTR_ERR(chip->psy);
1827 dev_err(chip->dev, "Error registering power-supply: %d\n", ret);
1831 ret = fusb302_debugfs_init(chip);
1835 chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1838 goto clear_client_data;
1840 INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1841 init_tcpc_dev(&chip->tcpc_dev);
1843 chip->vbus = devm_regulator_get(chip->dev, "vbus");
1844 if (IS_ERR(chip->vbus)) {
1845 ret = PTR_ERR(chip->vbus);
1846 goto destroy_workqueue;
1850 chip->gpio_int_n_irq = client->irq;
1852 ret = init_gpio(chip);
1854 goto destroy_workqueue;
1857 chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1858 if (IS_ERR(chip->tcpm_port)) {
1859 ret = PTR_ERR(chip->tcpm_port);
1860 dev_err(dev, "cannot register tcpm port, ret=%d", ret);
1861 goto destroy_workqueue;
1864 ret = devm_request_threaded_irq(chip->dev, chip->gpio_int_n_irq,
1865 NULL, fusb302_irq_intn,
1866 IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1867 "fsc_interrupt_int_n", chip);
1869 dev_err(dev, "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1870 goto tcpm_unregister_port;
1872 enable_irq_wake(chip->gpio_int_n_irq);
1875 tcpm_unregister_port:
1876 tcpm_unregister_port(chip->tcpm_port);
1878 destroy_workqueue(chip->wq);
1880 i2c_set_clientdata(client, NULL);
1881 fusb302_debugfs_exit(chip);
1886 static int fusb302_remove(struct i2c_client *client)
1888 struct fusb302_chip *chip = i2c_get_clientdata(client);
1890 tcpm_unregister_port(chip->tcpm_port);
1891 destroy_workqueue(chip->wq);
1892 i2c_set_clientdata(client, NULL);
1893 fusb302_debugfs_exit(chip);
1898 static int fusb302_pm_suspend(struct device *dev)
1900 struct fusb302_chip *chip = dev->driver_data;
1902 if (atomic_read(&chip->i2c_busy))
1904 atomic_set(&chip->pm_suspend, 1);
1909 static int fusb302_pm_resume(struct device *dev)
1911 struct fusb302_chip *chip = dev->driver_data;
1913 atomic_set(&chip->pm_suspend, 0);
1918 static const struct of_device_id fusb302_dt_match[] = {
1919 {.compatible = "fcs,fusb302"},
1922 MODULE_DEVICE_TABLE(of, fusb302_dt_match);
1924 static const struct i2c_device_id fusb302_i2c_device_id[] = {
1925 {"typec_fusb302", 0},
1928 MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
1930 static const struct dev_pm_ops fusb302_pm_ops = {
1931 .suspend = fusb302_pm_suspend,
1932 .resume = fusb302_pm_resume,
1935 static struct i2c_driver fusb302_driver = {
1937 .name = "typec_fusb302",
1938 .pm = &fusb302_pm_ops,
1939 .of_match_table = of_match_ptr(fusb302_dt_match),
1941 .probe = fusb302_probe,
1942 .remove = fusb302_remove,
1943 .id_table = fusb302_i2c_device_id,
1945 module_i2c_driver(fusb302_driver);
1947 MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1948 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1949 MODULE_LICENSE("GPL");