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/extcon.h>
21 #include <linux/gpio.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/of_device.h>
28 #include <linux/of_device.h>
29 #include <linux/of_gpio.h>
30 #include <linux/pinctrl/consumer.h>
31 #include <linux/power_supply.h>
32 #include <linux/proc_fs.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/sched/clock.h>
35 #include <linux/seq_file.h>
36 #include <linux/slab.h>
37 #include <linux/string.h>
38 #include <linux/types.h>
39 #include <linux/usb/typec.h>
40 #include <linux/workqueue.h>
42 #include "fusb302_reg.h"
47 * When the device is SNK, BC_LVL interrupt is used to monitor cc pins
48 * for the current capability offered by the SRC. As FUSB302 chip fires
49 * the BC_LVL interrupt on PD signalings, cc lvl should be handled after
50 * a delay to avoid measuring on PD activities. The delay is slightly
51 * longer than PD_T_PD_DEBPUNCE (10-20ms).
53 #define T_BC_LVL_DEBOUNCE_DELAY_MS 30
62 static const char * const toggling_mode_name[] = {
63 [TOGGLINE_MODE_OFF] = "toggling_OFF",
64 [TOGGLING_MODE_DRP] = "toggling_DRP",
65 [TOGGLING_MODE_SNK] = "toggling_SNK",
66 [TOGGLING_MODE_SRC] = "toggling_SRC",
69 enum src_current_status {
75 static const u8 ra_mda_value[] = {
76 [SRC_CURRENT_DEFAULT] = 4, /* 210mV */
77 [SRC_CURRENT_MEDIUM] = 9, /* 420mV */
78 [SRC_CURRENT_HIGH] = 18, /* 798mV */
81 static const u8 rd_mda_value[] = {
82 [SRC_CURRENT_DEFAULT] = 38, /* 1638mV */
83 [SRC_CURRENT_MEDIUM] = 38, /* 1638mV */
84 [SRC_CURRENT_HIGH] = 61, /* 2604mV */
87 #define LOG_BUFFER_ENTRIES 1024
88 #define LOG_BUFFER_ENTRY_SIZE 128
92 struct i2c_client *i2c_client;
93 struct tcpm_port *tcpm_port;
94 struct tcpc_dev tcpc_dev;
95 struct tcpc_config tcpc_config;
97 struct regulator *vbus;
101 struct extcon_dev *extcon;
103 struct workqueue_struct *wq;
104 struct delayed_work bc_lvl_handler;
109 /* lock for sharing chip states */
112 /* psy + psy status */
113 struct power_supply *psy;
118 enum toggling_mode toggling_mode;
119 enum src_current_status src_current_status;
130 enum typec_cc_polarity cc_polarity;
131 enum typec_cc_status cc1;
132 enum typec_cc_status cc2;
134 #ifdef CONFIG_DEBUG_FS
135 struct dentry *dentry;
136 /* lock for log buffer access */
137 struct mutex logbuffer_lock;
140 u8 *logbuffer[LOG_BUFFER_ENTRIES];
148 #ifdef CONFIG_DEBUG_FS
150 static bool fusb302_log_full(struct fusb302_chip *chip)
152 return chip->logbuffer_tail ==
153 (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
156 static void _fusb302_log(struct fusb302_chip *chip, const char *fmt,
159 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
160 u64 ts_nsec = local_clock();
161 unsigned long rem_nsec;
163 if (!chip->logbuffer[chip->logbuffer_head]) {
164 chip->logbuffer[chip->logbuffer_head] =
165 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
166 if (!chip->logbuffer[chip->logbuffer_head])
170 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
172 mutex_lock(&chip->logbuffer_lock);
174 if (fusb302_log_full(chip)) {
175 chip->logbuffer_head = max(chip->logbuffer_head - 1, 0);
176 strlcpy(tmpbuffer, "overflow", sizeof(tmpbuffer));
179 if (chip->logbuffer_head < 0 ||
180 chip->logbuffer_head >= LOG_BUFFER_ENTRIES) {
182 "Bad log buffer index %d\n", chip->logbuffer_head);
186 if (!chip->logbuffer[chip->logbuffer_head]) {
188 "Log buffer index %d is NULL\n", chip->logbuffer_head);
192 rem_nsec = do_div(ts_nsec, 1000000000);
193 scnprintf(chip->logbuffer[chip->logbuffer_head],
194 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
195 (unsigned long)ts_nsec, rem_nsec / 1000,
197 chip->logbuffer_head = (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
200 mutex_unlock(&chip->logbuffer_lock);
203 static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
208 _fusb302_log(chip, fmt, args);
212 static int fusb302_seq_show(struct seq_file *s, void *v)
214 struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
217 mutex_lock(&chip->logbuffer_lock);
218 tail = chip->logbuffer_tail;
219 while (tail != chip->logbuffer_head) {
220 seq_printf(s, "%s\n", chip->logbuffer[tail]);
221 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
223 if (!seq_has_overflowed(s))
224 chip->logbuffer_tail = tail;
225 mutex_unlock(&chip->logbuffer_lock);
230 static int fusb302_debug_open(struct inode *inode, struct file *file)
232 return single_open(file, fusb302_seq_show, inode->i_private);
235 static const struct file_operations fusb302_debug_operations = {
236 .open = fusb302_debug_open,
239 .release = single_release,
242 static struct dentry *rootdir;
244 static int fusb302_debugfs_init(struct fusb302_chip *chip)
246 mutex_init(&chip->logbuffer_lock);
248 rootdir = debugfs_create_dir("fusb302", NULL);
253 chip->dentry = debugfs_create_file(dev_name(chip->dev),
254 S_IFREG | 0444, rootdir,
255 chip, &fusb302_debug_operations);
260 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
262 debugfs_remove(chip->dentry);
267 static void fusb302_log(const struct fusb302_chip *chip,
268 const char *fmt, ...) { }
269 static int fusb302_debugfs_init(const struct fusb302_chip *chip) { return 0; }
270 static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
274 #define FUSB302_RESUME_RETRY 10
275 #define FUSB302_RESUME_RETRY_SLEEP 50
277 static bool fusb302_is_suspended(struct fusb302_chip *chip)
281 for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) {
282 if (atomic_read(&chip->pm_suspend)) {
283 dev_err(chip->dev, "i2c: pm suspend, retry %d/%d\n",
284 retry_cnt + 1, FUSB302_RESUME_RETRY);
285 msleep(FUSB302_RESUME_RETRY_SLEEP);
294 static int fusb302_i2c_write(struct fusb302_chip *chip,
299 atomic_set(&chip->i2c_busy, 1);
301 if (fusb302_is_suspended(chip)) {
302 atomic_set(&chip->i2c_busy, 0);
306 ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
308 fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
310 atomic_set(&chip->i2c_busy, 0);
315 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
316 u8 length, const u8 *data)
322 atomic_set(&chip->i2c_busy, 1);
324 if (fusb302_is_suspended(chip)) {
325 atomic_set(&chip->i2c_busy, 0);
329 ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
332 fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
333 address, length, ret);
334 atomic_set(&chip->i2c_busy, 0);
339 static int fusb302_i2c_read(struct fusb302_chip *chip,
340 u8 address, u8 *data)
344 atomic_set(&chip->i2c_busy, 1);
346 if (fusb302_is_suspended(chip)) {
347 atomic_set(&chip->i2c_busy, 0);
351 ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
354 fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
355 atomic_set(&chip->i2c_busy, 0);
360 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
367 atomic_set(&chip->i2c_busy, 1);
369 if (fusb302_is_suspended(chip)) {
370 atomic_set(&chip->i2c_busy, 0);
374 ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
377 fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
378 address, length, ret);
382 fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
383 ret, length, address);
388 atomic_set(&chip->i2c_busy, 0);
393 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
399 ret = fusb302_i2c_read(chip, address, &data);
404 ret = fusb302_i2c_write(chip, address, data);
411 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
414 return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
417 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
420 return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
423 static int fusb302_sw_reset(struct fusb302_chip *chip)
427 ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
428 FUSB_REG_RESET_SW_RESET);
430 fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
432 fusb302_log(chip, "sw reset");
437 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip)
441 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
442 FUSB_REG_CONTROL3_N_RETRIES_3 |
443 FUSB_REG_CONTROL3_AUTO_RETRY);
449 * initialize interrupt on the chip
450 * - unmasked interrupt: VBUS_OK
452 static int fusb302_init_interrupt(struct fusb302_chip *chip)
456 ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
457 0xFF & ~FUSB_REG_MASK_VBUSOK);
460 ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
463 ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
466 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
467 FUSB_REG_CONTROL0_INT_MASK);
474 static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
478 ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
483 static int tcpm_init(struct tcpc_dev *dev)
485 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
490 ret = fusb302_sw_reset(chip);
493 ret = fusb302_enable_tx_auto_retries(chip);
496 ret = fusb302_init_interrupt(chip);
499 ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
502 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
505 chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
506 ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
509 fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
514 static int tcpm_get_vbus(struct tcpc_dev *dev)
516 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
520 mutex_lock(&chip->lock);
521 ret = chip->vbus_present ? 1 : 0;
522 mutex_unlock(&chip->lock);
527 static int tcpm_get_current_limit(struct tcpc_dev *dev)
529 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
531 int current_limit = 0;
532 unsigned long timeout;
538 * USB2 Charger detection may still be in progress when we get here,
539 * this can take upto 600ms, wait 800ms max.
541 timeout = jiffies + msecs_to_jiffies(800);
543 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_SDP) == 1)
546 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_CDP) == 1 ||
547 extcon_get_state(chip->extcon, EXTCON_CHG_USB_ACA) == 1)
548 current_limit = 1500;
550 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_DCP) == 1)
551 current_limit = 2000;
554 } while (current_limit == 0 && time_before(jiffies, timeout));
556 return current_limit;
559 static int fusb302_set_cc_pull(struct fusb302_chip *chip,
560 bool pull_up, bool pull_down)
564 u8 mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
565 FUSB_REG_SWITCHES0_CC2_PU_EN |
566 FUSB_REG_SWITCHES0_CC1_PD_EN |
567 FUSB_REG_SWITCHES0_CC2_PD_EN;
570 data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
571 FUSB_REG_SWITCHES0_CC1_PU_EN :
572 FUSB_REG_SWITCHES0_CC2_PU_EN;
574 data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
575 FUSB_REG_SWITCHES0_CC2_PD_EN;
576 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
580 chip->pull_up = pull_up;
585 static int fusb302_set_src_current(struct fusb302_chip *chip,
586 enum src_current_status status)
590 chip->src_current_status = status;
592 case SRC_CURRENT_DEFAULT:
593 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
594 FUSB_REG_CONTROL0_HOST_CUR_MASK,
595 FUSB_REG_CONTROL0_HOST_CUR_DEF);
597 case SRC_CURRENT_MEDIUM:
598 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
599 FUSB_REG_CONTROL0_HOST_CUR_MASK,
600 FUSB_REG_CONTROL0_HOST_CUR_MED);
602 case SRC_CURRENT_HIGH:
603 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
604 FUSB_REG_CONTROL0_HOST_CUR_MASK,
605 FUSB_REG_CONTROL0_HOST_CUR_HIGH);
614 static int fusb302_set_toggling(struct fusb302_chip *chip,
615 enum toggling_mode mode)
619 /* first disable toggling */
620 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
621 FUSB_REG_CONTROL2_TOGGLE);
624 /* mask interrupts for SRC or SNK */
625 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
626 FUSB_REG_MASK_BC_LVL |
627 FUSB_REG_MASK_COMP_CHNG);
630 chip->intr_bc_lvl = false;
631 chip->intr_comp_chng = false;
632 /* configure toggling mode: none/snk/src/drp */
634 case TOGGLINE_MODE_OFF:
635 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
636 FUSB_REG_CONTROL2_MODE_MASK,
637 FUSB_REG_CONTROL2_MODE_NONE);
641 case TOGGLING_MODE_SNK:
642 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
643 FUSB_REG_CONTROL2_MODE_MASK,
644 FUSB_REG_CONTROL2_MODE_UFP);
648 case TOGGLING_MODE_SRC:
649 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
650 FUSB_REG_CONTROL2_MODE_MASK,
651 FUSB_REG_CONTROL2_MODE_DFP);
655 case TOGGLING_MODE_DRP:
656 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
657 FUSB_REG_CONTROL2_MODE_MASK,
658 FUSB_REG_CONTROL2_MODE_DRP);
666 if (mode == TOGGLINE_MODE_OFF) {
667 /* mask TOGDONE interrupt */
668 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
669 FUSB_REG_MASKA_TOGDONE);
672 chip->intr_togdone = false;
674 /* Datasheet says vconn MUST be off when toggling */
675 WARN(chip->vconn_on, "Vconn is on during toggle start");
676 /* unmask TOGDONE interrupt */
677 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
678 FUSB_REG_MASKA_TOGDONE);
681 chip->intr_togdone = true;
683 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
684 FUSB_REG_CONTROL2_TOGGLE);
687 /* during toggling, consider cc as Open */
688 chip->cc1 = TYPEC_CC_OPEN;
689 chip->cc2 = TYPEC_CC_OPEN;
691 chip->toggling_mode = mode;
696 static const char * const typec_cc_status_name[] = {
697 [TYPEC_CC_OPEN] = "Open",
698 [TYPEC_CC_RA] = "Ra",
699 [TYPEC_CC_RD] = "Rd",
700 [TYPEC_CC_RP_DEF] = "Rp-def",
701 [TYPEC_CC_RP_1_5] = "Rp-1.5",
702 [TYPEC_CC_RP_3_0] = "Rp-3.0",
705 static const enum src_current_status cc_src_current[] = {
706 [TYPEC_CC_OPEN] = SRC_CURRENT_DEFAULT,
707 [TYPEC_CC_RA] = SRC_CURRENT_DEFAULT,
708 [TYPEC_CC_RD] = SRC_CURRENT_DEFAULT,
709 [TYPEC_CC_RP_DEF] = SRC_CURRENT_DEFAULT,
710 [TYPEC_CC_RP_1_5] = SRC_CURRENT_MEDIUM,
711 [TYPEC_CC_RP_3_0] = SRC_CURRENT_HIGH,
714 static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
716 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
719 bool pull_up, pull_down;
722 mutex_lock(&chip->lock);
732 case TYPEC_CC_RP_DEF:
733 case TYPEC_CC_RP_1_5:
734 case TYPEC_CC_RP_3_0:
739 fusb302_log(chip, "unsupported cc value %s",
740 typec_cc_status_name[cc]);
744 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
746 fusb302_log(chip, "cannot stop toggling, ret=%d", ret);
749 ret = fusb302_set_cc_pull(chip, pull_up, pull_down);
752 "cannot set cc pulling up %s, down %s, ret = %d",
753 pull_up ? "True" : "False",
754 pull_down ? "True" : "False",
758 /* reset the cc status */
759 chip->cc1 = TYPEC_CC_OPEN;
760 chip->cc2 = TYPEC_CC_OPEN;
761 /* adjust current for SRC */
763 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
765 fusb302_log(chip, "cannot set src current %s, ret=%d",
766 typec_cc_status_name[cc], ret);
770 /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
772 rd_mda = rd_mda_value[cc_src_current[cc]];
773 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
776 "cannot set SRC measure value, ret=%d",
780 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
781 FUSB_REG_MASK_BC_LVL |
782 FUSB_REG_MASK_COMP_CHNG,
783 FUSB_REG_MASK_COMP_CHNG);
785 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
789 chip->intr_bc_lvl = false;
790 chip->intr_comp_chng = true;
793 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
794 FUSB_REG_MASK_BC_LVL |
795 FUSB_REG_MASK_COMP_CHNG,
796 FUSB_REG_MASK_BC_LVL);
798 fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
802 chip->intr_bc_lvl = true;
803 chip->intr_comp_chng = false;
805 fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
807 mutex_unlock(&chip->lock);
812 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
813 enum typec_cc_status *cc2)
815 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
818 mutex_lock(&chip->lock);
821 fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
822 typec_cc_status_name[*cc2]);
823 mutex_unlock(&chip->lock);
828 static int tcpm_set_polarity(struct tcpc_dev *dev,
829 enum typec_cc_polarity polarity)
834 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
836 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
839 u8 switches0_data = 0x00;
840 u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
841 FUSB_REG_SWITCHES0_VCONN_CC2;
843 mutex_lock(&chip->lock);
844 if (chip->vconn_on == on) {
845 fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
849 switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
850 FUSB_REG_SWITCHES0_VCONN_CC2 :
851 FUSB_REG_SWITCHES0_VCONN_CC1;
853 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
854 switches0_mask, switches0_data);
858 fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
860 mutex_unlock(&chip->lock);
865 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
867 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
871 mutex_lock(&chip->lock);
872 if (chip->vbus_on == on) {
873 fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
876 ret = regulator_enable(chip->vbus);
878 ret = regulator_disable(chip->vbus);
880 fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
881 on ? "enable" : "disable", ret);
885 fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
887 if (chip->charge_on == charge) {
888 fusb302_log(chip, "charge is already %s",
889 charge ? "On" : "Off");
891 chip->charge_on = charge;
892 power_supply_changed(chip->psy);
896 mutex_unlock(&chip->lock);
901 static int tcpm_set_current_limit(struct tcpc_dev *dev, u32 max_ma, u32 mv)
903 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
906 fusb302_log(chip, "current limit: %d ma, %d mv (not implemented)",
909 chip->supply_voltage = mv;
910 chip->current_limit = max_ma;
912 power_supply_changed(chip->psy);
917 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
919 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
920 FUSB_REG_CONTROL0_TX_FLUSH);
923 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
925 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
926 FUSB_REG_CONTROL1_RX_FLUSH);
929 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
932 return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
933 FUSB_REG_SWITCHES1_AUTO_GCRC);
934 return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
935 FUSB_REG_SWITCHES1_AUTO_GCRC);
938 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
941 u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
942 u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
943 FUSB_REG_MASKA_HARDSENT |
944 FUSB_REG_MASKA_TX_SUCCESS |
945 FUSB_REG_MASKA_HARDRESET;
946 u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
949 fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
950 fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
954 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
955 fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
959 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
960 fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
964 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
966 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
970 mutex_lock(&chip->lock);
971 ret = fusb302_pd_rx_flush(chip);
973 fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
976 ret = fusb302_pd_tx_flush(chip);
978 fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
981 ret = fusb302_pd_set_auto_goodcrc(chip, on);
983 fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
984 on ? "on" : "off", ret);
987 ret = fusb302_pd_set_interrupts(chip, on);
989 fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
990 on ? "on" : "off", ret);
993 fusb302_log(chip, "pd := %s", on ? "on" : "off");
995 mutex_unlock(&chip->lock);
1000 static const char * const typec_role_name[] = {
1001 [TYPEC_SINK] = "Sink",
1002 [TYPEC_SOURCE] = "Source",
1005 static const char * const typec_data_role_name[] = {
1006 [TYPEC_DEVICE] = "Device",
1007 [TYPEC_HOST] = "Host",
1010 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
1011 enum typec_role pwr, enum typec_data_role data)
1013 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1016 u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
1017 FUSB_REG_SWITCHES1_DATAROLE;
1018 u8 switches1_data = 0x00;
1020 mutex_lock(&chip->lock);
1021 if (pwr == TYPEC_SOURCE)
1022 switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
1023 if (data == TYPEC_HOST)
1024 switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
1025 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1026 switches1_mask, switches1_data);
1028 fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
1029 typec_role_name[pwr], typec_data_role_name[data],
1033 fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
1034 typec_data_role_name[data]);
1036 mutex_unlock(&chip->lock);
1041 static int tcpm_start_drp_toggling(struct tcpc_dev *dev,
1042 enum typec_cc_status cc)
1044 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1048 mutex_lock(&chip->lock);
1049 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
1051 fusb302_log(chip, "unable to set src current %s, ret=%d",
1052 typec_cc_status_name[cc], ret);
1055 ret = fusb302_set_toggling(chip, TOGGLING_MODE_DRP);
1058 "unable to start drp toggling, ret=%d", ret);
1061 fusb302_log(chip, "start drp toggling");
1063 mutex_unlock(&chip->lock);
1068 static int fusb302_pd_send_message(struct fusb302_chip *chip,
1069 const struct pd_message *msg)
1077 buf[pos++] = FUSB302_TKN_SYNC1;
1078 buf[pos++] = FUSB302_TKN_SYNC1;
1079 buf[pos++] = FUSB302_TKN_SYNC1;
1080 buf[pos++] = FUSB302_TKN_SYNC2;
1082 len = pd_header_cnt_le(msg->header) * 4;
1083 /* plug 2 for header */
1087 "PD message too long %d (incl. header)", len);
1090 /* packsym tells the FUSB302 chip that the next X bytes are payload */
1091 buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
1092 memcpy(&buf[pos], &msg->header, sizeof(msg->header));
1093 pos += sizeof(msg->header);
1096 memcpy(&buf[pos], msg->payload, len);
1100 buf[pos++] = FUSB302_TKN_JAMCRC;
1102 buf[pos++] = FUSB302_TKN_EOP;
1103 /* turn tx off after sending message */
1104 buf[pos++] = FUSB302_TKN_TXOFF;
1105 /* start transmission */
1106 buf[pos++] = FUSB302_TKN_TXON;
1108 ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
1111 fusb302_log(chip, "sending PD message header: %x", msg->header);
1112 fusb302_log(chip, "sending PD message len: %d", len);
1117 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1119 return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1120 FUSB_REG_CONTROL3_SEND_HARDRESET);
1123 static const char * const transmit_type_name[] = {
1124 [TCPC_TX_SOP] = "SOP",
1125 [TCPC_TX_SOP_PRIME] = "SOP'",
1126 [TCPC_TX_SOP_PRIME_PRIME] = "SOP''",
1127 [TCPC_TX_SOP_DEBUG_PRIME] = "DEBUG'",
1128 [TCPC_TX_SOP_DEBUG_PRIME_PRIME] = "DEBUG''",
1129 [TCPC_TX_HARD_RESET] = "HARD_RESET",
1130 [TCPC_TX_CABLE_RESET] = "CABLE_RESET",
1131 [TCPC_TX_BIST_MODE_2] = "BIST_MODE_2",
1134 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1135 const struct pd_message *msg)
1137 struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1141 mutex_lock(&chip->lock);
1144 ret = fusb302_pd_send_message(chip, msg);
1147 "cannot send PD message, ret=%d", ret);
1149 case TCPC_TX_HARD_RESET:
1150 ret = fusb302_pd_send_hardreset(chip);
1153 "cannot send hardreset, ret=%d", ret);
1156 fusb302_log(chip, "type %s not supported",
1157 transmit_type_name[type]);
1160 mutex_unlock(&chip->lock);
1165 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1167 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1168 return TYPEC_CC_RP_3_0;
1169 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1170 return TYPEC_CC_RP_1_5;
1171 if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1172 return TYPEC_CC_RP_DEF;
1173 return TYPEC_CC_OPEN;
1176 static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1178 struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1179 bc_lvl_handler.work);
1183 enum typec_cc_status cc_status;
1185 mutex_lock(&chip->lock);
1186 if (!chip->intr_bc_lvl) {
1187 fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1190 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1193 fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1194 if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1195 fusb302_log(chip, "CC activities detected, delay handling");
1196 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1197 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1200 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1201 cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1202 if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1203 if (chip->cc1 != cc_status) {
1204 fusb302_log(chip, "cc1: %s -> %s",
1205 typec_cc_status_name[chip->cc1],
1206 typec_cc_status_name[cc_status]);
1207 chip->cc1 = cc_status;
1208 tcpm_cc_change(chip->tcpm_port);
1211 if (chip->cc2 != cc_status) {
1212 fusb302_log(chip, "cc2: %s -> %s",
1213 typec_cc_status_name[chip->cc2],
1214 typec_cc_status_name[cc_status]);
1215 chip->cc2 = cc_status;
1216 tcpm_cc_change(chip->tcpm_port);
1221 mutex_unlock(&chip->lock);
1224 #define PDO_FIXED_FLAGS \
1225 (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1227 static const u32 src_pdo[] = {
1228 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1231 static const u32 snk_pdo[] = {
1232 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1235 static const struct tcpc_config fusb302_tcpc_config = {
1237 .nr_src_pdo = ARRAY_SIZE(src_pdo),
1239 .nr_snk_pdo = ARRAY_SIZE(snk_pdo),
1242 .max_snk_mw = 15000,
1243 .operating_snk_mw = 2500,
1244 .type = TYPEC_PORT_DRP,
1245 .default_role = TYPEC_SINK,
1249 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1251 fusb302_tcpc_dev->init = tcpm_init;
1252 fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1253 fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
1254 fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1255 fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1256 fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1257 fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1258 fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1259 fusb302_tcpc_dev->set_current_limit = tcpm_set_current_limit;
1260 fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1261 fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1262 fusb302_tcpc_dev->start_drp_toggling = tcpm_start_drp_toggling;
1263 fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1264 fusb302_tcpc_dev->mux = NULL;
1267 static const char * const cc_polarity_name[] = {
1268 [TYPEC_POLARITY_CC1] = "Polarity_CC1",
1269 [TYPEC_POLARITY_CC2] = "Polarity_CC2",
1272 static int fusb302_set_cc_polarity(struct fusb302_chip *chip,
1273 enum typec_cc_polarity cc_polarity)
1276 u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
1277 FUSB_REG_SWITCHES0_CC2_PU_EN |
1278 FUSB_REG_SWITCHES0_VCONN_CC1 |
1279 FUSB_REG_SWITCHES0_VCONN_CC2 |
1280 FUSB_REG_SWITCHES0_MEAS_CC1 |
1281 FUSB_REG_SWITCHES0_MEAS_CC2;
1282 u8 switches0_data = 0x00;
1283 u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
1284 FUSB_REG_SWITCHES1_TXCC2_EN;
1285 u8 switches1_data = 0x00;
1287 if (cc_polarity == TYPEC_POLARITY_CC1) {
1288 switches0_data = FUSB_REG_SWITCHES0_MEAS_CC1;
1290 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1292 switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1293 switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1295 switches0_data = FUSB_REG_SWITCHES0_MEAS_CC2;
1297 switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1299 switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1300 switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1302 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
1303 switches0_mask, switches0_data);
1306 ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1307 switches1_mask, switches1_data);
1310 chip->cc_polarity = cc_polarity;
1315 static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1321 enum typec_cc_polarity cc_polarity;
1322 enum typec_cc_status cc_status_active, cc1, cc2;
1324 /* set pull_up, pull_down */
1325 ret = fusb302_set_cc_pull(chip, false, true);
1327 fusb302_log(chip, "cannot set cc to pull down, ret=%d", ret);
1331 cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1332 TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1333 ret = fusb302_set_cc_polarity(chip, cc_polarity);
1335 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1336 cc_polarity_name[cc_polarity], ret);
1339 /* fusb302_set_cc_polarity() has set the correct measure block */
1340 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1343 bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1344 cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1345 /* restart toggling if the cc status on the active line is OPEN */
1346 if (cc_status_active == TYPEC_CC_OPEN) {
1347 fusb302_log(chip, "restart toggling as CC_OPEN detected");
1348 ret = fusb302_set_toggling(chip, chip->toggling_mode);
1351 /* update tcpm with the new cc value */
1352 cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1353 cc_status_active : TYPEC_CC_OPEN;
1354 cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1355 cc_status_active : TYPEC_CC_OPEN;
1356 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1359 tcpm_cc_change(chip->tcpm_port);
1361 /* turn off toggling */
1362 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
1365 "cannot set toggling mode off, ret=%d", ret);
1368 /* unmask bc_lvl interrupt */
1369 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1372 "cannot unmask bc_lcl interrupt, ret=%d", ret);
1375 chip->intr_bc_lvl = true;
1376 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1377 typec_cc_status_name[cc1],
1378 typec_cc_status_name[cc2]);
1383 static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1387 * - set polarity (measure cc, vconn, tx)
1388 * - set pull_up, pull_down
1389 * - set cc1, cc2, and update to tcpm_port
1390 * - set I_COMP interrupt on
1394 u8 ra_mda = ra_mda_value[chip->src_current_status];
1395 u8 rd_mda = rd_mda_value[chip->src_current_status];
1396 bool ra_comp, rd_comp;
1397 enum typec_cc_polarity cc_polarity;
1398 enum typec_cc_status cc_status_active, cc1, cc2;
1400 /* set pull_up, pull_down */
1401 ret = fusb302_set_cc_pull(chip, true, false);
1403 fusb302_log(chip, "cannot set cc to pull up, ret=%d", ret);
1407 cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1) ?
1408 TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1409 ret = fusb302_set_cc_polarity(chip, cc_polarity);
1411 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1412 cc_polarity_name[cc_polarity], ret);
1415 /* fusb302_set_cc_polarity() has set the correct measure block */
1416 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1419 usleep_range(50, 100);
1420 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1423 rd_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
1425 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1428 usleep_range(50, 100);
1429 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1432 ra_comp = !!(status0 & FUSB_REG_STATUS0_COMP);
1435 cc_status_active = TYPEC_CC_OPEN;
1437 cc_status_active = TYPEC_CC_RD;
1439 /* Ra is not supported, report as Open */
1440 cc_status_active = TYPEC_CC_OPEN;
1441 /* restart toggling if the cc status on the active line is OPEN */
1442 if (cc_status_active == TYPEC_CC_OPEN) {
1443 fusb302_log(chip, "restart toggling as CC_OPEN detected");
1444 ret = fusb302_set_toggling(chip, chip->toggling_mode);
1447 /* update tcpm with the new cc value */
1448 cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1449 cc_status_active : TYPEC_CC_OPEN;
1450 cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1451 cc_status_active : TYPEC_CC_OPEN;
1452 if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1455 tcpm_cc_change(chip->tcpm_port);
1457 /* turn off toggling */
1458 ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
1461 "cannot set toggling mode off, ret=%d", ret);
1464 /* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1465 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1468 /* unmask comp_chng interrupt */
1469 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1470 FUSB_REG_MASK_COMP_CHNG);
1473 "cannot unmask bc_lcl interrupt, ret=%d", ret);
1476 chip->intr_comp_chng = true;
1477 fusb302_log(chip, "detected cc1=%s, cc2=%s",
1478 typec_cc_status_name[cc1],
1479 typec_cc_status_name[cc2]);
1484 static int fusb302_handle_togdone(struct fusb302_chip *chip)
1490 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1493 togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1494 FUSB_REG_STATUS1A_TOGSS_MASK;
1495 switch (togdone_result) {
1496 case FUSB_REG_STATUS1A_TOGSS_SNK1:
1497 case FUSB_REG_STATUS1A_TOGSS_SNK2:
1498 return fusb302_handle_togdone_snk(chip, togdone_result);
1499 case FUSB_REG_STATUS1A_TOGSS_SRC1:
1500 case FUSB_REG_STATUS1A_TOGSS_SRC2:
1501 return fusb302_handle_togdone_src(chip, togdone_result);
1502 case FUSB_REG_STATUS1A_TOGSS_AA:
1503 /* doesn't support */
1504 fusb302_log(chip, "AudioAccessory not supported");
1505 fusb302_set_toggling(chip, chip->toggling_mode);
1508 fusb302_log(chip, "TOGDONE with an invalid state: %d",
1510 fusb302_set_toggling(chip, chip->toggling_mode);
1516 static int fusb302_pd_reset(struct fusb302_chip *chip)
1518 return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1519 FUSB_REG_RESET_PD_RESET);
1522 static int fusb302_pd_read_message(struct fusb302_chip *chip,
1523 struct pd_message *msg)
1530 /* first SOP token */
1531 ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1534 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1535 (u8 *)&msg->header);
1538 len = pd_header_cnt_le(msg->header) * 4;
1539 /* add 4 to length to include the CRC */
1540 if (len > PD_MAX_PAYLOAD * 4) {
1541 fusb302_log(chip, "PD message too long %d", len);
1545 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1546 (u8 *)msg->payload);
1550 /* another 4 bytes to read CRC out */
1551 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1554 fusb302_log(chip, "PD message header: %x", msg->header);
1555 fusb302_log(chip, "PD message len: %d", len);
1558 * Check if we've read off a GoodCRC message. If so then indicate to
1559 * TCPM that the previous transmission has completed. Otherwise we pass
1560 * the received message over to TCPM for processing.
1562 * We make this check here instead of basing the reporting decision on
1563 * the IRQ event type, as it's possible for the chip to report the
1564 * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
1565 * to check the message type to ensure correct reporting to TCPM.
1567 if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
1568 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1570 tcpm_pd_receive(chip->tcpm_port, msg);
1575 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1577 struct fusb302_chip *chip = dev_id;
1587 bool intr_comp_chng;
1588 struct pd_message pd_msg;
1590 mutex_lock(&chip->lock);
1591 /* grab a snapshot of intr flags */
1592 intr_togdone = chip->intr_togdone;
1593 intr_bc_lvl = chip->intr_bc_lvl;
1594 intr_comp_chng = chip->intr_comp_chng;
1596 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1599 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1602 ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1605 ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1609 "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1610 interrupt, interrupta, interruptb, status0);
1612 if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1613 vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1614 fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1615 vbus_present ? "On" : "Off");
1616 if (vbus_present != chip->vbus_present) {
1617 chip->vbus_present = vbus_present;
1618 tcpm_vbus_change(chip->tcpm_port);
1622 if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1623 fusb302_log(chip, "IRQ: TOGDONE");
1624 ret = fusb302_handle_togdone(chip);
1627 "handle togdone error, ret=%d", ret);
1632 if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1633 fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1635 * as BC_LVL interrupt can be affected by PD activity,
1636 * apply delay to for the handler to wait for the PD
1637 * signaling to finish.
1639 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1640 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1643 if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1644 comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1645 fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1646 comp_result ? "true" : "false");
1648 /* cc level > Rd_threashold, detach */
1649 if (chip->cc_polarity == TYPEC_POLARITY_CC1)
1650 chip->cc1 = TYPEC_CC_OPEN;
1652 chip->cc2 = TYPEC_CC_OPEN;
1653 tcpm_cc_change(chip->tcpm_port);
1657 if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1658 fusb302_log(chip, "IRQ: PD collision");
1659 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1662 if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1663 fusb302_log(chip, "IRQ: PD retry failed");
1664 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1667 if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1668 fusb302_log(chip, "IRQ: PD hardreset sent");
1669 ret = fusb302_pd_reset(chip);
1671 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1674 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1677 if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1678 fusb302_log(chip, "IRQ: PD tx success");
1679 ret = fusb302_pd_read_message(chip, &pd_msg);
1682 "cannot read in PD message, ret=%d", ret);
1687 if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1688 fusb302_log(chip, "IRQ: PD received hardreset");
1689 ret = fusb302_pd_reset(chip);
1691 fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1694 tcpm_pd_hard_reset(chip->tcpm_port);
1697 if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1698 fusb302_log(chip, "IRQ: PD sent good CRC");
1699 ret = fusb302_pd_read_message(chip, &pd_msg);
1702 "cannot read in PD message, ret=%d", ret);
1707 mutex_unlock(&chip->lock);
1712 static int fusb302_psy_get_property(struct power_supply *psy,
1713 enum power_supply_property psp,
1714 union power_supply_propval *val)
1716 struct fusb302_chip *chip = power_supply_get_drvdata(psy);
1719 case POWER_SUPPLY_PROP_ONLINE:
1720 val->intval = chip->charge_on;
1722 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1723 val->intval = chip->supply_voltage * 1000; /* mV -> µV */
1725 case POWER_SUPPLY_PROP_CURRENT_MAX:
1726 val->intval = chip->current_limit * 1000; /* mA -> µA */
1735 static enum power_supply_property fusb302_psy_properties[] = {
1736 POWER_SUPPLY_PROP_ONLINE,
1737 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1738 POWER_SUPPLY_PROP_CURRENT_MAX,
1741 static const struct power_supply_desc fusb302_psy_desc = {
1742 .name = "fusb302-typec-source",
1743 .type = POWER_SUPPLY_TYPE_USB_TYPE_C,
1744 .properties = fusb302_psy_properties,
1745 .num_properties = ARRAY_SIZE(fusb302_psy_properties),
1746 .get_property = fusb302_psy_get_property,
1749 static int init_gpio(struct fusb302_chip *chip)
1751 struct device_node *node;
1754 node = chip->dev->of_node;
1755 chip->gpio_int_n = of_get_named_gpio(node, "fcs,int_n", 0);
1756 if (!gpio_is_valid(chip->gpio_int_n)) {
1757 ret = chip->gpio_int_n;
1758 fusb302_log(chip, "cannot get named GPIO Int_N, ret=%d", ret);
1761 ret = devm_gpio_request(chip->dev, chip->gpio_int_n, "fcs,int_n");
1763 fusb302_log(chip, "cannot request GPIO Int_N, ret=%d", ret);
1766 ret = gpio_direction_input(chip->gpio_int_n);
1769 "cannot set GPIO Int_N to input, ret=%d", ret);
1772 ret = gpio_to_irq(chip->gpio_int_n);
1775 "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1778 chip->gpio_int_n_irq = ret;
1782 static int fusb302_probe(struct i2c_client *client,
1783 const struct i2c_device_id *id)
1785 struct fusb302_chip *chip;
1786 struct i2c_adapter *adapter;
1787 struct device *dev = &client->dev;
1788 struct power_supply_config cfg = {};
1793 adapter = to_i2c_adapter(client->dev.parent);
1794 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1795 dev_err(&client->dev,
1796 "I2C/SMBus block functionality not supported!\n");
1799 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1803 chip->i2c_client = client;
1804 i2c_set_clientdata(client, chip);
1805 chip->dev = &client->dev;
1806 chip->tcpc_config = fusb302_tcpc_config;
1807 chip->tcpc_dev.config = &chip->tcpc_config;
1808 mutex_init(&chip->lock);
1810 if (!device_property_read_u32(dev, "fcs,max-sink-microvolt", &v))
1811 chip->tcpc_config.max_snk_mv = v / 1000;
1813 if (!device_property_read_u32(dev, "fcs,max-sink-microamp", &v))
1814 chip->tcpc_config.max_snk_ma = v / 1000;
1816 if (!device_property_read_u32(dev, "fcs,max-sink-microwatt", &v))
1817 chip->tcpc_config.max_snk_mw = v / 1000;
1819 if (!device_property_read_u32(dev, "fcs,operating-sink-microwatt", &v))
1820 chip->tcpc_config.operating_snk_mw = v / 1000;
1823 * Devicetree platforms should get extcon via phandle (not yet
1824 * supported). On ACPI platforms, we get the name from a device prop.
1825 * This device prop is for kernel internal use only and is expected
1826 * to be set by the platform code which also registers the i2c client
1829 if (device_property_read_string(dev, "fcs,extcon-name", &name) == 0) {
1830 chip->extcon = extcon_get_extcon_dev(name);
1832 return -EPROBE_DEFER;
1835 cfg.drv_data = chip;
1836 chip->psy = devm_power_supply_register(dev, &fusb302_psy_desc, &cfg);
1837 if (IS_ERR(chip->psy)) {
1838 ret = PTR_ERR(chip->psy);
1839 dev_err(chip->dev, "Error registering power-supply: %d\n", ret);
1843 ret = fusb302_debugfs_init(chip);
1847 chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1850 goto clear_client_data;
1852 INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1853 init_tcpc_dev(&chip->tcpc_dev);
1855 chip->vbus = devm_regulator_get(chip->dev, "vbus");
1856 if (IS_ERR(chip->vbus)) {
1857 ret = PTR_ERR(chip->vbus);
1858 goto destroy_workqueue;
1862 chip->gpio_int_n_irq = client->irq;
1864 ret = init_gpio(chip);
1866 goto destroy_workqueue;
1869 chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1870 if (IS_ERR(chip->tcpm_port)) {
1871 ret = PTR_ERR(chip->tcpm_port);
1872 fusb302_log(chip, "cannot register tcpm port, ret=%d", ret);
1873 goto destroy_workqueue;
1876 ret = devm_request_threaded_irq(chip->dev, chip->gpio_int_n_irq,
1877 NULL, fusb302_irq_intn,
1878 IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1879 "fsc_interrupt_int_n", chip);
1882 "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1883 goto tcpm_unregister_port;
1885 enable_irq_wake(chip->gpio_int_n_irq);
1888 tcpm_unregister_port:
1889 tcpm_unregister_port(chip->tcpm_port);
1891 destroy_workqueue(chip->wq);
1893 i2c_set_clientdata(client, NULL);
1894 fusb302_debugfs_exit(chip);
1899 static int fusb302_remove(struct i2c_client *client)
1901 struct fusb302_chip *chip = i2c_get_clientdata(client);
1903 tcpm_unregister_port(chip->tcpm_port);
1904 destroy_workqueue(chip->wq);
1905 i2c_set_clientdata(client, NULL);
1906 fusb302_debugfs_exit(chip);
1911 static int fusb302_pm_suspend(struct device *dev)
1913 struct fusb302_chip *chip = dev->driver_data;
1915 if (atomic_read(&chip->i2c_busy))
1917 atomic_set(&chip->pm_suspend, 1);
1922 static int fusb302_pm_resume(struct device *dev)
1924 struct fusb302_chip *chip = dev->driver_data;
1926 atomic_set(&chip->pm_suspend, 0);
1931 static const struct of_device_id fusb302_dt_match[] = {
1932 {.compatible = "fcs,fusb302"},
1935 MODULE_DEVICE_TABLE(of, fusb302_dt_match);
1937 static const struct i2c_device_id fusb302_i2c_device_id[] = {
1938 {"typec_fusb302", 0},
1941 MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
1943 static const struct dev_pm_ops fusb302_pm_ops = {
1944 .suspend = fusb302_pm_suspend,
1945 .resume = fusb302_pm_resume,
1948 static struct i2c_driver fusb302_driver = {
1950 .name = "typec_fusb302",
1951 .pm = &fusb302_pm_ops,
1952 .of_match_table = of_match_ptr(fusb302_dt_match),
1954 .probe = fusb302_probe,
1955 .remove = fusb302_remove,
1956 .id_table = fusb302_i2c_device_id,
1958 module_i2c_driver(fusb302_driver);
1960 MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1961 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1962 MODULE_LICENSE("GPL");