2 * Copyright 2016-2017 Google, Inc
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * Fairchild FUSB302 Type-C Chip Driver
17 #include <linux/debugfs.h>
18 #include <linux/delay.h>
19 #include <linux/errno.h>
20 #include <linux/gpio.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/of_device.h>
27 #include <linux/of_device.h>
28 #include <linux/of_gpio.h>
29 #include <linux/pinctrl/consumer.h>
30 #include <linux/proc_fs.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/sched/clock.h>
33 #include <linux/seq_file.h>
34 #include <linux/slab.h>
35 #include <linux/string.h>
36 #include <linux/types.h>
37 #include <linux/usb/typec.h>
38 #include <linux/workqueue.h>
40 #include "fusb302_reg.h"
45 * When the device is SNK, BC_LVL interrupt is used to monitor cc pins
46 * for the current capability offered by the SRC. As FUSB302 chip fires
47 * the BC_LVL interrupt on PD signalings, cc lvl should be handled after
48 * a delay to avoid measuring on PD activities. The delay is slightly
49 * longer than PD_T_PD_DEBPUNCE (10-20ms).
51 #define T_BC_LVL_DEBOUNCE_DELAY_MS 30
60 static const char * const toggling_mode_name[] = {
61 [TOGGLINE_MODE_OFF] = "toggling_OFF",
62 [TOGGLING_MODE_DRP] = "toggling_DRP",
63 [TOGGLING_MODE_SNK] = "toggling_SNK",
64 [TOGGLING_MODE_SRC] = "toggling_SRC",
67 enum src_current_status {
73 static const u8 ra_mda_value[] = {
74 [SRC_CURRENT_DEFAULT] = 4, /* 210mV */
75 [SRC_CURRENT_MEDIUM] = 9, /* 420mV */
76 [SRC_CURRENT_HIGH] = 18, /* 798mV */
79 static const u8 rd_mda_value[] = {
80 [SRC_CURRENT_DEFAULT] = 38, /* 1638mV */
81 [SRC_CURRENT_MEDIUM] = 38, /* 1638mV */
82 [SRC_CURRENT_HIGH] = 61, /* 2604mV */
85 #define LOG_BUFFER_ENTRIES 1024
86 #define LOG_BUFFER_ENTRY_SIZE 128
90 struct i2c_client *i2c_client;
91 struct tcpm_port *tcpm_port;
92 struct tcpc_dev tcpc_dev;
94 struct regulator *vbus;
99 struct workqueue_struct *wq;
100 struct delayed_work bc_lvl_handler;
105 /* lock for sharing chip states */
109 enum toggling_mode toggling_mode;
110 enum src_current_status src_current_status;
121 enum typec_cc_polarity cc_polarity;
122 enum typec_cc_status cc1;
123 enum typec_cc_status cc2;
125 #ifdef CONFIG_DEBUG_FS
126 struct dentry *dentry;
127 /* lock for log buffer access */
128 struct mutex logbuffer_lock;
131 u8 *logbuffer[LOG_BUFFER_ENTRIES];
139 #ifdef CONFIG_DEBUG_FS
141 static bool fusb302_log_full(struct fusb302_chip *chip)
143 return chip->logbuffer_tail ==
144 (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
147 static void _fusb302_log(struct fusb302_chip *chip, const char *fmt,
150 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
151 u64 ts_nsec = local_clock();
152 unsigned long rem_nsec;
154 if (!chip->logbuffer[chip->logbuffer_head]) {
155 chip->logbuffer[chip->logbuffer_head] =
156 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
157 if (!chip->logbuffer[chip->logbuffer_head])
161 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
163 mutex_lock(&chip->logbuffer_lock);
165 if (fusb302_log_full(chip)) {
166 chip->logbuffer_head = max(chip->logbuffer_head - 1, 0);
167 strlcpy(tmpbuffer, "overflow", sizeof(tmpbuffer));
170 if (chip->logbuffer_head < 0 ||
171 chip->logbuffer_head >= LOG_BUFFER_ENTRIES) {
173 "Bad log buffer index %d\n", chip->logbuffer_head);
177 if (!chip->logbuffer[chip->logbuffer_head]) {
179 "Log buffer index %d is NULL\n", chip->logbuffer_head);
183 rem_nsec = do_div(ts_nsec, 1000000000);
184 scnprintf(chip->logbuffer[chip->logbuffer_head],
185 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
186 (unsigned long)ts_nsec, rem_nsec / 1000,
188 chip->logbuffer_head = (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
191 mutex_unlock(&chip->logbuffer_lock);
194 static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
199 _fusb302_log(chip, fmt, args);
203 static int fusb302_seq_show(struct seq_file *s, void *v)
205 struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
208 mutex_lock(&chip->logbuffer_lock);
209 tail = chip->logbuffer_tail;
210 while (tail != chip->logbuffer_head) {
211 seq_printf(s, "%s\n", chip->logbuffer[tail]);
212 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
214 if (!seq_has_overflowed(s))
215 chip->logbuffer_tail = tail;
216 mutex_unlock(&chip->logbuffer_lock);
221 static int fusb302_debug_open(struct inode *inode, struct file *file)
223 return single_open(file, fusb302_seq_show, inode->i_private);
226 static const struct file_operations fusb302_debug_operations = {
227 .open = fusb302_debug_open,
230 .release = single_release,
233 static struct dentry *rootdir;
235 static int fusb302_debugfs_init(struct fusb302_chip *chip)
237 mutex_init(&chip->logbuffer_lock);
239 rootdir = debugfs_create_dir("fusb302", NULL);
244 chip->dentry = debugfs_create_file(dev_name(chip->dev),
245 S_IFREG | 0444, rootdir,
246 chip, &fusb302_debug_operations);
251 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
253 debugfs_remove(chip->dentry);
258 static void fusb302_log(const struct fusb302_chip *chip,
259 const char *fmt, ...) { }
260 static int fusb302_debugfs_init(const struct fusb302_chip *chip) { return 0; }
261 static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
265 #define FUSB302_RESUME_RETRY 10
266 #define FUSB302_RESUME_RETRY_SLEEP 50
267 static int fusb302_i2c_write(struct fusb302_chip *chip,
273 atomic_set(&chip->i2c_busy, 1);
274 for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) {
275 if (atomic_read(&chip->pm_suspend)) {
276 pr_err("fusb302_i2c: pm suspend, retry %d/%d\n",
277 retry_cnt + 1, FUSB302_RESUME_RETRY);
278 msleep(FUSB302_RESUME_RETRY_SLEEP);
283 ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
285 fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
287 atomic_set(&chip->i2c_busy, 0);
292 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
293 u8 length, const u8 *data)
300 atomic_set(&chip->i2c_busy, 1);
301 for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) {
302 if (atomic_read(&chip->pm_suspend)) {
303 pr_err("fusb302_i2c: pm suspend, retry %d/%d\n",
304 retry_cnt + 1, FUSB302_RESUME_RETRY);
305 msleep(FUSB302_RESUME_RETRY_SLEEP);
310 ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
313 fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
314 address, length, ret);
315 atomic_set(&chip->i2c_busy, 0);
320 static int fusb302_i2c_read(struct fusb302_chip *chip,
321 u8 address, u8 *data)
326 atomic_set(&chip->i2c_busy, 1);
327 for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) {
328 if (atomic_read(&chip->pm_suspend)) {
329 pr_err("fusb302_i2c: pm suspend, retry %d/%d\n",
330 retry_cnt + 1, FUSB302_RESUME_RETRY);
331 msleep(FUSB302_RESUME_RETRY_SLEEP);
336 ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
339 fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
340 atomic_set(&chip->i2c_busy, 0);
345 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
353 atomic_set(&chip->i2c_busy, 1);
354 for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) {
355 if (atomic_read(&chip->pm_suspend)) {
356 pr_err("fusb302_i2c: pm suspend, retry %d/%d\n",
357 retry_cnt + 1, FUSB302_RESUME_RETRY);
358 msleep(FUSB302_RESUME_RETRY_SLEEP);
363 ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
366 fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
367 address, length, ret);
371 fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
372 ret, length, address);
375 atomic_set(&chip->i2c_busy, 0);
380 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
386 ret = fusb302_i2c_read(chip, address, &data);
391 ret = fusb302_i2c_write(chip, address, data);
398 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
401 return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
404 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
407 return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
410 static int fusb302_sw_reset(struct fusb302_chip *chip)
414 ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
415 FUSB_REG_RESET_SW_RESET);
417 fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
419 fusb302_log(chip, "sw reset");
424 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip)
428 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
429 FUSB_REG_CONTROL3_N_RETRIES_3 |
430 FUSB_REG_CONTROL3_AUTO_RETRY);
436 * initialize interrupt on the chip
437 * - unmasked interrupt: VBUS_OK
439 static int fusb302_init_interrupt(struct fusb302_chip *chip)
443 ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
444 0xFF & ~FUSB_REG_MASK_VBUSOK);
447 ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
450 ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
453 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
454 FUSB_REG_CONTROL0_INT_MASK);
461 static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
465 ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
470 static int tcpm_init(struct tcpc_dev *dev)
472 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
477 ret = fusb302_sw_reset(chip);
480 ret = fusb302_enable_tx_auto_retries(chip);
483 ret = fusb302_init_interrupt(chip);
486 ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
489 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
492 chip->vbus_present = !!(FUSB_REG_STATUS0 & FUSB_REG_STATUS0_VBUSOK);
493 ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
496 fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
501 static int tcpm_get_vbus(struct tcpc_dev *dev)
503 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
507 mutex_lock(&chip->lock);
508 ret = chip->vbus_present ? 1 : 0;
509 mutex_unlock(&chip->lock);
514 static int fusb302_set_cc_pull(struct fusb302_chip *chip,
515 bool pull_up, bool pull_down)
519 u8 mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
520 FUSB_REG_SWITCHES0_CC2_PU_EN |
521 FUSB_REG_SWITCHES0_CC1_PD_EN |
522 FUSB_REG_SWITCHES0_CC2_PD_EN;
525 data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
526 FUSB_REG_SWITCHES0_CC1_PU_EN :
527 FUSB_REG_SWITCHES0_CC2_PU_EN;
529 data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
530 FUSB_REG_SWITCHES0_CC2_PD_EN;
531 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
535 chip->pull_up = pull_up;
540 static int fusb302_set_src_current(struct fusb302_chip *chip,
541 enum src_current_status status)
545 chip->src_current_status = status;
547 case SRC_CURRENT_DEFAULT:
548 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
549 FUSB_REG_CONTROL0_HOST_CUR_MASK,
550 FUSB_REG_CONTROL0_HOST_CUR_DEF);
552 case SRC_CURRENT_MEDIUM:
553 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
554 FUSB_REG_CONTROL0_HOST_CUR_MASK,
555 FUSB_REG_CONTROL0_HOST_CUR_MED);
557 case SRC_CURRENT_HIGH:
558 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
559 FUSB_REG_CONTROL0_HOST_CUR_MASK,
560 FUSB_REG_CONTROL0_HOST_CUR_HIGH);
569 static int fusb302_set_toggling(struct fusb302_chip *chip,
570 enum toggling_mode mode)
574 /* first disable toggling */
575 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
576 FUSB_REG_CONTROL2_TOGGLE);
579 /* mask interrupts for SRC or SNK */
580 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
581 FUSB_REG_MASK_BC_LVL |
582 FUSB_REG_MASK_COMP_CHNG);
585 chip->intr_bc_lvl = false;
586 chip->intr_comp_chng = false;
587 /* configure toggling mode: none/snk/src/drp */
589 case TOGGLINE_MODE_OFF:
590 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
591 FUSB_REG_CONTROL2_MODE_MASK,
592 FUSB_REG_CONTROL2_MODE_NONE);
596 case TOGGLING_MODE_SNK:
597 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
598 FUSB_REG_CONTROL2_MODE_MASK,
599 FUSB_REG_CONTROL2_MODE_UFP);
603 case TOGGLING_MODE_SRC:
604 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
605 FUSB_REG_CONTROL2_MODE_MASK,
606 FUSB_REG_CONTROL2_MODE_DFP);
610 case TOGGLING_MODE_DRP:
611 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
612 FUSB_REG_CONTROL2_MODE_MASK,
613 FUSB_REG_CONTROL2_MODE_DRP);
621 if (mode == TOGGLINE_MODE_OFF) {
622 /* mask TOGDONE interrupt */
623 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
624 FUSB_REG_MASKA_TOGDONE);
627 chip->intr_togdone = false;
629 /* unmask TOGDONE interrupt */
630 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
631 FUSB_REG_MASKA_TOGDONE);
634 chip->intr_togdone = true;
636 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
637 FUSB_REG_CONTROL2_TOGGLE);
640 /* during toggling, consider cc as Open */
641 chip->cc1 = TYPEC_CC_OPEN;
642 chip->cc2 = TYPEC_CC_OPEN;
644 chip->toggling_mode = mode;
649 static const char * const typec_cc_status_name[] = {
650 [TYPEC_CC_OPEN] = "Open",
651 [TYPEC_CC_RA] = "Ra",
652 [TYPEC_CC_RD] = "Rd",
653 [TYPEC_CC_RP_DEF] = "Rp-def",
654 [TYPEC_CC_RP_1_5] = "Rp-1.5",
655 [TYPEC_CC_RP_3_0] = "Rp-3.0",
658 static const enum src_current_status cc_src_current[] = {
659 [TYPEC_CC_OPEN] = SRC_CURRENT_DEFAULT,
660 [TYPEC_CC_RA] = SRC_CURRENT_DEFAULT,
661 [TYPEC_CC_RD] = SRC_CURRENT_DEFAULT,
662 [TYPEC_CC_RP_DEF] = SRC_CURRENT_DEFAULT,
663 [TYPEC_CC_RP_1_5] = SRC_CURRENT_MEDIUM,
664 [TYPEC_CC_RP_3_0] = SRC_CURRENT_HIGH,
667 static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
669 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
672 bool pull_up, pull_down;
675 mutex_lock(&chip->lock);
685 case TYPEC_CC_RP_DEF:
686 case TYPEC_CC_RP_1_5:
687 case TYPEC_CC_RP_3_0:
692 fusb302_log(chip, "unsupported cc value %s",
693 typec_cc_status_name[cc]);
697 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
699 fusb302_log(chip, "cannot stop toggling, ret=%d", ret);
702 ret = fusb302_set_cc_pull(chip, pull_up, pull_down);
705 "cannot set cc pulling up %s, down %s, ret = %d",
706 pull_up ? "True" : "False",
707 pull_down ? "True" : "False",
711 /* reset the cc status */
712 chip->cc1 = TYPEC_CC_OPEN;
713 chip->cc2 = TYPEC_CC_OPEN;
714 /* adjust current for SRC */
716 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
718 fusb302_log(chip, "cannot set src current %s, ret=%d",
719 typec_cc_status_name[cc], ret);
723 /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
725 rd_mda = rd_mda_value[cc_src_current[cc]];
726 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
729 "cannot set SRC measure value, ret=%d",
733 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
734 FUSB_REG_MASK_BC_LVL |
735 FUSB_REG_MASK_COMP_CHNG,
736 FUSB_REG_MASK_COMP_CHNG);
738 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
742 chip->intr_bc_lvl = false;
743 chip->intr_comp_chng = true;
746 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
747 FUSB_REG_MASK_BC_LVL |
748 FUSB_REG_MASK_COMP_CHNG,
749 FUSB_REG_MASK_BC_LVL);
751 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
755 chip->intr_bc_lvl = true;
756 chip->intr_comp_chng = false;
758 fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
760 mutex_unlock(&chip->lock);
765 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
766 enum typec_cc_status *cc2)
768 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
771 mutex_lock(&chip->lock);
774 fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
775 typec_cc_status_name[*cc2]);
776 mutex_unlock(&chip->lock);
781 static int tcpm_set_polarity(struct tcpc_dev *dev,
782 enum typec_cc_polarity polarity)
787 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
789 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
792 u8 switches0_data = 0x00;
793 u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
794 FUSB_REG_SWITCHES0_VCONN_CC2;
796 mutex_lock(&chip->lock);
797 if (chip->vconn_on == on) {
798 fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
802 switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
803 FUSB_REG_SWITCHES0_VCONN_CC2 :
804 FUSB_REG_SWITCHES0_VCONN_CC1;
806 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
807 switches0_mask, switches0_data);
811 fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
813 mutex_unlock(&chip->lock);
818 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
820 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
824 mutex_lock(&chip->lock);
825 if (chip->vbus_on == on) {
826 fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
829 ret = regulator_enable(chip->vbus);
831 ret = regulator_disable(chip->vbus);
833 fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
834 on ? "enable" : "disable", ret);
838 fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
840 if (chip->charge_on == charge)
841 fusb302_log(chip, "charge is already %s",
842 charge ? "On" : "Off");
844 chip->charge_on = charge;
847 mutex_unlock(&chip->lock);
852 static int tcpm_set_current_limit(struct tcpc_dev *dev, u32 max_ma, u32 mv)
854 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
857 fusb302_log(chip, "current limit: %d ma, %d mv (not implemented)",
863 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
865 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
866 FUSB_REG_CONTROL0_TX_FLUSH);
869 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
871 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
872 FUSB_REG_CONTROL1_RX_FLUSH);
875 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
878 return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
879 FUSB_REG_SWITCHES1_AUTO_GCRC);
880 return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
881 FUSB_REG_SWITCHES1_AUTO_GCRC);
884 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
887 u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
888 u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
889 FUSB_REG_MASKA_HARDSENT |
890 FUSB_REG_MASKA_TX_SUCCESS |
891 FUSB_REG_MASKA_HARDRESET;
892 u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
895 fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
896 fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
900 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
901 fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
905 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
906 fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
910 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
912 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
916 mutex_lock(&chip->lock);
917 ret = fusb302_pd_rx_flush(chip);
919 fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
922 ret = fusb302_pd_tx_flush(chip);
924 fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
927 ret = fusb302_pd_set_auto_goodcrc(chip, on);
929 fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
930 on ? "on" : "off", ret);
933 ret = fusb302_pd_set_interrupts(chip, on);
935 fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
936 on ? "on" : "off", ret);
939 fusb302_log(chip, "pd := %s", on ? "on" : "off");
941 mutex_unlock(&chip->lock);
946 static const char * const typec_role_name[] = {
947 [TYPEC_SINK] = "Sink",
948 [TYPEC_SOURCE] = "Source",
951 static const char * const typec_data_role_name[] = {
952 [TYPEC_DEVICE] = "Device",
953 [TYPEC_HOST] = "Host",
956 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
957 enum typec_role pwr, enum typec_data_role data)
959 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
962 u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
963 FUSB_REG_SWITCHES1_DATAROLE;
964 u8 switches1_data = 0x00;
966 mutex_lock(&chip->lock);
967 if (pwr == TYPEC_SOURCE)
968 switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
969 if (data == TYPEC_HOST)
970 switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
971 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
972 switches1_mask, switches1_data);
974 fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
975 typec_role_name[pwr], typec_data_role_name[data],
979 fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
980 typec_data_role_name[data]);
982 mutex_unlock(&chip->lock);
987 static int tcpm_start_drp_toggling(struct tcpc_dev *dev,
988 enum typec_cc_status cc)
990 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
994 mutex_lock(&chip->lock);
995 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
997 fusb302_log(chip, "unable to set src current %s, ret=%d",
998 typec_cc_status_name[cc], ret);
1001 ret = fusb302_set_toggling(chip, TOGGLING_MODE_DRP);
1004 "unable to start drp toggling, ret=%d", ret);
1007 fusb302_log(chip, "start drp toggling");
1009 mutex_unlock(&chip->lock);
1014 static int fusb302_pd_send_message(struct fusb302_chip *chip,
1015 const struct pd_message *msg)
1023 buf[pos++] = FUSB302_TKN_SYNC1;
1024 buf[pos++] = FUSB302_TKN_SYNC1;
1025 buf[pos++] = FUSB302_TKN_SYNC1;
1026 buf[pos++] = FUSB302_TKN_SYNC2;
1028 len = pd_header_cnt(msg->header) * 4;
1029 /* plug 2 for header */
1033 "PD message too long %d (incl. header)", len);
1036 /* packsym tells the FUSB302 chip that the next X bytes are payload */
1037 buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
1038 buf[pos++] = msg->header & 0xFF;
1039 buf[pos++] = (msg->header >> 8) & 0xFF;
1042 memcpy(&buf[pos], msg->payload, len);
1046 buf[pos++] = FUSB302_TKN_JAMCRC;
1048 buf[pos++] = FUSB302_TKN_EOP;
1049 /* turn tx off after sending message */
1050 buf[pos++] = FUSB302_TKN_TXOFF;
1051 /* start transmission */
1052 buf[pos++] = FUSB302_TKN_TXON;
1054 ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
1057 fusb302_log(chip, "sending PD message header: %x", msg->header);
1058 fusb302_log(chip, "sending PD message len: %d", len);
1063 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1065 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1066 FUSB_REG_CONTROL3_SEND_HARDRESET);
1069 static const char * const transmit_type_name[] = {
1070 [TCPC_TX_SOP] = "SOP",
1071 [TCPC_TX_SOP_PRIME] = "SOP'",
1072 [TCPC_TX_SOP_PRIME_PRIME] = "SOP''",
1073 [TCPC_TX_SOP_DEBUG_PRIME] = "DEBUG'",
1074 [TCPC_TX_SOP_DEBUG_PRIME_PRIME] = "DEBUG''",
1075 [TCPC_TX_HARD_RESET] = "HARD_RESET",
1076 [TCPC_TX_CABLE_RESET] = "CABLE_RESET",
1077 [TCPC_TX_BIST_MODE_2] = "BIST_MODE_2",
1080 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1081 const struct pd_message *msg)
1083 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1087 mutex_lock(&chip->lock);
1090 ret = fusb302_pd_send_message(chip, msg);
1093 "cannot send PD message, ret=%d", ret);
1095 case TCPC_TX_HARD_RESET:
1096 ret = fusb302_pd_send_hardreset(chip);
1099 "cannot send hardreset, ret=%d", ret);
1102 fusb302_log(chip, "type %s not supported",
1103 transmit_type_name[type]);
1106 mutex_unlock(&chip->lock);
1111 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1113 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1114 return TYPEC_CC_RP_3_0;
1115 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1116 return TYPEC_CC_RP_1_5;
1117 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1118 return TYPEC_CC_RP_DEF;
1119 return TYPEC_CC_OPEN;
1122 static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1124 struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1125 bc_lvl_handler.work);
1129 enum typec_cc_status cc_status;
1131 mutex_lock(&chip->lock);
1132 if (!chip->intr_bc_lvl) {
1133 fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1136 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1139 fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1140 if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1141 fusb302_log(chip, "CC activities detected, delay handling");
1142 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1143 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1146 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1147 cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1148 if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1149 if (chip->cc1 != cc_status) {
1150 fusb302_log(chip, "cc1: %s -> %s",
1151 typec_cc_status_name[chip->cc1],
1152 typec_cc_status_name[cc_status]);
1153 chip->cc1 = cc_status;
1154 tcpm_cc_change(chip->tcpm_port);
1157 if (chip->cc2 != cc_status) {
1158 fusb302_log(chip, "cc2: %s -> %s",
1159 typec_cc_status_name[chip->cc2],
1160 typec_cc_status_name[cc_status]);
1161 chip->cc2 = cc_status;
1162 tcpm_cc_change(chip->tcpm_port);
1167 mutex_unlock(&chip->lock);
1170 #define PDO_FIXED_FLAGS \
1171 (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1173 static const u32 src_pdo[] = {
1174 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1177 static const u32 snk_pdo[] = {
1178 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1181 static const struct tcpc_config fusb302_tcpc_config = {
1183 .nr_src_pdo = ARRAY_SIZE(src_pdo),
1185 .nr_snk_pdo = ARRAY_SIZE(snk_pdo),
1188 .max_snk_mw = 27000,
1189 .operating_snk_mw = 2500,
1190 .type = TYPEC_PORT_DRP,
1191 .default_role = TYPEC_SINK,
1195 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1197 fusb302_tcpc_dev->config = &fusb302_tcpc_config;
1198 fusb302_tcpc_dev->init = tcpm_init;
1199 fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1200 fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1201 fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1202 fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1203 fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1204 fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1205 fusb302_tcpc_dev->set_current_limit = tcpm_set_current_limit;
1206 fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1207 fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1208 fusb302_tcpc_dev->start_drp_toggling = tcpm_start_drp_toggling;
1209 fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1210 fusb302_tcpc_dev->mux = NULL;
1213 static const char * const cc_polarity_name[] = {
1214 [TYPEC_POLARITY_CC1] = "Polarity_CC1",
1215 [TYPEC_POLARITY_CC2] = "Polarity_CC2",
1218 static int fusb302_set_cc_polarity(struct fusb302_chip *chip,
1219 enum typec_cc_polarity cc_polarity)
1222 u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
1223 FUSB_REG_SWITCHES0_CC2_PU_EN |
1224 FUSB_REG_SWITCHES0_VCONN_CC1 |
1225 FUSB_REG_SWITCHES0_VCONN_CC2 |
1226 FUSB_REG_SWITCHES0_MEAS_CC1 |
1227 FUSB_REG_SWITCHES0_MEAS_CC2;
1228 u8 switches0_data = 0x00;
1229 u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
1230 FUSB_REG_SWITCHES1_TXCC2_EN;
1231 u8 switches1_data = 0x00;
1233 if (cc_polarity == TYPEC_POLARITY_CC1) {
1234 switches0_data = FUSB_REG_SWITCHES0_MEAS_CC1;
1236 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1238 switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1239 switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1241 switches0_data = FUSB_REG_SWITCHES0_MEAS_CC2;
1243 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1245 switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1246 switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1248 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
1249 switches0_mask, switches0_data);
1252 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1253 switches1_mask, switches1_data);
1256 chip->cc_polarity = cc_polarity;
1261 static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1267 enum typec_cc_polarity cc_polarity;
1268 enum typec_cc_status cc_status_active, cc1, cc2;
1270 /* set pull_up, pull_down */
1271 ret = fusb302_set_cc_pull(chip, false, true);
1273 fusb302_log(chip, "cannot set cc to pull down, ret=%d", ret);
1277 cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1278 TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1279 ret = fusb302_set_cc_polarity(chip, cc_polarity);
1281 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1282 cc_polarity_name[cc_polarity], ret);
1285 /* fusb302_set_cc_polarity() has set the correct measure block */
1286 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1289 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1290 cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1291 /* restart toggling if the cc status on the active line is OPEN */
1292 if (cc_status_active == TYPEC_CC_OPEN) {
1293 fusb302_log(chip, "restart toggling as CC_OPEN detected");
1294 ret = fusb302_set_toggling(chip, chip->toggling_mode);
1297 /* update tcpm with the new cc value */
1298 cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1299 cc_status_active : TYPEC_CC_OPEN;
1300 cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1301 cc_status_active : TYPEC_CC_OPEN;
1302 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1305 tcpm_cc_change(chip->tcpm_port);
1307 /* turn off toggling */
1308 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
1311 "cannot set toggling mode off, ret=%d", ret);
1314 /* unmask bc_lvl interrupt */
1315 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1318 "cannot unmask bc_lcl interrupt, ret=%d", ret);
1321 chip->intr_bc_lvl = true;
1322 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1323 typec_cc_status_name[cc1],
1324 typec_cc_status_name[cc2]);
1329 static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1333 * - set polarity (measure cc, vconn, tx)
1334 * - set pull_up, pull_down
1335 * - set cc1, cc2, and update to tcpm_port
1336 * - set I_COMP interrupt on
1340 u8 ra_mda = ra_mda_value[chip->src_current_status];
1341 u8 rd_mda = rd_mda_value[chip->src_current_status];
1342 bool ra_comp, rd_comp;
1343 enum typec_cc_polarity cc_polarity;
1344 enum typec_cc_status cc_status_active, cc1, cc2;
1346 /* set pull_up, pull_down */
1347 ret = fusb302_set_cc_pull(chip, true, false);
1349 fusb302_log(chip, "cannot set cc to pull up, ret=%d", ret);
1353 cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1) ?
1354 TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1355 ret = fusb302_set_cc_polarity(chip, cc_polarity);
1357 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1358 cc_polarity_name[cc_polarity], ret);
1361 /* fusb302_set_cc_polarity() has set the correct measure block */
1362 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1365 usleep_range(50, 100);
1366 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1369 rd_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
1371 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1374 usleep_range(50, 100);
1375 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1378 ra_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
1381 cc_status_active = TYPEC_CC_OPEN;
1383 cc_status_active = TYPEC_CC_RD;
1385 /* Ra is not supported, report as Open */
1386 cc_status_active = TYPEC_CC_OPEN;
1387 /* restart toggling if the cc status on the active line is OPEN */
1388 if (cc_status_active == TYPEC_CC_OPEN) {
1389 fusb302_log(chip, "restart toggling as CC_OPEN detected");
1390 ret = fusb302_set_toggling(chip, chip->toggling_mode);
1393 /* update tcpm with the new cc value */
1394 cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1395 cc_status_active : TYPEC_CC_OPEN;
1396 cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1397 cc_status_active : TYPEC_CC_OPEN;
1398 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1401 tcpm_cc_change(chip->tcpm_port);
1403 /* turn off toggling */
1404 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
1407 "cannot set toggling mode off, ret=%d", ret);
1410 /* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1411 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1414 /* unmask comp_chng interrupt */
1415 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1416 FUSB_REG_MASK_COMP_CHNG);
1419 "cannot unmask bc_lcl interrupt, ret=%d", ret);
1422 chip->intr_comp_chng = true;
1423 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1424 typec_cc_status_name[cc1],
1425 typec_cc_status_name[cc2]);
1430 static int fusb302_handle_togdone(struct fusb302_chip *chip)
1436 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1439 togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1440 FUSB_REG_STATUS1A_TOGSS_MASK;
1441 switch (togdone_result) {
1442 case FUSB_REG_STATUS1A_TOGSS_SNK1:
1443 case FUSB_REG_STATUS1A_TOGSS_SNK2:
1444 return fusb302_handle_togdone_snk(chip, togdone_result);
1445 case FUSB_REG_STATUS1A_TOGSS_SRC1:
1446 case FUSB_REG_STATUS1A_TOGSS_SRC2:
1447 return fusb302_handle_togdone_src(chip, togdone_result);
1448 case FUSB_REG_STATUS1A_TOGSS_AA:
1449 /* doesn't support */
1450 fusb302_log(chip, "AudioAccessory not supported");
1451 fusb302_set_toggling(chip, chip->toggling_mode);
1454 fusb302_log(chip, "TOGDONE with an invalid state: %d",
1456 fusb302_set_toggling(chip, chip->toggling_mode);
1462 static int fusb302_pd_reset(struct fusb302_chip *chip)
1464 return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1465 FUSB_REG_RESET_PD_RESET);
1468 static int fusb302_pd_read_message(struct fusb302_chip *chip,
1469 struct pd_message *msg)
1476 /* first SOP token */
1477 ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1480 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1481 (u8 *)&msg->header);
1484 len = pd_header_cnt(msg->header) * 4;
1485 /* add 4 to length to include the CRC */
1486 if (len > PD_MAX_PAYLOAD * 4) {
1487 fusb302_log(chip, "PD message too long %d", len);
1491 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1492 (u8 *)msg->payload);
1496 /* another 4 bytes to read CRC out */
1497 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1500 fusb302_log(chip, "PD message header: %x", msg->header);
1501 fusb302_log(chip, "PD message len: %d", len);
1506 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1508 struct fusb302_chip *chip = dev_id;
1518 bool intr_comp_chng;
1519 struct pd_message pd_msg;
1521 mutex_lock(&chip->lock);
1522 /* grab a snapshot of intr flags */
1523 intr_togdone = chip->intr_togdone;
1524 intr_bc_lvl = chip->intr_bc_lvl;
1525 intr_comp_chng = chip->intr_comp_chng;
1527 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1530 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1533 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1536 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1540 "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1541 interrupt, interrupta, interruptb, status0);
1543 if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1544 vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1545 fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1546 vbus_present ? "On" : "Off");
1547 if (vbus_present != chip->vbus_present) {
1548 chip->vbus_present = vbus_present;
1549 tcpm_vbus_change(chip->tcpm_port);
1553 if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1554 fusb302_log(chip, "IRQ: TOGDONE");
1555 ret = fusb302_handle_togdone(chip);
1558 "handle togdone error, ret=%d", ret);
1563 if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1564 fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1566 * as BC_LVL interrupt can be affected by PD activity,
1567 * apply delay to for the handler to wait for the PD
1568 * signaling to finish.
1570 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1571 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1574 if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1575 comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1576 fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1577 comp_result ? "true" : "false");
1579 /* cc level > Rd_threashold, detach */
1580 if (chip->cc_polarity == TYPEC_POLARITY_CC1)
1581 chip->cc1 = TYPEC_CC_OPEN;
1583 chip->cc2 = TYPEC_CC_OPEN;
1584 tcpm_cc_change(chip->tcpm_port);
1588 if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1589 fusb302_log(chip, "IRQ: PD collision");
1590 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1593 if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1594 fusb302_log(chip, "IRQ: PD retry failed");
1595 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1598 if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1599 fusb302_log(chip, "IRQ: PD hardreset sent");
1600 ret = fusb302_pd_reset(chip);
1602 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1605 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1608 if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1609 fusb302_log(chip, "IRQ: PD tx success");
1610 /* read out the received good CRC */
1611 ret = fusb302_pd_read_message(chip, &pd_msg);
1613 fusb302_log(chip, "cannot read in GCRC, ret=%d", ret);
1616 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1619 if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1620 fusb302_log(chip, "IRQ: PD received hardreset");
1621 ret = fusb302_pd_reset(chip);
1623 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1626 tcpm_pd_hard_reset(chip->tcpm_port);
1629 if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1630 fusb302_log(chip, "IRQ: PD sent good CRC");
1631 ret = fusb302_pd_read_message(chip, &pd_msg);
1634 "cannot read in PD message, ret=%d", ret);
1637 tcpm_pd_receive(chip->tcpm_port, &pd_msg);
1640 mutex_unlock(&chip->lock);
1645 static int init_gpio(struct fusb302_chip *chip)
1647 struct device_node *node;
1650 node = chip->dev->of_node;
1651 chip->gpio_int_n = of_get_named_gpio(node, "fcs,int_n", 0);
1652 if (!gpio_is_valid(chip->gpio_int_n)) {
1653 ret = chip->gpio_int_n;
1654 fusb302_log(chip, "cannot get named GPIO Int_N, ret=%d", ret);
1657 ret = devm_gpio_request(chip->dev, chip->gpio_int_n, "fcs,int_n");
1659 fusb302_log(chip, "cannot request GPIO Int_N, ret=%d", ret);
1662 ret = gpio_direction_input(chip->gpio_int_n);
1665 "cannot set GPIO Int_N to input, ret=%d", ret);
1666 gpio_free(chip->gpio_int_n);
1669 ret = gpio_to_irq(chip->gpio_int_n);
1672 "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1673 gpio_free(chip->gpio_int_n);
1676 chip->gpio_int_n_irq = ret;
1680 static int fusb302_probe(struct i2c_client *client,
1681 const struct i2c_device_id *id)
1683 struct fusb302_chip *chip;
1684 struct i2c_adapter *adapter;
1687 adapter = to_i2c_adapter(client->dev.parent);
1688 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1689 dev_err(&client->dev,
1690 "I2C/SMBus block functionality not supported!\n");
1693 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1697 chip->i2c_client = client;
1698 i2c_set_clientdata(client, chip);
1699 chip->dev = &client->dev;
1700 mutex_init(&chip->lock);
1702 ret = fusb302_debugfs_init(chip);
1706 chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1709 goto clear_client_data;
1711 INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1712 init_tcpc_dev(&chip->tcpc_dev);
1714 chip->vbus = devm_regulator_get(chip->dev, "vbus");
1715 if (IS_ERR(chip->vbus)) {
1716 ret = PTR_ERR(chip->vbus);
1717 goto destroy_workqueue;
1720 ret = init_gpio(chip);
1722 goto destroy_workqueue;
1724 chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1725 if (IS_ERR(chip->tcpm_port)) {
1726 ret = PTR_ERR(chip->tcpm_port);
1727 fusb302_log(chip, "cannot register tcpm port, ret=%d", ret);
1728 goto destroy_workqueue;
1731 ret = devm_request_threaded_irq(chip->dev, chip->gpio_int_n_irq,
1732 NULL, fusb302_irq_intn,
1733 IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1734 "fsc_interrupt_int_n", chip);
1737 "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1738 goto tcpm_unregister_port;
1740 enable_irq_wake(chip->gpio_int_n_irq);
1743 tcpm_unregister_port:
1744 tcpm_unregister_port(chip->tcpm_port);
1746 destroy_workqueue(chip->wq);
1748 i2c_set_clientdata(client, NULL);
1749 fusb302_debugfs_exit(chip);
1754 static int fusb302_remove(struct i2c_client *client)
1756 struct fusb302_chip *chip = i2c_get_clientdata(client);
1758 tcpm_unregister_port(chip->tcpm_port);
1759 destroy_workqueue(chip->wq);
1760 i2c_set_clientdata(client, NULL);
1761 fusb302_debugfs_exit(chip);
1766 static int fusb302_pm_suspend(struct device *dev)
1768 struct fusb302_chip *chip = dev->driver_data;
1770 if (atomic_read(&chip->i2c_busy))
1772 atomic_set(&chip->pm_suspend, 1);
1777 static int fusb302_pm_resume(struct device *dev)
1779 struct fusb302_chip *chip = dev->driver_data;
1781 atomic_set(&chip->pm_suspend, 0);
1786 static const struct of_device_id fusb302_dt_match[] = {
1787 {.compatible = "fcs,fusb302"},
1791 static const struct i2c_device_id fusb302_i2c_device_id[] = {
1792 {"typec_fusb302", 0},
1796 static const struct dev_pm_ops fusb302_pm_ops = {
1797 .suspend = fusb302_pm_suspend,
1798 .resume = fusb302_pm_resume,
1801 static struct i2c_driver fusb302_driver = {
1803 .name = "typec_fusb302",
1804 .pm = &fusb302_pm_ops,
1805 .of_match_table = of_match_ptr(fusb302_dt_match),
1807 .probe = fusb302_probe,
1808 .remove = fusb302_remove,
1809 .id_table = fusb302_i2c_device_id,
1811 module_i2c_driver(fusb302_driver);
1813 MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1814 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1815 MODULE_LICENSE("GPL");