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
268 static bool fusb302_is_suspended(struct fusb302_chip
*chip
)
272 for (retry_cnt
= 0; retry_cnt
< FUSB302_RESUME_RETRY
; retry_cnt
++) {
273 if (atomic_read(&chip
->pm_suspend
)) {
274 dev_err(chip
->dev
, "i2c: pm suspend, retry %d/%d\n",
275 retry_cnt
+ 1, FUSB302_RESUME_RETRY
);
276 msleep(FUSB302_RESUME_RETRY_SLEEP
);
285 static int fusb302_i2c_write(struct fusb302_chip
*chip
,
290 atomic_set(&chip
->i2c_busy
, 1);
292 if (fusb302_is_suspended(chip
)) {
293 atomic_set(&chip
->i2c_busy
, 0);
297 ret
= i2c_smbus_write_byte_data(chip
->i2c_client
, address
, data
);
299 fusb302_log(chip
, "cannot write 0x%02x to 0x%02x, ret=%d",
301 atomic_set(&chip
->i2c_busy
, 0);
306 static int fusb302_i2c_block_write(struct fusb302_chip
*chip
, u8 address
,
307 u8 length
, const u8
*data
)
313 atomic_set(&chip
->i2c_busy
, 1);
315 if (fusb302_is_suspended(chip
)) {
316 atomic_set(&chip
->i2c_busy
, 0);
320 ret
= i2c_smbus_write_i2c_block_data(chip
->i2c_client
, address
,
323 fusb302_log(chip
, "cannot block write 0x%02x, len=%d, ret=%d",
324 address
, length
, ret
);
325 atomic_set(&chip
->i2c_busy
, 0);
330 static int fusb302_i2c_read(struct fusb302_chip
*chip
,
331 u8 address
, u8
*data
)
335 atomic_set(&chip
->i2c_busy
, 1);
337 if (fusb302_is_suspended(chip
)) {
338 atomic_set(&chip
->i2c_busy
, 0);
342 ret
= i2c_smbus_read_byte_data(chip
->i2c_client
, address
);
345 fusb302_log(chip
, "cannot read %02x, ret=%d", address
, ret
);
346 atomic_set(&chip
->i2c_busy
, 0);
351 static int fusb302_i2c_block_read(struct fusb302_chip
*chip
, u8 address
,
358 atomic_set(&chip
->i2c_busy
, 1);
360 if (fusb302_is_suspended(chip
)) {
361 atomic_set(&chip
->i2c_busy
, 0);
365 ret
= i2c_smbus_read_i2c_block_data(chip
->i2c_client
, address
,
368 fusb302_log(chip
, "cannot block read 0x%02x, len=%d, ret=%d",
369 address
, length
, ret
);
373 fusb302_log(chip
, "only read %d/%d bytes from 0x%02x",
374 ret
, length
, address
);
379 atomic_set(&chip
->i2c_busy
, 0);
384 static int fusb302_i2c_mask_write(struct fusb302_chip
*chip
, u8 address
,
390 ret
= fusb302_i2c_read(chip
, address
, &data
);
395 ret
= fusb302_i2c_write(chip
, address
, data
);
402 static int fusb302_i2c_set_bits(struct fusb302_chip
*chip
, u8 address
,
405 return fusb302_i2c_mask_write(chip
, address
, 0x00, set_bits
);
408 static int fusb302_i2c_clear_bits(struct fusb302_chip
*chip
, u8 address
,
411 return fusb302_i2c_mask_write(chip
, address
, clear_bits
, 0x00);
414 static int fusb302_sw_reset(struct fusb302_chip
*chip
)
418 ret
= fusb302_i2c_write(chip
, FUSB_REG_RESET
,
419 FUSB_REG_RESET_SW_RESET
);
421 fusb302_log(chip
, "cannot sw reset the chip, ret=%d", ret
);
423 fusb302_log(chip
, "sw reset");
428 static int fusb302_enable_tx_auto_retries(struct fusb302_chip
*chip
)
432 ret
= fusb302_i2c_set_bits(chip
, FUSB_REG_CONTROL3
,
433 FUSB_REG_CONTROL3_N_RETRIES_3
|
434 FUSB_REG_CONTROL3_AUTO_RETRY
);
440 * initialize interrupt on the chip
441 * - unmasked interrupt: VBUS_OK
443 static int fusb302_init_interrupt(struct fusb302_chip
*chip
)
447 ret
= fusb302_i2c_write(chip
, FUSB_REG_MASK
,
448 0xFF & ~FUSB_REG_MASK_VBUSOK
);
451 ret
= fusb302_i2c_write(chip
, FUSB_REG_MASKA
, 0xFF);
454 ret
= fusb302_i2c_write(chip
, FUSB_REG_MASKB
, 0xFF);
457 ret
= fusb302_i2c_clear_bits(chip
, FUSB_REG_CONTROL0
,
458 FUSB_REG_CONTROL0_INT_MASK
);
465 static int fusb302_set_power_mode(struct fusb302_chip
*chip
, u8 power_mode
)
469 ret
= fusb302_i2c_write(chip
, FUSB_REG_POWER
, power_mode
);
474 static int tcpm_init(struct tcpc_dev
*dev
)
476 struct fusb302_chip
*chip
= container_of(dev
, struct fusb302_chip
,
481 ret
= fusb302_sw_reset(chip
);
484 ret
= fusb302_enable_tx_auto_retries(chip
);
487 ret
= fusb302_init_interrupt(chip
);
490 ret
= fusb302_set_power_mode(chip
, FUSB_REG_POWER_PWR_ALL
);
493 ret
= fusb302_i2c_read(chip
, FUSB_REG_STATUS0
, &data
);
496 chip
->vbus_present
= !!(data
& FUSB_REG_STATUS0_VBUSOK
);
497 ret
= fusb302_i2c_read(chip
, FUSB_REG_DEVICE_ID
, &data
);
500 fusb302_log(chip
, "fusb302 device ID: 0x%02x", data
);
505 static int tcpm_get_vbus(struct tcpc_dev
*dev
)
507 struct fusb302_chip
*chip
= container_of(dev
, struct fusb302_chip
,
511 mutex_lock(&chip
->lock
);
512 ret
= chip
->vbus_present
? 1 : 0;
513 mutex_unlock(&chip
->lock
);
518 static int fusb302_set_cc_pull(struct fusb302_chip
*chip
,
519 bool pull_up
, bool pull_down
)
523 u8 mask
= FUSB_REG_SWITCHES0_CC1_PU_EN
|
524 FUSB_REG_SWITCHES0_CC2_PU_EN
|
525 FUSB_REG_SWITCHES0_CC1_PD_EN
|
526 FUSB_REG_SWITCHES0_CC2_PD_EN
;
529 data
|= (chip
->cc_polarity
== TYPEC_POLARITY_CC1
) ?
530 FUSB_REG_SWITCHES0_CC1_PU_EN
:
531 FUSB_REG_SWITCHES0_CC2_PU_EN
;
533 data
|= FUSB_REG_SWITCHES0_CC1_PD_EN
|
534 FUSB_REG_SWITCHES0_CC2_PD_EN
;
535 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_SWITCHES0
,
539 chip
->pull_up
= pull_up
;
544 static int fusb302_set_src_current(struct fusb302_chip
*chip
,
545 enum src_current_status status
)
549 chip
->src_current_status
= status
;
551 case SRC_CURRENT_DEFAULT
:
552 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_CONTROL0
,
553 FUSB_REG_CONTROL0_HOST_CUR_MASK
,
554 FUSB_REG_CONTROL0_HOST_CUR_DEF
);
556 case SRC_CURRENT_MEDIUM
:
557 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_CONTROL0
,
558 FUSB_REG_CONTROL0_HOST_CUR_MASK
,
559 FUSB_REG_CONTROL0_HOST_CUR_MED
);
561 case SRC_CURRENT_HIGH
:
562 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_CONTROL0
,
563 FUSB_REG_CONTROL0_HOST_CUR_MASK
,
564 FUSB_REG_CONTROL0_HOST_CUR_HIGH
);
573 static int fusb302_set_toggling(struct fusb302_chip
*chip
,
574 enum toggling_mode mode
)
578 /* first disable toggling */
579 ret
= fusb302_i2c_clear_bits(chip
, FUSB_REG_CONTROL2
,
580 FUSB_REG_CONTROL2_TOGGLE
);
583 /* mask interrupts for SRC or SNK */
584 ret
= fusb302_i2c_set_bits(chip
, FUSB_REG_MASK
,
585 FUSB_REG_MASK_BC_LVL
|
586 FUSB_REG_MASK_COMP_CHNG
);
589 chip
->intr_bc_lvl
= false;
590 chip
->intr_comp_chng
= false;
591 /* configure toggling mode: none/snk/src/drp */
593 case TOGGLINE_MODE_OFF
:
594 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_CONTROL2
,
595 FUSB_REG_CONTROL2_MODE_MASK
,
596 FUSB_REG_CONTROL2_MODE_NONE
);
600 case TOGGLING_MODE_SNK
:
601 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_CONTROL2
,
602 FUSB_REG_CONTROL2_MODE_MASK
,
603 FUSB_REG_CONTROL2_MODE_UFP
);
607 case TOGGLING_MODE_SRC
:
608 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_CONTROL2
,
609 FUSB_REG_CONTROL2_MODE_MASK
,
610 FUSB_REG_CONTROL2_MODE_DFP
);
614 case TOGGLING_MODE_DRP
:
615 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_CONTROL2
,
616 FUSB_REG_CONTROL2_MODE_MASK
,
617 FUSB_REG_CONTROL2_MODE_DRP
);
625 if (mode
== TOGGLINE_MODE_OFF
) {
626 /* mask TOGDONE interrupt */
627 ret
= fusb302_i2c_set_bits(chip
, FUSB_REG_MASKA
,
628 FUSB_REG_MASKA_TOGDONE
);
631 chip
->intr_togdone
= false;
633 /* unmask TOGDONE interrupt */
634 ret
= fusb302_i2c_clear_bits(chip
, FUSB_REG_MASKA
,
635 FUSB_REG_MASKA_TOGDONE
);
638 chip
->intr_togdone
= true;
640 ret
= fusb302_i2c_set_bits(chip
, FUSB_REG_CONTROL2
,
641 FUSB_REG_CONTROL2_TOGGLE
);
644 /* during toggling, consider cc as Open */
645 chip
->cc1
= TYPEC_CC_OPEN
;
646 chip
->cc2
= TYPEC_CC_OPEN
;
648 chip
->toggling_mode
= mode
;
653 static const char * const typec_cc_status_name
[] = {
654 [TYPEC_CC_OPEN
] = "Open",
655 [TYPEC_CC_RA
] = "Ra",
656 [TYPEC_CC_RD
] = "Rd",
657 [TYPEC_CC_RP_DEF
] = "Rp-def",
658 [TYPEC_CC_RP_1_5
] = "Rp-1.5",
659 [TYPEC_CC_RP_3_0
] = "Rp-3.0",
662 static const enum src_current_status cc_src_current
[] = {
663 [TYPEC_CC_OPEN
] = SRC_CURRENT_DEFAULT
,
664 [TYPEC_CC_RA
] = SRC_CURRENT_DEFAULT
,
665 [TYPEC_CC_RD
] = SRC_CURRENT_DEFAULT
,
666 [TYPEC_CC_RP_DEF
] = SRC_CURRENT_DEFAULT
,
667 [TYPEC_CC_RP_1_5
] = SRC_CURRENT_MEDIUM
,
668 [TYPEC_CC_RP_3_0
] = SRC_CURRENT_HIGH
,
671 static int tcpm_set_cc(struct tcpc_dev
*dev
, enum typec_cc_status cc
)
673 struct fusb302_chip
*chip
= container_of(dev
, struct fusb302_chip
,
676 bool pull_up
, pull_down
;
679 mutex_lock(&chip
->lock
);
689 case TYPEC_CC_RP_DEF
:
690 case TYPEC_CC_RP_1_5
:
691 case TYPEC_CC_RP_3_0
:
696 fusb302_log(chip
, "unsupported cc value %s",
697 typec_cc_status_name
[cc
]);
701 ret
= fusb302_set_toggling(chip
, TOGGLINE_MODE_OFF
);
703 fusb302_log(chip
, "cannot stop toggling, ret=%d", ret
);
706 ret
= fusb302_set_cc_pull(chip
, pull_up
, pull_down
);
709 "cannot set cc pulling up %s, down %s, ret = %d",
710 pull_up
? "True" : "False",
711 pull_down
? "True" : "False",
715 /* reset the cc status */
716 chip
->cc1
= TYPEC_CC_OPEN
;
717 chip
->cc2
= TYPEC_CC_OPEN
;
718 /* adjust current for SRC */
720 ret
= fusb302_set_src_current(chip
, cc_src_current
[cc
]);
722 fusb302_log(chip
, "cannot set src current %s, ret=%d",
723 typec_cc_status_name
[cc
], ret
);
727 /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
729 rd_mda
= rd_mda_value
[cc_src_current
[cc
]];
730 ret
= fusb302_i2c_write(chip
, FUSB_REG_MEASURE
, rd_mda
);
733 "cannot set SRC measure value, ret=%d",
737 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_MASK
,
738 FUSB_REG_MASK_BC_LVL
|
739 FUSB_REG_MASK_COMP_CHNG
,
740 FUSB_REG_MASK_COMP_CHNG
);
742 fusb302_log(chip
, "cannot set SRC interrupt, ret=%d",
746 chip
->intr_bc_lvl
= false;
747 chip
->intr_comp_chng
= true;
750 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_MASK
,
751 FUSB_REG_MASK_BC_LVL
|
752 FUSB_REG_MASK_COMP_CHNG
,
753 FUSB_REG_MASK_BC_LVL
);
755 fusb302_log(chip
, "cannot set SRC interrupt, ret=%d",
759 chip
->intr_bc_lvl
= true;
760 chip
->intr_comp_chng
= false;
762 fusb302_log(chip
, "cc := %s", typec_cc_status_name
[cc
]);
764 mutex_unlock(&chip
->lock
);
769 static int tcpm_get_cc(struct tcpc_dev
*dev
, enum typec_cc_status
*cc1
,
770 enum typec_cc_status
*cc2
)
772 struct fusb302_chip
*chip
= container_of(dev
, struct fusb302_chip
,
775 mutex_lock(&chip
->lock
);
778 fusb302_log(chip
, "cc1=%s, cc2=%s", typec_cc_status_name
[*cc1
],
779 typec_cc_status_name
[*cc2
]);
780 mutex_unlock(&chip
->lock
);
785 static int tcpm_set_polarity(struct tcpc_dev
*dev
,
786 enum typec_cc_polarity polarity
)
791 static int tcpm_set_vconn(struct tcpc_dev
*dev
, bool on
)
793 struct fusb302_chip
*chip
= container_of(dev
, struct fusb302_chip
,
796 u8 switches0_data
= 0x00;
797 u8 switches0_mask
= FUSB_REG_SWITCHES0_VCONN_CC1
|
798 FUSB_REG_SWITCHES0_VCONN_CC2
;
800 mutex_lock(&chip
->lock
);
801 if (chip
->vconn_on
== on
) {
802 fusb302_log(chip
, "vconn is already %s", on
? "On" : "Off");
806 switches0_data
= (chip
->cc_polarity
== TYPEC_POLARITY_CC1
) ?
807 FUSB_REG_SWITCHES0_VCONN_CC2
:
808 FUSB_REG_SWITCHES0_VCONN_CC1
;
810 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_SWITCHES0
,
811 switches0_mask
, switches0_data
);
815 fusb302_log(chip
, "vconn := %s", on
? "On" : "Off");
817 mutex_unlock(&chip
->lock
);
822 static int tcpm_set_vbus(struct tcpc_dev
*dev
, bool on
, bool charge
)
824 struct fusb302_chip
*chip
= container_of(dev
, struct fusb302_chip
,
828 mutex_lock(&chip
->lock
);
829 if (chip
->vbus_on
== on
) {
830 fusb302_log(chip
, "vbus is already %s", on
? "On" : "Off");
833 ret
= regulator_enable(chip
->vbus
);
835 ret
= regulator_disable(chip
->vbus
);
837 fusb302_log(chip
, "cannot %s vbus regulator, ret=%d",
838 on
? "enable" : "disable", ret
);
842 fusb302_log(chip
, "vbus := %s", on
? "On" : "Off");
844 if (chip
->charge_on
== charge
)
845 fusb302_log(chip
, "charge is already %s",
846 charge
? "On" : "Off");
848 chip
->charge_on
= charge
;
851 mutex_unlock(&chip
->lock
);
856 static int tcpm_set_current_limit(struct tcpc_dev
*dev
, u32 max_ma
, u32 mv
)
858 struct fusb302_chip
*chip
= container_of(dev
, struct fusb302_chip
,
861 fusb302_log(chip
, "current limit: %d ma, %d mv (not implemented)",
867 static int fusb302_pd_tx_flush(struct fusb302_chip
*chip
)
869 return fusb302_i2c_set_bits(chip
, FUSB_REG_CONTROL0
,
870 FUSB_REG_CONTROL0_TX_FLUSH
);
873 static int fusb302_pd_rx_flush(struct fusb302_chip
*chip
)
875 return fusb302_i2c_set_bits(chip
, FUSB_REG_CONTROL1
,
876 FUSB_REG_CONTROL1_RX_FLUSH
);
879 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip
*chip
, bool on
)
882 return fusb302_i2c_set_bits(chip
, FUSB_REG_SWITCHES1
,
883 FUSB_REG_SWITCHES1_AUTO_GCRC
);
884 return fusb302_i2c_clear_bits(chip
, FUSB_REG_SWITCHES1
,
885 FUSB_REG_SWITCHES1_AUTO_GCRC
);
888 static int fusb302_pd_set_interrupts(struct fusb302_chip
*chip
, bool on
)
891 u8 mask_interrupts
= FUSB_REG_MASK_COLLISION
;
892 u8 maska_interrupts
= FUSB_REG_MASKA_RETRYFAIL
|
893 FUSB_REG_MASKA_HARDSENT
|
894 FUSB_REG_MASKA_TX_SUCCESS
|
895 FUSB_REG_MASKA_HARDRESET
;
896 u8 maskb_interrupts
= FUSB_REG_MASKB_GCRCSENT
;
899 fusb302_i2c_clear_bits(chip
, FUSB_REG_MASK
, mask_interrupts
) :
900 fusb302_i2c_set_bits(chip
, FUSB_REG_MASK
, mask_interrupts
);
904 fusb302_i2c_clear_bits(chip
, FUSB_REG_MASKA
, maska_interrupts
) :
905 fusb302_i2c_set_bits(chip
, FUSB_REG_MASKA
, maska_interrupts
);
909 fusb302_i2c_clear_bits(chip
, FUSB_REG_MASKB
, maskb_interrupts
) :
910 fusb302_i2c_set_bits(chip
, FUSB_REG_MASKB
, maskb_interrupts
);
914 static int tcpm_set_pd_rx(struct tcpc_dev
*dev
, bool on
)
916 struct fusb302_chip
*chip
= container_of(dev
, struct fusb302_chip
,
920 mutex_lock(&chip
->lock
);
921 ret
= fusb302_pd_rx_flush(chip
);
923 fusb302_log(chip
, "cannot flush pd rx buffer, ret=%d", ret
);
926 ret
= fusb302_pd_tx_flush(chip
);
928 fusb302_log(chip
, "cannot flush pd tx buffer, ret=%d", ret
);
931 ret
= fusb302_pd_set_auto_goodcrc(chip
, on
);
933 fusb302_log(chip
, "cannot turn %s auto GCRC, ret=%d",
934 on
? "on" : "off", ret
);
937 ret
= fusb302_pd_set_interrupts(chip
, on
);
939 fusb302_log(chip
, "cannot turn %s pd interrupts, ret=%d",
940 on
? "on" : "off", ret
);
943 fusb302_log(chip
, "pd := %s", on
? "on" : "off");
945 mutex_unlock(&chip
->lock
);
950 static const char * const typec_role_name
[] = {
951 [TYPEC_SINK
] = "Sink",
952 [TYPEC_SOURCE
] = "Source",
955 static const char * const typec_data_role_name
[] = {
956 [TYPEC_DEVICE
] = "Device",
957 [TYPEC_HOST
] = "Host",
960 static int tcpm_set_roles(struct tcpc_dev
*dev
, bool attached
,
961 enum typec_role pwr
, enum typec_data_role data
)
963 struct fusb302_chip
*chip
= container_of(dev
, struct fusb302_chip
,
966 u8 switches1_mask
= FUSB_REG_SWITCHES1_POWERROLE
|
967 FUSB_REG_SWITCHES1_DATAROLE
;
968 u8 switches1_data
= 0x00;
970 mutex_lock(&chip
->lock
);
971 if (pwr
== TYPEC_SOURCE
)
972 switches1_data
|= FUSB_REG_SWITCHES1_POWERROLE
;
973 if (data
== TYPEC_HOST
)
974 switches1_data
|= FUSB_REG_SWITCHES1_DATAROLE
;
975 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_SWITCHES1
,
976 switches1_mask
, switches1_data
);
978 fusb302_log(chip
, "unable to set pd header %s, %s, ret=%d",
979 typec_role_name
[pwr
], typec_data_role_name
[data
],
983 fusb302_log(chip
, "pd header := %s, %s", typec_role_name
[pwr
],
984 typec_data_role_name
[data
]);
986 mutex_unlock(&chip
->lock
);
991 static int tcpm_start_drp_toggling(struct tcpc_dev
*dev
,
992 enum typec_cc_status cc
)
994 struct fusb302_chip
*chip
= container_of(dev
, struct fusb302_chip
,
998 mutex_lock(&chip
->lock
);
999 ret
= fusb302_set_src_current(chip
, cc_src_current
[cc
]);
1001 fusb302_log(chip
, "unable to set src current %s, ret=%d",
1002 typec_cc_status_name
[cc
], ret
);
1005 ret
= fusb302_set_toggling(chip
, TOGGLING_MODE_DRP
);
1008 "unable to start drp toggling, ret=%d", ret
);
1011 fusb302_log(chip
, "start drp toggling");
1013 mutex_unlock(&chip
->lock
);
1018 static int fusb302_pd_send_message(struct fusb302_chip
*chip
,
1019 const struct pd_message
*msg
)
1027 buf
[pos
++] = FUSB302_TKN_SYNC1
;
1028 buf
[pos
++] = FUSB302_TKN_SYNC1
;
1029 buf
[pos
++] = FUSB302_TKN_SYNC1
;
1030 buf
[pos
++] = FUSB302_TKN_SYNC2
;
1032 len
= pd_header_cnt_le(msg
->header
) * 4;
1033 /* plug 2 for header */
1037 "PD message too long %d (incl. header)", len
);
1040 /* packsym tells the FUSB302 chip that the next X bytes are payload */
1041 buf
[pos
++] = FUSB302_TKN_PACKSYM
| (len
& 0x1F);
1042 buf
[pos
++] = msg
->header
& 0xFF;
1043 buf
[pos
++] = (msg
->header
>> 8) & 0xFF;
1046 memcpy(&buf
[pos
], msg
->payload
, len
);
1050 buf
[pos
++] = FUSB302_TKN_JAMCRC
;
1052 buf
[pos
++] = FUSB302_TKN_EOP
;
1053 /* turn tx off after sending message */
1054 buf
[pos
++] = FUSB302_TKN_TXOFF
;
1055 /* start transmission */
1056 buf
[pos
++] = FUSB302_TKN_TXON
;
1058 ret
= fusb302_i2c_block_write(chip
, FUSB_REG_FIFOS
, pos
, buf
);
1061 fusb302_log(chip
, "sending PD message header: %x", msg
->header
);
1062 fusb302_log(chip
, "sending PD message len: %d", len
);
1067 static int fusb302_pd_send_hardreset(struct fusb302_chip
*chip
)
1069 return fusb302_i2c_set_bits(chip
, FUSB_REG_CONTROL3
,
1070 FUSB_REG_CONTROL3_SEND_HARDRESET
);
1073 static const char * const transmit_type_name
[] = {
1074 [TCPC_TX_SOP
] = "SOP",
1075 [TCPC_TX_SOP_PRIME
] = "SOP'",
1076 [TCPC_TX_SOP_PRIME_PRIME
] = "SOP''",
1077 [TCPC_TX_SOP_DEBUG_PRIME
] = "DEBUG'",
1078 [TCPC_TX_SOP_DEBUG_PRIME_PRIME
] = "DEBUG''",
1079 [TCPC_TX_HARD_RESET
] = "HARD_RESET",
1080 [TCPC_TX_CABLE_RESET
] = "CABLE_RESET",
1081 [TCPC_TX_BIST_MODE_2
] = "BIST_MODE_2",
1084 static int tcpm_pd_transmit(struct tcpc_dev
*dev
, enum tcpm_transmit_type type
,
1085 const struct pd_message
*msg
)
1087 struct fusb302_chip
*chip
= container_of(dev
, struct fusb302_chip
,
1091 mutex_lock(&chip
->lock
);
1094 ret
= fusb302_pd_send_message(chip
, msg
);
1097 "cannot send PD message, ret=%d", ret
);
1099 case TCPC_TX_HARD_RESET
:
1100 ret
= fusb302_pd_send_hardreset(chip
);
1103 "cannot send hardreset, ret=%d", ret
);
1106 fusb302_log(chip
, "type %s not supported",
1107 transmit_type_name
[type
]);
1110 mutex_unlock(&chip
->lock
);
1115 static enum typec_cc_status
fusb302_bc_lvl_to_cc(u8 bc_lvl
)
1117 if (bc_lvl
== FUSB_REG_STATUS0_BC_LVL_1230_MAX
)
1118 return TYPEC_CC_RP_3_0
;
1119 if (bc_lvl
== FUSB_REG_STATUS0_BC_LVL_600_1230
)
1120 return TYPEC_CC_RP_1_5
;
1121 if (bc_lvl
== FUSB_REG_STATUS0_BC_LVL_200_600
)
1122 return TYPEC_CC_RP_DEF
;
1123 return TYPEC_CC_OPEN
;
1126 static void fusb302_bc_lvl_handler_work(struct work_struct
*work
)
1128 struct fusb302_chip
*chip
= container_of(work
, struct fusb302_chip
,
1129 bc_lvl_handler
.work
);
1133 enum typec_cc_status cc_status
;
1135 mutex_lock(&chip
->lock
);
1136 if (!chip
->intr_bc_lvl
) {
1137 fusb302_log(chip
, "BC_LVL interrupt is turned off, abort");
1140 ret
= fusb302_i2c_read(chip
, FUSB_REG_STATUS0
, &status0
);
1143 fusb302_log(chip
, "BC_LVL handler, status0=0x%02x", status0
);
1144 if (status0
& FUSB_REG_STATUS0_ACTIVITY
) {
1145 fusb302_log(chip
, "CC activities detected, delay handling");
1146 mod_delayed_work(chip
->wq
, &chip
->bc_lvl_handler
,
1147 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS
));
1150 bc_lvl
= status0
& FUSB_REG_STATUS0_BC_LVL_MASK
;
1151 cc_status
= fusb302_bc_lvl_to_cc(bc_lvl
);
1152 if (chip
->cc_polarity
== TYPEC_POLARITY_CC1
) {
1153 if (chip
->cc1
!= cc_status
) {
1154 fusb302_log(chip
, "cc1: %s -> %s",
1155 typec_cc_status_name
[chip
->cc1
],
1156 typec_cc_status_name
[cc_status
]);
1157 chip
->cc1
= cc_status
;
1158 tcpm_cc_change(chip
->tcpm_port
);
1161 if (chip
->cc2
!= cc_status
) {
1162 fusb302_log(chip
, "cc2: %s -> %s",
1163 typec_cc_status_name
[chip
->cc2
],
1164 typec_cc_status_name
[cc_status
]);
1165 chip
->cc2
= cc_status
;
1166 tcpm_cc_change(chip
->tcpm_port
);
1171 mutex_unlock(&chip
->lock
);
1174 #define PDO_FIXED_FLAGS \
1175 (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1177 static const u32 src_pdo
[] = {
1178 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS
),
1181 static const u32 snk_pdo
[] = {
1182 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS
),
1185 static const struct tcpc_config fusb302_tcpc_config
= {
1187 .nr_src_pdo
= ARRAY_SIZE(src_pdo
),
1189 .nr_snk_pdo
= ARRAY_SIZE(snk_pdo
),
1192 .max_snk_mw
= 27000,
1193 .operating_snk_mw
= 2500,
1194 .type
= TYPEC_PORT_DRP
,
1195 .default_role
= TYPEC_SINK
,
1199 static void init_tcpc_dev(struct tcpc_dev
*fusb302_tcpc_dev
)
1201 fusb302_tcpc_dev
->config
= &fusb302_tcpc_config
;
1202 fusb302_tcpc_dev
->init
= tcpm_init
;
1203 fusb302_tcpc_dev
->get_vbus
= tcpm_get_vbus
;
1204 fusb302_tcpc_dev
->set_cc
= tcpm_set_cc
;
1205 fusb302_tcpc_dev
->get_cc
= tcpm_get_cc
;
1206 fusb302_tcpc_dev
->set_polarity
= tcpm_set_polarity
;
1207 fusb302_tcpc_dev
->set_vconn
= tcpm_set_vconn
;
1208 fusb302_tcpc_dev
->set_vbus
= tcpm_set_vbus
;
1209 fusb302_tcpc_dev
->set_current_limit
= tcpm_set_current_limit
;
1210 fusb302_tcpc_dev
->set_pd_rx
= tcpm_set_pd_rx
;
1211 fusb302_tcpc_dev
->set_roles
= tcpm_set_roles
;
1212 fusb302_tcpc_dev
->start_drp_toggling
= tcpm_start_drp_toggling
;
1213 fusb302_tcpc_dev
->pd_transmit
= tcpm_pd_transmit
;
1214 fusb302_tcpc_dev
->mux
= NULL
;
1217 static const char * const cc_polarity_name
[] = {
1218 [TYPEC_POLARITY_CC1
] = "Polarity_CC1",
1219 [TYPEC_POLARITY_CC2
] = "Polarity_CC2",
1222 static int fusb302_set_cc_polarity(struct fusb302_chip
*chip
,
1223 enum typec_cc_polarity cc_polarity
)
1226 u8 switches0_mask
= FUSB_REG_SWITCHES0_CC1_PU_EN
|
1227 FUSB_REG_SWITCHES0_CC2_PU_EN
|
1228 FUSB_REG_SWITCHES0_VCONN_CC1
|
1229 FUSB_REG_SWITCHES0_VCONN_CC2
|
1230 FUSB_REG_SWITCHES0_MEAS_CC1
|
1231 FUSB_REG_SWITCHES0_MEAS_CC2
;
1232 u8 switches0_data
= 0x00;
1233 u8 switches1_mask
= FUSB_REG_SWITCHES1_TXCC1_EN
|
1234 FUSB_REG_SWITCHES1_TXCC2_EN
;
1235 u8 switches1_data
= 0x00;
1237 if (cc_polarity
== TYPEC_POLARITY_CC1
) {
1238 switches0_data
= FUSB_REG_SWITCHES0_MEAS_CC1
;
1240 switches0_data
|= FUSB_REG_SWITCHES0_VCONN_CC2
;
1242 switches0_data
|= FUSB_REG_SWITCHES0_CC1_PU_EN
;
1243 switches1_data
= FUSB_REG_SWITCHES1_TXCC1_EN
;
1245 switches0_data
= FUSB_REG_SWITCHES0_MEAS_CC2
;
1247 switches0_data
|= FUSB_REG_SWITCHES0_VCONN_CC1
;
1249 switches0_data
|= FUSB_REG_SWITCHES0_CC2_PU_EN
;
1250 switches1_data
= FUSB_REG_SWITCHES1_TXCC2_EN
;
1252 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_SWITCHES0
,
1253 switches0_mask
, switches0_data
);
1256 ret
= fusb302_i2c_mask_write(chip
, FUSB_REG_SWITCHES1
,
1257 switches1_mask
, switches1_data
);
1260 chip
->cc_polarity
= cc_polarity
;
1265 static int fusb302_handle_togdone_snk(struct fusb302_chip
*chip
,
1271 enum typec_cc_polarity cc_polarity
;
1272 enum typec_cc_status cc_status_active
, cc1
, cc2
;
1274 /* set pull_up, pull_down */
1275 ret
= fusb302_set_cc_pull(chip
, false, true);
1277 fusb302_log(chip
, "cannot set cc to pull down, ret=%d", ret
);
1281 cc_polarity
= (togdone_result
== FUSB_REG_STATUS1A_TOGSS_SNK1
) ?
1282 TYPEC_POLARITY_CC1
: TYPEC_POLARITY_CC2
;
1283 ret
= fusb302_set_cc_polarity(chip
, cc_polarity
);
1285 fusb302_log(chip
, "cannot set cc polarity %s, ret=%d",
1286 cc_polarity_name
[cc_polarity
], ret
);
1289 /* fusb302_set_cc_polarity() has set the correct measure block */
1290 ret
= fusb302_i2c_read(chip
, FUSB_REG_STATUS0
, &status0
);
1293 bc_lvl
= status0
& FUSB_REG_STATUS0_BC_LVL_MASK
;
1294 cc_status_active
= fusb302_bc_lvl_to_cc(bc_lvl
);
1295 /* restart toggling if the cc status on the active line is OPEN */
1296 if (cc_status_active
== TYPEC_CC_OPEN
) {
1297 fusb302_log(chip
, "restart toggling as CC_OPEN detected");
1298 ret
= fusb302_set_toggling(chip
, chip
->toggling_mode
);
1301 /* update tcpm with the new cc value */
1302 cc1
= (cc_polarity
== TYPEC_POLARITY_CC1
) ?
1303 cc_status_active
: TYPEC_CC_OPEN
;
1304 cc2
= (cc_polarity
== TYPEC_POLARITY_CC2
) ?
1305 cc_status_active
: TYPEC_CC_OPEN
;
1306 if ((chip
->cc1
!= cc1
) || (chip
->cc2
!= cc2
)) {
1309 tcpm_cc_change(chip
->tcpm_port
);
1311 /* turn off toggling */
1312 ret
= fusb302_set_toggling(chip
, TOGGLINE_MODE_OFF
);
1315 "cannot set toggling mode off, ret=%d", ret
);
1318 /* unmask bc_lvl interrupt */
1319 ret
= fusb302_i2c_clear_bits(chip
, FUSB_REG_MASK
, FUSB_REG_MASK_BC_LVL
);
1322 "cannot unmask bc_lcl interrupt, ret=%d", ret
);
1325 chip
->intr_bc_lvl
= true;
1326 fusb302_log(chip
, "detected cc1=%s, cc2=%s",
1327 typec_cc_status_name
[cc1
],
1328 typec_cc_status_name
[cc2
]);
1333 static int fusb302_handle_togdone_src(struct fusb302_chip
*chip
,
1337 * - set polarity (measure cc, vconn, tx)
1338 * - set pull_up, pull_down
1339 * - set cc1, cc2, and update to tcpm_port
1340 * - set I_COMP interrupt on
1344 u8 ra_mda
= ra_mda_value
[chip
->src_current_status
];
1345 u8 rd_mda
= rd_mda_value
[chip
->src_current_status
];
1346 bool ra_comp
, rd_comp
;
1347 enum typec_cc_polarity cc_polarity
;
1348 enum typec_cc_status cc_status_active
, cc1
, cc2
;
1350 /* set pull_up, pull_down */
1351 ret
= fusb302_set_cc_pull(chip
, true, false);
1353 fusb302_log(chip
, "cannot set cc to pull up, ret=%d", ret
);
1357 cc_polarity
= (togdone_result
== FUSB_REG_STATUS1A_TOGSS_SRC1
) ?
1358 TYPEC_POLARITY_CC1
: TYPEC_POLARITY_CC2
;
1359 ret
= fusb302_set_cc_polarity(chip
, cc_polarity
);
1361 fusb302_log(chip
, "cannot set cc polarity %s, ret=%d",
1362 cc_polarity_name
[cc_polarity
], ret
);
1365 /* fusb302_set_cc_polarity() has set the correct measure block */
1366 ret
= fusb302_i2c_write(chip
, FUSB_REG_MEASURE
, rd_mda
);
1369 usleep_range(50, 100);
1370 ret
= fusb302_i2c_read(chip
, FUSB_REG_STATUS0
, &status0
);
1373 rd_comp
= !!(status0
& FUSB_REG_STATUS0_COMP
);
1375 ret
= fusb302_i2c_write(chip
, FUSB_REG_MEASURE
, ra_mda
);
1378 usleep_range(50, 100);
1379 ret
= fusb302_i2c_read(chip
, FUSB_REG_STATUS0
, &status0
);
1382 ra_comp
= !!(status0
& FUSB_REG_STATUS0_COMP
);
1385 cc_status_active
= TYPEC_CC_OPEN
;
1387 cc_status_active
= TYPEC_CC_RD
;
1389 /* Ra is not supported, report as Open */
1390 cc_status_active
= TYPEC_CC_OPEN
;
1391 /* restart toggling if the cc status on the active line is OPEN */
1392 if (cc_status_active
== TYPEC_CC_OPEN
) {
1393 fusb302_log(chip
, "restart toggling as CC_OPEN detected");
1394 ret
= fusb302_set_toggling(chip
, chip
->toggling_mode
);
1397 /* update tcpm with the new cc value */
1398 cc1
= (cc_polarity
== TYPEC_POLARITY_CC1
) ?
1399 cc_status_active
: TYPEC_CC_OPEN
;
1400 cc2
= (cc_polarity
== TYPEC_POLARITY_CC2
) ?
1401 cc_status_active
: TYPEC_CC_OPEN
;
1402 if ((chip
->cc1
!= cc1
) || (chip
->cc2
!= cc2
)) {
1405 tcpm_cc_change(chip
->tcpm_port
);
1407 /* turn off toggling */
1408 ret
= fusb302_set_toggling(chip
, TOGGLINE_MODE_OFF
);
1411 "cannot set toggling mode off, ret=%d", ret
);
1414 /* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1415 ret
= fusb302_i2c_write(chip
, FUSB_REG_MEASURE
, rd_mda
);
1418 /* unmask comp_chng interrupt */
1419 ret
= fusb302_i2c_clear_bits(chip
, FUSB_REG_MASK
,
1420 FUSB_REG_MASK_COMP_CHNG
);
1423 "cannot unmask bc_lcl interrupt, ret=%d", ret
);
1426 chip
->intr_comp_chng
= true;
1427 fusb302_log(chip
, "detected cc1=%s, cc2=%s",
1428 typec_cc_status_name
[cc1
],
1429 typec_cc_status_name
[cc2
]);
1434 static int fusb302_handle_togdone(struct fusb302_chip
*chip
)
1440 ret
= fusb302_i2c_read(chip
, FUSB_REG_STATUS1A
, &status1a
);
1443 togdone_result
= (status1a
>> FUSB_REG_STATUS1A_TOGSS_POS
) &
1444 FUSB_REG_STATUS1A_TOGSS_MASK
;
1445 switch (togdone_result
) {
1446 case FUSB_REG_STATUS1A_TOGSS_SNK1
:
1447 case FUSB_REG_STATUS1A_TOGSS_SNK2
:
1448 return fusb302_handle_togdone_snk(chip
, togdone_result
);
1449 case FUSB_REG_STATUS1A_TOGSS_SRC1
:
1450 case FUSB_REG_STATUS1A_TOGSS_SRC2
:
1451 return fusb302_handle_togdone_src(chip
, togdone_result
);
1452 case FUSB_REG_STATUS1A_TOGSS_AA
:
1453 /* doesn't support */
1454 fusb302_log(chip
, "AudioAccessory not supported");
1455 fusb302_set_toggling(chip
, chip
->toggling_mode
);
1458 fusb302_log(chip
, "TOGDONE with an invalid state: %d",
1460 fusb302_set_toggling(chip
, chip
->toggling_mode
);
1466 static int fusb302_pd_reset(struct fusb302_chip
*chip
)
1468 return fusb302_i2c_set_bits(chip
, FUSB_REG_RESET
,
1469 FUSB_REG_RESET_PD_RESET
);
1472 static int fusb302_pd_read_message(struct fusb302_chip
*chip
,
1473 struct pd_message
*msg
)
1480 /* first SOP token */
1481 ret
= fusb302_i2c_read(chip
, FUSB_REG_FIFOS
, &token
);
1484 ret
= fusb302_i2c_block_read(chip
, FUSB_REG_FIFOS
, 2,
1485 (u8
*)&msg
->header
);
1488 len
= pd_header_cnt_le(msg
->header
) * 4;
1489 /* add 4 to length to include the CRC */
1490 if (len
> PD_MAX_PAYLOAD
* 4) {
1491 fusb302_log(chip
, "PD message too long %d", len
);
1495 ret
= fusb302_i2c_block_read(chip
, FUSB_REG_FIFOS
, len
,
1496 (u8
*)msg
->payload
);
1500 /* another 4 bytes to read CRC out */
1501 ret
= fusb302_i2c_block_read(chip
, FUSB_REG_FIFOS
, 4, crc
);
1504 fusb302_log(chip
, "PD message header: %x", msg
->header
);
1505 fusb302_log(chip
, "PD message len: %d", len
);
1510 static irqreturn_t
fusb302_irq_intn(int irq
, void *dev_id
)
1512 struct fusb302_chip
*chip
= dev_id
;
1522 bool intr_comp_chng
;
1523 struct pd_message pd_msg
;
1525 mutex_lock(&chip
->lock
);
1526 /* grab a snapshot of intr flags */
1527 intr_togdone
= chip
->intr_togdone
;
1528 intr_bc_lvl
= chip
->intr_bc_lvl
;
1529 intr_comp_chng
= chip
->intr_comp_chng
;
1531 ret
= fusb302_i2c_read(chip
, FUSB_REG_INTERRUPT
, &interrupt
);
1534 ret
= fusb302_i2c_read(chip
, FUSB_REG_INTERRUPTA
, &interrupta
);
1537 ret
= fusb302_i2c_read(chip
, FUSB_REG_INTERRUPTB
, &interruptb
);
1540 ret
= fusb302_i2c_read(chip
, FUSB_REG_STATUS0
, &status0
);
1544 "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1545 interrupt
, interrupta
, interruptb
, status0
);
1547 if (interrupt
& FUSB_REG_INTERRUPT_VBUSOK
) {
1548 vbus_present
= !!(status0
& FUSB_REG_STATUS0_VBUSOK
);
1549 fusb302_log(chip
, "IRQ: VBUS_OK, vbus=%s",
1550 vbus_present
? "On" : "Off");
1551 if (vbus_present
!= chip
->vbus_present
) {
1552 chip
->vbus_present
= vbus_present
;
1553 tcpm_vbus_change(chip
->tcpm_port
);
1557 if ((interrupta
& FUSB_REG_INTERRUPTA_TOGDONE
) && intr_togdone
) {
1558 fusb302_log(chip
, "IRQ: TOGDONE");
1559 ret
= fusb302_handle_togdone(chip
);
1562 "handle togdone error, ret=%d", ret
);
1567 if ((interrupt
& FUSB_REG_INTERRUPT_BC_LVL
) && intr_bc_lvl
) {
1568 fusb302_log(chip
, "IRQ: BC_LVL, handler pending");
1570 * as BC_LVL interrupt can be affected by PD activity,
1571 * apply delay to for the handler to wait for the PD
1572 * signaling to finish.
1574 mod_delayed_work(chip
->wq
, &chip
->bc_lvl_handler
,
1575 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS
));
1578 if ((interrupt
& FUSB_REG_INTERRUPT_COMP_CHNG
) && intr_comp_chng
) {
1579 comp_result
= !!(status0
& FUSB_REG_STATUS0_COMP
);
1580 fusb302_log(chip
, "IRQ: COMP_CHNG, comp=%s",
1581 comp_result
? "true" : "false");
1583 /* cc level > Rd_threashold, detach */
1584 if (chip
->cc_polarity
== TYPEC_POLARITY_CC1
)
1585 chip
->cc1
= TYPEC_CC_OPEN
;
1587 chip
->cc2
= TYPEC_CC_OPEN
;
1588 tcpm_cc_change(chip
->tcpm_port
);
1592 if (interrupt
& FUSB_REG_INTERRUPT_COLLISION
) {
1593 fusb302_log(chip
, "IRQ: PD collision");
1594 tcpm_pd_transmit_complete(chip
->tcpm_port
, TCPC_TX_FAILED
);
1597 if (interrupta
& FUSB_REG_INTERRUPTA_RETRYFAIL
) {
1598 fusb302_log(chip
, "IRQ: PD retry failed");
1599 tcpm_pd_transmit_complete(chip
->tcpm_port
, TCPC_TX_FAILED
);
1602 if (interrupta
& FUSB_REG_INTERRUPTA_HARDSENT
) {
1603 fusb302_log(chip
, "IRQ: PD hardreset sent");
1604 ret
= fusb302_pd_reset(chip
);
1606 fusb302_log(chip
, "cannot PD reset, ret=%d", ret
);
1609 tcpm_pd_transmit_complete(chip
->tcpm_port
, TCPC_TX_SUCCESS
);
1612 if (interrupta
& FUSB_REG_INTERRUPTA_TX_SUCCESS
) {
1613 fusb302_log(chip
, "IRQ: PD tx success");
1614 /* read out the received good CRC */
1615 ret
= fusb302_pd_read_message(chip
, &pd_msg
);
1617 fusb302_log(chip
, "cannot read in GCRC, ret=%d", ret
);
1620 tcpm_pd_transmit_complete(chip
->tcpm_port
, TCPC_TX_SUCCESS
);
1623 if (interrupta
& FUSB_REG_INTERRUPTA_HARDRESET
) {
1624 fusb302_log(chip
, "IRQ: PD received hardreset");
1625 ret
= fusb302_pd_reset(chip
);
1627 fusb302_log(chip
, "cannot PD reset, ret=%d", ret
);
1630 tcpm_pd_hard_reset(chip
->tcpm_port
);
1633 if (interruptb
& FUSB_REG_INTERRUPTB_GCRCSENT
) {
1634 fusb302_log(chip
, "IRQ: PD sent good CRC");
1635 ret
= fusb302_pd_read_message(chip
, &pd_msg
);
1638 "cannot read in PD message, ret=%d", ret
);
1641 tcpm_pd_receive(chip
->tcpm_port
, &pd_msg
);
1644 mutex_unlock(&chip
->lock
);
1649 static int init_gpio(struct fusb302_chip
*chip
)
1651 struct device_node
*node
;
1654 node
= chip
->dev
->of_node
;
1655 chip
->gpio_int_n
= of_get_named_gpio(node
, "fcs,int_n", 0);
1656 if (!gpio_is_valid(chip
->gpio_int_n
)) {
1657 ret
= chip
->gpio_int_n
;
1658 fusb302_log(chip
, "cannot get named GPIO Int_N, ret=%d", ret
);
1661 ret
= devm_gpio_request(chip
->dev
, chip
->gpio_int_n
, "fcs,int_n");
1663 fusb302_log(chip
, "cannot request GPIO Int_N, ret=%d", ret
);
1666 ret
= gpio_direction_input(chip
->gpio_int_n
);
1669 "cannot set GPIO Int_N to input, ret=%d", ret
);
1672 ret
= gpio_to_irq(chip
->gpio_int_n
);
1675 "cannot request IRQ for GPIO Int_N, ret=%d", ret
);
1678 chip
->gpio_int_n_irq
= ret
;
1682 static int fusb302_probe(struct i2c_client
*client
,
1683 const struct i2c_device_id
*id
)
1685 struct fusb302_chip
*chip
;
1686 struct i2c_adapter
*adapter
;
1689 adapter
= to_i2c_adapter(client
->dev
.parent
);
1690 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
)) {
1691 dev_err(&client
->dev
,
1692 "I2C/SMBus block functionality not supported!\n");
1695 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
1699 chip
->i2c_client
= client
;
1700 i2c_set_clientdata(client
, chip
);
1701 chip
->dev
= &client
->dev
;
1702 mutex_init(&chip
->lock
);
1704 ret
= fusb302_debugfs_init(chip
);
1708 chip
->wq
= create_singlethread_workqueue(dev_name(chip
->dev
));
1711 goto clear_client_data
;
1713 INIT_DELAYED_WORK(&chip
->bc_lvl_handler
, fusb302_bc_lvl_handler_work
);
1714 init_tcpc_dev(&chip
->tcpc_dev
);
1716 chip
->vbus
= devm_regulator_get(chip
->dev
, "vbus");
1717 if (IS_ERR(chip
->vbus
)) {
1718 ret
= PTR_ERR(chip
->vbus
);
1719 goto destroy_workqueue
;
1722 ret
= init_gpio(chip
);
1724 goto destroy_workqueue
;
1726 chip
->tcpm_port
= tcpm_register_port(&client
->dev
, &chip
->tcpc_dev
);
1727 if (IS_ERR(chip
->tcpm_port
)) {
1728 ret
= PTR_ERR(chip
->tcpm_port
);
1729 fusb302_log(chip
, "cannot register tcpm port, ret=%d", ret
);
1730 goto destroy_workqueue
;
1733 ret
= devm_request_threaded_irq(chip
->dev
, chip
->gpio_int_n_irq
,
1734 NULL
, fusb302_irq_intn
,
1735 IRQF_ONESHOT
| IRQF_TRIGGER_LOW
,
1736 "fsc_interrupt_int_n", chip
);
1739 "cannot request IRQ for GPIO Int_N, ret=%d", ret
);
1740 goto tcpm_unregister_port
;
1742 enable_irq_wake(chip
->gpio_int_n_irq
);
1745 tcpm_unregister_port
:
1746 tcpm_unregister_port(chip
->tcpm_port
);
1748 destroy_workqueue(chip
->wq
);
1750 i2c_set_clientdata(client
, NULL
);
1751 fusb302_debugfs_exit(chip
);
1756 static int fusb302_remove(struct i2c_client
*client
)
1758 struct fusb302_chip
*chip
= i2c_get_clientdata(client
);
1760 tcpm_unregister_port(chip
->tcpm_port
);
1761 destroy_workqueue(chip
->wq
);
1762 i2c_set_clientdata(client
, NULL
);
1763 fusb302_debugfs_exit(chip
);
1768 static int fusb302_pm_suspend(struct device
*dev
)
1770 struct fusb302_chip
*chip
= dev
->driver_data
;
1772 if (atomic_read(&chip
->i2c_busy
))
1774 atomic_set(&chip
->pm_suspend
, 1);
1779 static int fusb302_pm_resume(struct device
*dev
)
1781 struct fusb302_chip
*chip
= dev
->driver_data
;
1783 atomic_set(&chip
->pm_suspend
, 0);
1788 static const struct of_device_id fusb302_dt_match
[] = {
1789 {.compatible
= "fcs,fusb302"},
1792 MODULE_DEVICE_TABLE(of
, fusb302_dt_match
);
1794 static const struct i2c_device_id fusb302_i2c_device_id
[] = {
1795 {"typec_fusb302", 0},
1798 MODULE_DEVICE_TABLE(i2c
, fusb302_i2c_device_id
);
1800 static const struct dev_pm_ops fusb302_pm_ops
= {
1801 .suspend
= fusb302_pm_suspend
,
1802 .resume
= fusb302_pm_resume
,
1805 static struct i2c_driver fusb302_driver
= {
1807 .name
= "typec_fusb302",
1808 .pm
= &fusb302_pm_ops
,
1809 .of_match_table
= of_match_ptr(fusb302_dt_match
),
1811 .probe
= fusb302_probe
,
1812 .remove
= fusb302_remove
,
1813 .id_table
= fusb302_i2c_device_id
,
1815 module_i2c_driver(fusb302_driver
);
1817 MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1818 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1819 MODULE_LICENSE("GPL");