xref: /openbmc/linux/drivers/usb/typec/tcpm/fusb302.c (revision 34facb04)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2016-2017 Google, Inc
4  *
5  * Fairchild FUSB302 Type-C Chip Driver
6  */
7 
8 #include <linux/debugfs.h>
9 #include <linux/delay.h>
10 #include <linux/errno.h>
11 #include <linux/extcon.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of_device.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/proc_fs.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/sched/clock.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
28 #include <linux/usb.h>
29 #include <linux/usb/typec.h>
30 #include <linux/usb/tcpm.h>
31 #include <linux/usb/pd.h>
32 #include <linux/workqueue.h>
33 
34 #include "fusb302_reg.h"
35 
36 /*
37  * When the device is SNK, BC_LVL interrupt is used to monitor cc pins
38  * for the current capability offered by the SRC. As FUSB302 chip fires
39  * the BC_LVL interrupt on PD signalings, cc lvl should be handled after
40  * a delay to avoid measuring on PD activities. The delay is slightly
41  * longer than PD_T_PD_DEBPUNCE (10-20ms).
42  */
43 #define T_BC_LVL_DEBOUNCE_DELAY_MS 30
44 
45 enum toggling_mode {
46 	TOGGLING_MODE_OFF,
47 	TOGGLING_MODE_DRP,
48 	TOGGLING_MODE_SNK,
49 	TOGGLING_MODE_SRC,
50 };
51 
52 enum src_current_status {
53 	SRC_CURRENT_DEFAULT,
54 	SRC_CURRENT_MEDIUM,
55 	SRC_CURRENT_HIGH,
56 };
57 
58 static const u8 ra_mda_value[] = {
59 	[SRC_CURRENT_DEFAULT] = 4,	/* 210mV */
60 	[SRC_CURRENT_MEDIUM] = 9,	/* 420mV */
61 	[SRC_CURRENT_HIGH] = 18,	/* 798mV */
62 };
63 
64 static const u8 rd_mda_value[] = {
65 	[SRC_CURRENT_DEFAULT] = 38,	/* 1638mV */
66 	[SRC_CURRENT_MEDIUM] = 38,	/* 1638mV */
67 	[SRC_CURRENT_HIGH] = 61,	/* 2604mV */
68 };
69 
70 #define LOG_BUFFER_ENTRIES	1024
71 #define LOG_BUFFER_ENTRY_SIZE	128
72 
73 struct fusb302_chip {
74 	struct device *dev;
75 	struct i2c_client *i2c_client;
76 	struct tcpm_port *tcpm_port;
77 	struct tcpc_dev tcpc_dev;
78 
79 	struct regulator *vbus;
80 
81 	spinlock_t irq_lock;
82 	struct work_struct irq_work;
83 	bool irq_suspended;
84 	bool irq_while_suspended;
85 	struct gpio_desc *gpio_int_n;
86 	int gpio_int_n_irq;
87 	struct extcon_dev *extcon;
88 
89 	struct workqueue_struct *wq;
90 	struct delayed_work bc_lvl_handler;
91 
92 	/* lock for sharing chip states */
93 	struct mutex lock;
94 
95 	/* chip status */
96 	enum toggling_mode toggling_mode;
97 	enum src_current_status src_current_status;
98 	bool intr_togdone;
99 	bool intr_bc_lvl;
100 	bool intr_comp_chng;
101 
102 	/* port status */
103 	bool vconn_on;
104 	bool vbus_on;
105 	bool charge_on;
106 	bool vbus_present;
107 	enum typec_cc_polarity cc_polarity;
108 	enum typec_cc_status cc1;
109 	enum typec_cc_status cc2;
110 	u32 snk_pdo[PDO_MAX_OBJECTS];
111 
112 #ifdef CONFIG_DEBUG_FS
113 	struct dentry *dentry;
114 	/* lock for log buffer access */
115 	struct mutex logbuffer_lock;
116 	int logbuffer_head;
117 	int logbuffer_tail;
118 	u8 *logbuffer[LOG_BUFFER_ENTRIES];
119 #endif
120 };
121 
122 /*
123  * Logging
124  */
125 
126 #ifdef CONFIG_DEBUG_FS
127 static bool fusb302_log_full(struct fusb302_chip *chip)
128 {
129 	return chip->logbuffer_tail ==
130 		(chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
131 }
132 
133 __printf(2, 0)
134 static void _fusb302_log(struct fusb302_chip *chip, const char *fmt,
135 			 va_list args)
136 {
137 	char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
138 	u64 ts_nsec = local_clock();
139 	unsigned long rem_nsec;
140 
141 	if (!chip->logbuffer[chip->logbuffer_head]) {
142 		chip->logbuffer[chip->logbuffer_head] =
143 				kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
144 		if (!chip->logbuffer[chip->logbuffer_head])
145 			return;
146 	}
147 
148 	vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
149 
150 	mutex_lock(&chip->logbuffer_lock);
151 
152 	if (fusb302_log_full(chip)) {
153 		chip->logbuffer_head = max(chip->logbuffer_head - 1, 0);
154 		strlcpy(tmpbuffer, "overflow", sizeof(tmpbuffer));
155 	}
156 
157 	if (chip->logbuffer_head < 0 ||
158 	    chip->logbuffer_head >= LOG_BUFFER_ENTRIES) {
159 		dev_warn(chip->dev,
160 			 "Bad log buffer index %d\n", chip->logbuffer_head);
161 		goto abort;
162 	}
163 
164 	if (!chip->logbuffer[chip->logbuffer_head]) {
165 		dev_warn(chip->dev,
166 			 "Log buffer index %d is NULL\n", chip->logbuffer_head);
167 		goto abort;
168 	}
169 
170 	rem_nsec = do_div(ts_nsec, 1000000000);
171 	scnprintf(chip->logbuffer[chip->logbuffer_head],
172 		  LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
173 		  (unsigned long)ts_nsec, rem_nsec / 1000,
174 		  tmpbuffer);
175 	chip->logbuffer_head = (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
176 
177 abort:
178 	mutex_unlock(&chip->logbuffer_lock);
179 }
180 
181 static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
182 {
183 	va_list args;
184 
185 	va_start(args, fmt);
186 	_fusb302_log(chip, fmt, args);
187 	va_end(args);
188 }
189 
190 static int fusb302_debug_show(struct seq_file *s, void *v)
191 {
192 	struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
193 	int tail;
194 
195 	mutex_lock(&chip->logbuffer_lock);
196 	tail = chip->logbuffer_tail;
197 	while (tail != chip->logbuffer_head) {
198 		seq_printf(s, "%s\n", chip->logbuffer[tail]);
199 		tail = (tail + 1) % LOG_BUFFER_ENTRIES;
200 	}
201 	if (!seq_has_overflowed(s))
202 		chip->logbuffer_tail = tail;
203 	mutex_unlock(&chip->logbuffer_lock);
204 
205 	return 0;
206 }
207 DEFINE_SHOW_ATTRIBUTE(fusb302_debug);
208 
209 static void fusb302_debugfs_init(struct fusb302_chip *chip)
210 {
211 	char name[NAME_MAX];
212 
213 	mutex_init(&chip->logbuffer_lock);
214 	snprintf(name, NAME_MAX, "fusb302-%s", dev_name(chip->dev));
215 	chip->dentry = debugfs_create_file(name, S_IFREG | 0444, usb_debug_root,
216 					   chip, &fusb302_debug_fops);
217 }
218 
219 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
220 {
221 	debugfs_remove(chip->dentry);
222 }
223 
224 #else
225 
226 static void fusb302_log(const struct fusb302_chip *chip,
227 			const char *fmt, ...) { }
228 static void fusb302_debugfs_init(const struct fusb302_chip *chip) { }
229 static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
230 
231 #endif
232 
233 static int fusb302_i2c_write(struct fusb302_chip *chip,
234 			     u8 address, u8 data)
235 {
236 	int ret = 0;
237 
238 	ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
239 	if (ret < 0)
240 		fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
241 			    data, address, ret);
242 
243 	return ret;
244 }
245 
246 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
247 				   u8 length, const u8 *data)
248 {
249 	int ret = 0;
250 
251 	if (length <= 0)
252 		return ret;
253 
254 	ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
255 					     length, data);
256 	if (ret < 0)
257 		fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
258 			    address, length, ret);
259 
260 	return ret;
261 }
262 
263 static int fusb302_i2c_read(struct fusb302_chip *chip,
264 			    u8 address, u8 *data)
265 {
266 	int ret = 0;
267 
268 	ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
269 	*data = (u8)ret;
270 	if (ret < 0)
271 		fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
272 
273 	return ret;
274 }
275 
276 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
277 				  u8 length, u8 *data)
278 {
279 	int ret = 0;
280 
281 	if (length <= 0)
282 		return ret;
283 
284 	ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
285 					    length, data);
286 	if (ret < 0) {
287 		fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
288 			    address, length, ret);
289 		goto done;
290 	}
291 	if (ret != length) {
292 		fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
293 			    ret, length, address);
294 		ret = -EIO;
295 	}
296 
297 done:
298 	return ret;
299 }
300 
301 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
302 				  u8 mask, u8 value)
303 {
304 	int ret = 0;
305 	u8 data;
306 
307 	ret = fusb302_i2c_read(chip, address, &data);
308 	if (ret < 0)
309 		return ret;
310 	data &= ~mask;
311 	data |= value;
312 	ret = fusb302_i2c_write(chip, address, data);
313 	if (ret < 0)
314 		return ret;
315 
316 	return ret;
317 }
318 
319 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
320 				u8 set_bits)
321 {
322 	return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
323 }
324 
325 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
326 				  u8 clear_bits)
327 {
328 	return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
329 }
330 
331 static int fusb302_sw_reset(struct fusb302_chip *chip)
332 {
333 	int ret = 0;
334 
335 	ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
336 				FUSB_REG_RESET_SW_RESET);
337 	if (ret < 0)
338 		fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
339 	else
340 		fusb302_log(chip, "sw reset");
341 
342 	return ret;
343 }
344 
345 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip)
346 {
347 	int ret = 0;
348 
349 	ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
350 				   FUSB_REG_CONTROL3_N_RETRIES_3 |
351 				   FUSB_REG_CONTROL3_AUTO_RETRY);
352 
353 	return ret;
354 }
355 
356 /*
357  * initialize interrupt on the chip
358  * - unmasked interrupt: VBUS_OK
359  */
360 static int fusb302_init_interrupt(struct fusb302_chip *chip)
361 {
362 	int ret = 0;
363 
364 	ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
365 				0xFF & ~FUSB_REG_MASK_VBUSOK);
366 	if (ret < 0)
367 		return ret;
368 	ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
369 	if (ret < 0)
370 		return ret;
371 	ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
372 	if (ret < 0)
373 		return ret;
374 	ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
375 				     FUSB_REG_CONTROL0_INT_MASK);
376 	if (ret < 0)
377 		return ret;
378 
379 	return ret;
380 }
381 
382 static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
383 {
384 	int ret = 0;
385 
386 	ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
387 
388 	return ret;
389 }
390 
391 static int tcpm_init(struct tcpc_dev *dev)
392 {
393 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
394 						 tcpc_dev);
395 	int ret = 0;
396 	u8 data;
397 
398 	ret = fusb302_sw_reset(chip);
399 	if (ret < 0)
400 		return ret;
401 	ret = fusb302_enable_tx_auto_retries(chip);
402 	if (ret < 0)
403 		return ret;
404 	ret = fusb302_init_interrupt(chip);
405 	if (ret < 0)
406 		return ret;
407 	ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
408 	if (ret < 0)
409 		return ret;
410 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
411 	if (ret < 0)
412 		return ret;
413 	chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
414 	ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
415 	if (ret < 0)
416 		return ret;
417 	fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
418 
419 	return ret;
420 }
421 
422 static int tcpm_get_vbus(struct tcpc_dev *dev)
423 {
424 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
425 						 tcpc_dev);
426 	int ret = 0;
427 
428 	mutex_lock(&chip->lock);
429 	ret = chip->vbus_present ? 1 : 0;
430 	mutex_unlock(&chip->lock);
431 
432 	return ret;
433 }
434 
435 static int tcpm_get_current_limit(struct tcpc_dev *dev)
436 {
437 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
438 						 tcpc_dev);
439 	int current_limit = 0;
440 	unsigned long timeout;
441 
442 	if (!chip->extcon)
443 		return 0;
444 
445 	/*
446 	 * USB2 Charger detection may still be in progress when we get here,
447 	 * this can take upto 600ms, wait 800ms max.
448 	 */
449 	timeout = jiffies + msecs_to_jiffies(800);
450 	do {
451 		if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_SDP) == 1)
452 			current_limit = 500;
453 
454 		if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_CDP) == 1 ||
455 		    extcon_get_state(chip->extcon, EXTCON_CHG_USB_ACA) == 1)
456 			current_limit = 1500;
457 
458 		if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_DCP) == 1)
459 			current_limit = 2000;
460 
461 		msleep(50);
462 	} while (current_limit == 0 && time_before(jiffies, timeout));
463 
464 	return current_limit;
465 }
466 
467 static int fusb302_set_src_current(struct fusb302_chip *chip,
468 				   enum src_current_status status)
469 {
470 	int ret = 0;
471 
472 	chip->src_current_status = status;
473 	switch (status) {
474 	case SRC_CURRENT_DEFAULT:
475 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
476 					     FUSB_REG_CONTROL0_HOST_CUR_MASK,
477 					     FUSB_REG_CONTROL0_HOST_CUR_DEF);
478 		break;
479 	case SRC_CURRENT_MEDIUM:
480 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
481 					     FUSB_REG_CONTROL0_HOST_CUR_MASK,
482 					     FUSB_REG_CONTROL0_HOST_CUR_MED);
483 		break;
484 	case SRC_CURRENT_HIGH:
485 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
486 					     FUSB_REG_CONTROL0_HOST_CUR_MASK,
487 					     FUSB_REG_CONTROL0_HOST_CUR_HIGH);
488 		break;
489 	default:
490 		break;
491 	}
492 
493 	return ret;
494 }
495 
496 static int fusb302_set_toggling(struct fusb302_chip *chip,
497 				enum toggling_mode mode)
498 {
499 	int ret = 0;
500 
501 	/* first disable toggling */
502 	ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
503 				     FUSB_REG_CONTROL2_TOGGLE);
504 	if (ret < 0)
505 		return ret;
506 	/* mask interrupts for SRC or SNK */
507 	ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
508 				   FUSB_REG_MASK_BC_LVL |
509 				   FUSB_REG_MASK_COMP_CHNG);
510 	if (ret < 0)
511 		return ret;
512 	chip->intr_bc_lvl = false;
513 	chip->intr_comp_chng = false;
514 	/* configure toggling mode: none/snk/src/drp */
515 	switch (mode) {
516 	case TOGGLING_MODE_OFF:
517 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
518 					     FUSB_REG_CONTROL2_MODE_MASK,
519 					     FUSB_REG_CONTROL2_MODE_NONE);
520 		if (ret < 0)
521 			return ret;
522 		break;
523 	case TOGGLING_MODE_SNK:
524 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
525 					     FUSB_REG_CONTROL2_MODE_MASK,
526 					     FUSB_REG_CONTROL2_MODE_UFP);
527 		if (ret < 0)
528 			return ret;
529 		break;
530 	case TOGGLING_MODE_SRC:
531 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
532 					     FUSB_REG_CONTROL2_MODE_MASK,
533 					     FUSB_REG_CONTROL2_MODE_DFP);
534 		if (ret < 0)
535 			return ret;
536 		break;
537 	case TOGGLING_MODE_DRP:
538 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
539 					     FUSB_REG_CONTROL2_MODE_MASK,
540 					     FUSB_REG_CONTROL2_MODE_DRP);
541 		if (ret < 0)
542 			return ret;
543 		break;
544 	default:
545 		break;
546 	}
547 
548 	if (mode == TOGGLING_MODE_OFF) {
549 		/* mask TOGDONE interrupt */
550 		ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
551 					   FUSB_REG_MASKA_TOGDONE);
552 		if (ret < 0)
553 			return ret;
554 		chip->intr_togdone = false;
555 	} else {
556 		/* Datasheet says vconn MUST be off when toggling */
557 		WARN(chip->vconn_on, "Vconn is on during toggle start");
558 		/* unmask TOGDONE interrupt */
559 		ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
560 					     FUSB_REG_MASKA_TOGDONE);
561 		if (ret < 0)
562 			return ret;
563 		chip->intr_togdone = true;
564 		/* start toggling */
565 		ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
566 					   FUSB_REG_CONTROL2_TOGGLE);
567 		if (ret < 0)
568 			return ret;
569 		/* during toggling, consider cc as Open */
570 		chip->cc1 = TYPEC_CC_OPEN;
571 		chip->cc2 = TYPEC_CC_OPEN;
572 	}
573 	chip->toggling_mode = mode;
574 
575 	return ret;
576 }
577 
578 static const char * const typec_cc_status_name[] = {
579 	[TYPEC_CC_OPEN]		= "Open",
580 	[TYPEC_CC_RA]		= "Ra",
581 	[TYPEC_CC_RD]		= "Rd",
582 	[TYPEC_CC_RP_DEF]	= "Rp-def",
583 	[TYPEC_CC_RP_1_5]	= "Rp-1.5",
584 	[TYPEC_CC_RP_3_0]	= "Rp-3.0",
585 };
586 
587 static const enum src_current_status cc_src_current[] = {
588 	[TYPEC_CC_OPEN]		= SRC_CURRENT_DEFAULT,
589 	[TYPEC_CC_RA]		= SRC_CURRENT_DEFAULT,
590 	[TYPEC_CC_RD]		= SRC_CURRENT_DEFAULT,
591 	[TYPEC_CC_RP_DEF]	= SRC_CURRENT_DEFAULT,
592 	[TYPEC_CC_RP_1_5]	= SRC_CURRENT_MEDIUM,
593 	[TYPEC_CC_RP_3_0]	= SRC_CURRENT_HIGH,
594 };
595 
596 static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
597 {
598 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
599 						 tcpc_dev);
600 	u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
601 			    FUSB_REG_SWITCHES0_CC2_PU_EN |
602 			    FUSB_REG_SWITCHES0_CC1_PD_EN |
603 			    FUSB_REG_SWITCHES0_CC2_PD_EN;
604 	u8 rd_mda, switches0_data = 0x00;
605 	int ret = 0;
606 
607 	mutex_lock(&chip->lock);
608 	switch (cc) {
609 	case TYPEC_CC_OPEN:
610 		break;
611 	case TYPEC_CC_RD:
612 		switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
613 				  FUSB_REG_SWITCHES0_CC2_PD_EN;
614 		break;
615 	case TYPEC_CC_RP_DEF:
616 	case TYPEC_CC_RP_1_5:
617 	case TYPEC_CC_RP_3_0:
618 		switches0_data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
619 				  FUSB_REG_SWITCHES0_CC1_PU_EN :
620 				  FUSB_REG_SWITCHES0_CC2_PU_EN;
621 		break;
622 	default:
623 		fusb302_log(chip, "unsupported cc value %s",
624 			    typec_cc_status_name[cc]);
625 		ret = -EINVAL;
626 		goto done;
627 	}
628 
629 	fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
630 
631 	ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
632 	if (ret < 0) {
633 		fusb302_log(chip, "cannot set toggling mode, ret=%d", ret);
634 		goto done;
635 	}
636 
637 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
638 				     switches0_mask, switches0_data);
639 	if (ret < 0) {
640 		fusb302_log(chip, "cannot set pull-up/-down, ret = %d", ret);
641 		goto done;
642 	}
643 	/* reset the cc status */
644 	chip->cc1 = TYPEC_CC_OPEN;
645 	chip->cc2 = TYPEC_CC_OPEN;
646 
647 	/* adjust current for SRC */
648 	ret = fusb302_set_src_current(chip, cc_src_current[cc]);
649 	if (ret < 0) {
650 		fusb302_log(chip, "cannot set src current %s, ret=%d",
651 			    typec_cc_status_name[cc], ret);
652 		goto done;
653 	}
654 
655 	/* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
656 	switch (cc) {
657 	case TYPEC_CC_RP_DEF:
658 	case TYPEC_CC_RP_1_5:
659 	case TYPEC_CC_RP_3_0:
660 		rd_mda = rd_mda_value[cc_src_current[cc]];
661 		ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
662 		if (ret < 0) {
663 			fusb302_log(chip,
664 				    "cannot set SRC measure value, ret=%d",
665 				    ret);
666 			goto done;
667 		}
668 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
669 					     FUSB_REG_MASK_BC_LVL |
670 					     FUSB_REG_MASK_COMP_CHNG,
671 					     FUSB_REG_MASK_COMP_CHNG);
672 		if (ret < 0) {
673 			fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
674 				    ret);
675 			goto done;
676 		}
677 		chip->intr_comp_chng = true;
678 		break;
679 	case TYPEC_CC_RD:
680 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
681 					     FUSB_REG_MASK_BC_LVL |
682 					     FUSB_REG_MASK_COMP_CHNG,
683 					     FUSB_REG_MASK_BC_LVL);
684 		if (ret < 0) {
685 			fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
686 				    ret);
687 			goto done;
688 		}
689 		chip->intr_bc_lvl = true;
690 		break;
691 	default:
692 		break;
693 	}
694 done:
695 	mutex_unlock(&chip->lock);
696 
697 	return ret;
698 }
699 
700 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
701 		       enum typec_cc_status *cc2)
702 {
703 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
704 						 tcpc_dev);
705 
706 	mutex_lock(&chip->lock);
707 	*cc1 = chip->cc1;
708 	*cc2 = chip->cc2;
709 	fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
710 		    typec_cc_status_name[*cc2]);
711 	mutex_unlock(&chip->lock);
712 
713 	return 0;
714 }
715 
716 static int tcpm_set_polarity(struct tcpc_dev *dev,
717 			     enum typec_cc_polarity polarity)
718 {
719 	return 0;
720 }
721 
722 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
723 {
724 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
725 						 tcpc_dev);
726 	int ret = 0;
727 	u8 switches0_data = 0x00;
728 	u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
729 			    FUSB_REG_SWITCHES0_VCONN_CC2;
730 
731 	mutex_lock(&chip->lock);
732 	if (chip->vconn_on == on) {
733 		fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
734 		goto done;
735 	}
736 	if (on) {
737 		switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
738 				 FUSB_REG_SWITCHES0_VCONN_CC2 :
739 				 FUSB_REG_SWITCHES0_VCONN_CC1;
740 	}
741 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
742 				     switches0_mask, switches0_data);
743 	if (ret < 0)
744 		goto done;
745 	chip->vconn_on = on;
746 	fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
747 done:
748 	mutex_unlock(&chip->lock);
749 
750 	return ret;
751 }
752 
753 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
754 {
755 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
756 						 tcpc_dev);
757 	int ret = 0;
758 
759 	mutex_lock(&chip->lock);
760 	if (chip->vbus_on == on) {
761 		fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
762 	} else {
763 		if (on)
764 			ret = regulator_enable(chip->vbus);
765 		else
766 			ret = regulator_disable(chip->vbus);
767 		if (ret < 0) {
768 			fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
769 				    on ? "enable" : "disable", ret);
770 			goto done;
771 		}
772 		chip->vbus_on = on;
773 		fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
774 	}
775 	if (chip->charge_on == charge)
776 		fusb302_log(chip, "charge is already %s",
777 			    charge ? "On" : "Off");
778 	else
779 		chip->charge_on = charge;
780 
781 done:
782 	mutex_unlock(&chip->lock);
783 
784 	return ret;
785 }
786 
787 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
788 {
789 	return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
790 				    FUSB_REG_CONTROL0_TX_FLUSH);
791 }
792 
793 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
794 {
795 	return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
796 				    FUSB_REG_CONTROL1_RX_FLUSH);
797 }
798 
799 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
800 {
801 	if (on)
802 		return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
803 					    FUSB_REG_SWITCHES1_AUTO_GCRC);
804 	return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
805 					    FUSB_REG_SWITCHES1_AUTO_GCRC);
806 }
807 
808 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
809 {
810 	int ret = 0;
811 	u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
812 	u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
813 			      FUSB_REG_MASKA_HARDSENT |
814 			      FUSB_REG_MASKA_TX_SUCCESS |
815 			      FUSB_REG_MASKA_HARDRESET;
816 	u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
817 
818 	ret = on ?
819 		fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
820 		fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
821 	if (ret < 0)
822 		return ret;
823 	ret = on ?
824 		fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
825 		fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
826 	if (ret < 0)
827 		return ret;
828 	ret = on ?
829 		fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
830 		fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
831 	return ret;
832 }
833 
834 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
835 {
836 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
837 						 tcpc_dev);
838 	int ret = 0;
839 
840 	mutex_lock(&chip->lock);
841 	ret = fusb302_pd_rx_flush(chip);
842 	if (ret < 0) {
843 		fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
844 		goto done;
845 	}
846 	ret = fusb302_pd_tx_flush(chip);
847 	if (ret < 0) {
848 		fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
849 		goto done;
850 	}
851 	ret = fusb302_pd_set_auto_goodcrc(chip, on);
852 	if (ret < 0) {
853 		fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
854 			    on ? "on" : "off", ret);
855 		goto done;
856 	}
857 	ret = fusb302_pd_set_interrupts(chip, on);
858 	if (ret < 0) {
859 		fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
860 			    on ? "on" : "off", ret);
861 		goto done;
862 	}
863 	fusb302_log(chip, "pd := %s", on ? "on" : "off");
864 done:
865 	mutex_unlock(&chip->lock);
866 
867 	return ret;
868 }
869 
870 static const char * const typec_role_name[] = {
871 	[TYPEC_SINK]		= "Sink",
872 	[TYPEC_SOURCE]		= "Source",
873 };
874 
875 static const char * const typec_data_role_name[] = {
876 	[TYPEC_DEVICE]		= "Device",
877 	[TYPEC_HOST]		= "Host",
878 };
879 
880 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
881 			  enum typec_role pwr, enum typec_data_role data)
882 {
883 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
884 						 tcpc_dev);
885 	int ret = 0;
886 	u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
887 			    FUSB_REG_SWITCHES1_DATAROLE;
888 	u8 switches1_data = 0x00;
889 
890 	mutex_lock(&chip->lock);
891 	if (pwr == TYPEC_SOURCE)
892 		switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
893 	if (data == TYPEC_HOST)
894 		switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
895 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
896 				     switches1_mask, switches1_data);
897 	if (ret < 0) {
898 		fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
899 			    typec_role_name[pwr], typec_data_role_name[data],
900 			    ret);
901 		goto done;
902 	}
903 	fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
904 		    typec_data_role_name[data]);
905 done:
906 	mutex_unlock(&chip->lock);
907 
908 	return ret;
909 }
910 
911 static int tcpm_start_toggling(struct tcpc_dev *dev,
912 			       enum typec_port_type port_type,
913 			       enum typec_cc_status cc)
914 {
915 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
916 						 tcpc_dev);
917 	enum toggling_mode mode = TOGGLING_MODE_OFF;
918 	int ret = 0;
919 
920 	switch (port_type) {
921 	case TYPEC_PORT_SRC:
922 		mode = TOGGLING_MODE_SRC;
923 		break;
924 	case TYPEC_PORT_SNK:
925 		mode = TOGGLING_MODE_SNK;
926 		break;
927 	case TYPEC_PORT_DRP:
928 		mode = TOGGLING_MODE_DRP;
929 		break;
930 	}
931 
932 	mutex_lock(&chip->lock);
933 	ret = fusb302_set_src_current(chip, cc_src_current[cc]);
934 	if (ret < 0) {
935 		fusb302_log(chip, "unable to set src current %s, ret=%d",
936 			    typec_cc_status_name[cc], ret);
937 		goto done;
938 	}
939 	ret = fusb302_set_toggling(chip, mode);
940 	if (ret < 0) {
941 		fusb302_log(chip,
942 			    "unable to start drp toggling, ret=%d", ret);
943 		goto done;
944 	}
945 	fusb302_log(chip, "start drp toggling");
946 done:
947 	mutex_unlock(&chip->lock);
948 
949 	return ret;
950 }
951 
952 static int fusb302_pd_send_message(struct fusb302_chip *chip,
953 				   const struct pd_message *msg)
954 {
955 	int ret = 0;
956 	u8 buf[40];
957 	u8 pos = 0;
958 	int len;
959 
960 	/* SOP tokens */
961 	buf[pos++] = FUSB302_TKN_SYNC1;
962 	buf[pos++] = FUSB302_TKN_SYNC1;
963 	buf[pos++] = FUSB302_TKN_SYNC1;
964 	buf[pos++] = FUSB302_TKN_SYNC2;
965 
966 	len = pd_header_cnt_le(msg->header) * 4;
967 	/* plug 2 for header */
968 	len += 2;
969 	if (len > 0x1F) {
970 		fusb302_log(chip,
971 			    "PD message too long %d (incl. header)", len);
972 		return -EINVAL;
973 	}
974 	/* packsym tells the FUSB302 chip that the next X bytes are payload */
975 	buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
976 	memcpy(&buf[pos], &msg->header, sizeof(msg->header));
977 	pos += sizeof(msg->header);
978 
979 	len -= 2;
980 	memcpy(&buf[pos], msg->payload, len);
981 	pos += len;
982 
983 	/* CRC */
984 	buf[pos++] = FUSB302_TKN_JAMCRC;
985 	/* EOP */
986 	buf[pos++] = FUSB302_TKN_EOP;
987 	/* turn tx off after sending message */
988 	buf[pos++] = FUSB302_TKN_TXOFF;
989 	/* start transmission */
990 	buf[pos++] = FUSB302_TKN_TXON;
991 
992 	ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
993 	if (ret < 0)
994 		return ret;
995 	fusb302_log(chip, "sending PD message header: %x", msg->header);
996 	fusb302_log(chip, "sending PD message len: %d", len);
997 
998 	return ret;
999 }
1000 
1001 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1002 {
1003 	return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1004 				    FUSB_REG_CONTROL3_SEND_HARDRESET);
1005 }
1006 
1007 static const char * const transmit_type_name[] = {
1008 	[TCPC_TX_SOP]			= "SOP",
1009 	[TCPC_TX_SOP_PRIME]		= "SOP'",
1010 	[TCPC_TX_SOP_PRIME_PRIME]	= "SOP''",
1011 	[TCPC_TX_SOP_DEBUG_PRIME]	= "DEBUG'",
1012 	[TCPC_TX_SOP_DEBUG_PRIME_PRIME]	= "DEBUG''",
1013 	[TCPC_TX_HARD_RESET]		= "HARD_RESET",
1014 	[TCPC_TX_CABLE_RESET]		= "CABLE_RESET",
1015 	[TCPC_TX_BIST_MODE_2]		= "BIST_MODE_2",
1016 };
1017 
1018 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1019 			    const struct pd_message *msg)
1020 {
1021 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1022 						 tcpc_dev);
1023 	int ret = 0;
1024 
1025 	mutex_lock(&chip->lock);
1026 	switch (type) {
1027 	case TCPC_TX_SOP:
1028 		ret = fusb302_pd_send_message(chip, msg);
1029 		if (ret < 0)
1030 			fusb302_log(chip,
1031 				    "cannot send PD message, ret=%d", ret);
1032 		break;
1033 	case TCPC_TX_HARD_RESET:
1034 		ret = fusb302_pd_send_hardreset(chip);
1035 		if (ret < 0)
1036 			fusb302_log(chip,
1037 				    "cannot send hardreset, ret=%d", ret);
1038 		break;
1039 	default:
1040 		fusb302_log(chip, "type %s not supported",
1041 			    transmit_type_name[type]);
1042 		ret = -EINVAL;
1043 	}
1044 	mutex_unlock(&chip->lock);
1045 
1046 	return ret;
1047 }
1048 
1049 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1050 {
1051 	if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1052 		return TYPEC_CC_RP_3_0;
1053 	if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1054 		return TYPEC_CC_RP_1_5;
1055 	if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1056 		return TYPEC_CC_RP_DEF;
1057 	return TYPEC_CC_OPEN;
1058 }
1059 
1060 static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1061 {
1062 	struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1063 						 bc_lvl_handler.work);
1064 	int ret = 0;
1065 	u8 status0;
1066 	u8 bc_lvl;
1067 	enum typec_cc_status cc_status;
1068 
1069 	mutex_lock(&chip->lock);
1070 	if (!chip->intr_bc_lvl) {
1071 		fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1072 		goto done;
1073 	}
1074 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1075 	if (ret < 0)
1076 		goto done;
1077 	fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1078 	if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1079 		fusb302_log(chip, "CC activities detected, delay handling");
1080 		mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1081 				 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1082 		goto done;
1083 	}
1084 	bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1085 	cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1086 	if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1087 		if (chip->cc1 != cc_status) {
1088 			fusb302_log(chip, "cc1: %s -> %s",
1089 				    typec_cc_status_name[chip->cc1],
1090 				    typec_cc_status_name[cc_status]);
1091 			chip->cc1 = cc_status;
1092 			tcpm_cc_change(chip->tcpm_port);
1093 		}
1094 	} else {
1095 		if (chip->cc2 != cc_status) {
1096 			fusb302_log(chip, "cc2: %s -> %s",
1097 				    typec_cc_status_name[chip->cc2],
1098 				    typec_cc_status_name[cc_status]);
1099 			chip->cc2 = cc_status;
1100 			tcpm_cc_change(chip->tcpm_port);
1101 		}
1102 	}
1103 
1104 done:
1105 	mutex_unlock(&chip->lock);
1106 }
1107 
1108 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1109 {
1110 	fusb302_tcpc_dev->init = tcpm_init;
1111 	fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1112 	fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
1113 	fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1114 	fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1115 	fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1116 	fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1117 	fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1118 	fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1119 	fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1120 	fusb302_tcpc_dev->start_toggling = tcpm_start_toggling;
1121 	fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1122 }
1123 
1124 static const char * const cc_polarity_name[] = {
1125 	[TYPEC_POLARITY_CC1]	= "Polarity_CC1",
1126 	[TYPEC_POLARITY_CC2]	= "Polarity_CC2",
1127 };
1128 
1129 static int fusb302_set_cc_polarity_and_pull(struct fusb302_chip *chip,
1130 					    enum typec_cc_polarity cc_polarity,
1131 					    bool pull_up, bool pull_down)
1132 {
1133 	int ret = 0;
1134 	u8 switches0_data = 0x00;
1135 	u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
1136 			    FUSB_REG_SWITCHES1_TXCC2_EN;
1137 	u8 switches1_data = 0x00;
1138 
1139 	if (pull_down)
1140 		switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
1141 				  FUSB_REG_SWITCHES0_CC2_PD_EN;
1142 
1143 	if (cc_polarity == TYPEC_POLARITY_CC1) {
1144 		switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC1;
1145 		if (chip->vconn_on)
1146 			switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1147 		if (pull_up)
1148 			switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1149 		switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1150 	} else {
1151 		switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC2;
1152 		if (chip->vconn_on)
1153 			switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1154 		if (pull_up)
1155 			switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1156 		switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1157 	}
1158 	ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1159 	if (ret < 0)
1160 		return ret;
1161 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1162 				     switches1_mask, switches1_data);
1163 	if (ret < 0)
1164 		return ret;
1165 	chip->cc_polarity = cc_polarity;
1166 
1167 	return ret;
1168 }
1169 
1170 static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1171 				      u8 togdone_result)
1172 {
1173 	int ret = 0;
1174 	u8 status0;
1175 	u8 bc_lvl;
1176 	enum typec_cc_polarity cc_polarity;
1177 	enum typec_cc_status cc_status_active, cc1, cc2;
1178 
1179 	/* set polarity and pull_up, pull_down */
1180 	cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1181 		      TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1182 	ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, false, true);
1183 	if (ret < 0) {
1184 		fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1185 			    cc_polarity_name[cc_polarity], ret);
1186 		return ret;
1187 	}
1188 	/* fusb302_set_cc_polarity() has set the correct measure block */
1189 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1190 	if (ret < 0)
1191 		return ret;
1192 	bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1193 	cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1194 	/* restart toggling if the cc status on the active line is OPEN */
1195 	if (cc_status_active == TYPEC_CC_OPEN) {
1196 		fusb302_log(chip, "restart toggling as CC_OPEN detected");
1197 		ret = fusb302_set_toggling(chip, chip->toggling_mode);
1198 		return ret;
1199 	}
1200 	/* update tcpm with the new cc value */
1201 	cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1202 	      cc_status_active : TYPEC_CC_OPEN;
1203 	cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1204 	      cc_status_active : TYPEC_CC_OPEN;
1205 	if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1206 		chip->cc1 = cc1;
1207 		chip->cc2 = cc2;
1208 		tcpm_cc_change(chip->tcpm_port);
1209 	}
1210 	/* turn off toggling */
1211 	ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1212 	if (ret < 0) {
1213 		fusb302_log(chip,
1214 			    "cannot set toggling mode off, ret=%d", ret);
1215 		return ret;
1216 	}
1217 	/* unmask bc_lvl interrupt */
1218 	ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1219 	if (ret < 0) {
1220 		fusb302_log(chip,
1221 			    "cannot unmask bc_lcl interrupt, ret=%d", ret);
1222 		return ret;
1223 	}
1224 	chip->intr_bc_lvl = true;
1225 	fusb302_log(chip, "detected cc1=%s, cc2=%s",
1226 		    typec_cc_status_name[cc1],
1227 		    typec_cc_status_name[cc2]);
1228 
1229 	return ret;
1230 }
1231 
1232 /* On error returns < 0, otherwise a typec_cc_status value */
1233 static int fusb302_get_src_cc_status(struct fusb302_chip *chip,
1234 				     enum typec_cc_polarity cc_polarity,
1235 				     enum typec_cc_status *cc)
1236 {
1237 	u8 ra_mda = ra_mda_value[chip->src_current_status];
1238 	u8 rd_mda = rd_mda_value[chip->src_current_status];
1239 	u8 switches0_data, status0;
1240 	int ret;
1241 
1242 	/* Step 1: Set switches so that we measure the right CC pin */
1243 	switches0_data = (cc_polarity == TYPEC_POLARITY_CC1) ?
1244 		FUSB_REG_SWITCHES0_CC1_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC1 :
1245 		FUSB_REG_SWITCHES0_CC2_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC2;
1246 	ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1247 	if (ret < 0)
1248 		return ret;
1249 
1250 	fusb302_i2c_read(chip, FUSB_REG_SWITCHES0, &status0);
1251 	fusb302_log(chip, "get_src_cc_status switches: 0x%0x", status0);
1252 
1253 	/* Step 2: Set compararator volt to differentiate between Open and Rd */
1254 	ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1255 	if (ret < 0)
1256 		return ret;
1257 
1258 	usleep_range(50, 100);
1259 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1260 	if (ret < 0)
1261 		return ret;
1262 
1263 	fusb302_log(chip, "get_src_cc_status rd_mda status0: 0x%0x", status0);
1264 	if (status0 & FUSB_REG_STATUS0_COMP) {
1265 		*cc = TYPEC_CC_OPEN;
1266 		return 0;
1267 	}
1268 
1269 	/* Step 3: Set compararator input to differentiate between Rd and Ra. */
1270 	ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1271 	if (ret < 0)
1272 		return ret;
1273 
1274 	usleep_range(50, 100);
1275 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1276 	if (ret < 0)
1277 		return ret;
1278 
1279 	fusb302_log(chip, "get_src_cc_status ra_mda status0: 0x%0x", status0);
1280 	if (status0 & FUSB_REG_STATUS0_COMP)
1281 		*cc = TYPEC_CC_RD;
1282 	else
1283 		*cc = TYPEC_CC_RA;
1284 
1285 	return 0;
1286 }
1287 
1288 static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1289 				      u8 togdone_result)
1290 {
1291 	/*
1292 	 * - set polarity (measure cc, vconn, tx)
1293 	 * - set pull_up, pull_down
1294 	 * - set cc1, cc2, and update to tcpm_port
1295 	 * - set I_COMP interrupt on
1296 	 */
1297 	int ret = 0;
1298 	u8 rd_mda = rd_mda_value[chip->src_current_status];
1299 	enum toggling_mode toggling_mode = chip->toggling_mode;
1300 	enum typec_cc_polarity cc_polarity;
1301 	enum typec_cc_status cc1, cc2;
1302 
1303 	/*
1304 	 * The toggle-engine will stop in a src state if it sees either Ra or
1305 	 * Rd. Determine the status for both CC pins, starting with the one
1306 	 * where toggling stopped, as that is where the switches point now.
1307 	 */
1308 	if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1309 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1310 	else
1311 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1312 	if (ret < 0)
1313 		return ret;
1314 	/* we must turn off toggling before we can measure the other pin */
1315 	ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1316 	if (ret < 0) {
1317 		fusb302_log(chip, "cannot set toggling mode off, ret=%d", ret);
1318 		return ret;
1319 	}
1320 	/* get the status of the other pin */
1321 	if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1322 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1323 	else
1324 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1325 	if (ret < 0)
1326 		return ret;
1327 
1328 	/* determine polarity based on the status of both pins */
1329 	if (cc1 == TYPEC_CC_RD &&
1330 			(cc2 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_RA)) {
1331 		cc_polarity = TYPEC_POLARITY_CC1;
1332 	} else if (cc2 == TYPEC_CC_RD &&
1333 		    (cc1 == TYPEC_CC_OPEN || cc1 == TYPEC_CC_RA)) {
1334 		cc_polarity = TYPEC_POLARITY_CC2;
1335 	} else {
1336 		fusb302_log(chip, "unexpected CC status cc1=%s, cc2=%s, restarting toggling",
1337 			    typec_cc_status_name[cc1],
1338 			    typec_cc_status_name[cc2]);
1339 		return fusb302_set_toggling(chip, toggling_mode);
1340 	}
1341 	/* set polarity and pull_up, pull_down */
1342 	ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, true, false);
1343 	if (ret < 0) {
1344 		fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1345 			    cc_polarity_name[cc_polarity], ret);
1346 		return ret;
1347 	}
1348 	/* update tcpm with the new cc value */
1349 	if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1350 		chip->cc1 = cc1;
1351 		chip->cc2 = cc2;
1352 		tcpm_cc_change(chip->tcpm_port);
1353 	}
1354 	/* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1355 	ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1356 	if (ret < 0)
1357 		return ret;
1358 	/* unmask comp_chng interrupt */
1359 	ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1360 				     FUSB_REG_MASK_COMP_CHNG);
1361 	if (ret < 0) {
1362 		fusb302_log(chip,
1363 			    "cannot unmask comp_chng interrupt, ret=%d", ret);
1364 		return ret;
1365 	}
1366 	chip->intr_comp_chng = true;
1367 	fusb302_log(chip, "detected cc1=%s, cc2=%s",
1368 		    typec_cc_status_name[cc1],
1369 		    typec_cc_status_name[cc2]);
1370 
1371 	return ret;
1372 }
1373 
1374 static int fusb302_handle_togdone(struct fusb302_chip *chip)
1375 {
1376 	int ret = 0;
1377 	u8 status1a;
1378 	u8 togdone_result;
1379 
1380 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1381 	if (ret < 0)
1382 		return ret;
1383 	togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1384 			 FUSB_REG_STATUS1A_TOGSS_MASK;
1385 	switch (togdone_result) {
1386 	case FUSB_REG_STATUS1A_TOGSS_SNK1:
1387 	case FUSB_REG_STATUS1A_TOGSS_SNK2:
1388 		return fusb302_handle_togdone_snk(chip, togdone_result);
1389 	case FUSB_REG_STATUS1A_TOGSS_SRC1:
1390 	case FUSB_REG_STATUS1A_TOGSS_SRC2:
1391 		return fusb302_handle_togdone_src(chip, togdone_result);
1392 	case FUSB_REG_STATUS1A_TOGSS_AA:
1393 		/* doesn't support */
1394 		fusb302_log(chip, "AudioAccessory not supported");
1395 		fusb302_set_toggling(chip, chip->toggling_mode);
1396 		break;
1397 	default:
1398 		fusb302_log(chip, "TOGDONE with an invalid state: %d",
1399 			    togdone_result);
1400 		fusb302_set_toggling(chip, chip->toggling_mode);
1401 		break;
1402 	}
1403 	return ret;
1404 }
1405 
1406 static int fusb302_pd_reset(struct fusb302_chip *chip)
1407 {
1408 	return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1409 				    FUSB_REG_RESET_PD_RESET);
1410 }
1411 
1412 static int fusb302_pd_read_message(struct fusb302_chip *chip,
1413 				   struct pd_message *msg)
1414 {
1415 	int ret = 0;
1416 	u8 token;
1417 	u8 crc[4];
1418 	int len;
1419 
1420 	/* first SOP token */
1421 	ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1422 	if (ret < 0)
1423 		return ret;
1424 	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1425 				     (u8 *)&msg->header);
1426 	if (ret < 0)
1427 		return ret;
1428 	len = pd_header_cnt_le(msg->header) * 4;
1429 	/* add 4 to length to include the CRC */
1430 	if (len > PD_MAX_PAYLOAD * 4) {
1431 		fusb302_log(chip, "PD message too long %d", len);
1432 		return -EINVAL;
1433 	}
1434 	if (len > 0) {
1435 		ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1436 					     (u8 *)msg->payload);
1437 		if (ret < 0)
1438 			return ret;
1439 	}
1440 	/* another 4 bytes to read CRC out */
1441 	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1442 	if (ret < 0)
1443 		return ret;
1444 	fusb302_log(chip, "PD message header: %x", msg->header);
1445 	fusb302_log(chip, "PD message len: %d", len);
1446 
1447 	/*
1448 	 * Check if we've read off a GoodCRC message. If so then indicate to
1449 	 * TCPM that the previous transmission has completed. Otherwise we pass
1450 	 * the received message over to TCPM for processing.
1451 	 *
1452 	 * We make this check here instead of basing the reporting decision on
1453 	 * the IRQ event type, as it's possible for the chip to report the
1454 	 * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
1455 	 * to check the message type to ensure correct reporting to TCPM.
1456 	 */
1457 	if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
1458 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1459 	else
1460 		tcpm_pd_receive(chip->tcpm_port, msg);
1461 
1462 	return ret;
1463 }
1464 
1465 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1466 {
1467 	struct fusb302_chip *chip = dev_id;
1468 	unsigned long flags;
1469 
1470 	/* Disable our level triggered IRQ until our irq_work has cleared it */
1471 	disable_irq_nosync(chip->gpio_int_n_irq);
1472 
1473 	spin_lock_irqsave(&chip->irq_lock, flags);
1474 	if (chip->irq_suspended)
1475 		chip->irq_while_suspended = true;
1476 	else
1477 		schedule_work(&chip->irq_work);
1478 	spin_unlock_irqrestore(&chip->irq_lock, flags);
1479 
1480 	return IRQ_HANDLED;
1481 }
1482 
1483 static void fusb302_irq_work(struct work_struct *work)
1484 {
1485 	struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1486 						 irq_work);
1487 	int ret = 0;
1488 	u8 interrupt;
1489 	u8 interrupta;
1490 	u8 interruptb;
1491 	u8 status0;
1492 	bool vbus_present;
1493 	bool comp_result;
1494 	bool intr_togdone;
1495 	bool intr_bc_lvl;
1496 	bool intr_comp_chng;
1497 	struct pd_message pd_msg;
1498 
1499 	mutex_lock(&chip->lock);
1500 	/* grab a snapshot of intr flags */
1501 	intr_togdone = chip->intr_togdone;
1502 	intr_bc_lvl = chip->intr_bc_lvl;
1503 	intr_comp_chng = chip->intr_comp_chng;
1504 
1505 	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1506 	if (ret < 0)
1507 		goto done;
1508 	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1509 	if (ret < 0)
1510 		goto done;
1511 	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1512 	if (ret < 0)
1513 		goto done;
1514 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1515 	if (ret < 0)
1516 		goto done;
1517 	fusb302_log(chip,
1518 		    "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1519 		    interrupt, interrupta, interruptb, status0);
1520 
1521 	if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1522 		vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1523 		fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1524 			    vbus_present ? "On" : "Off");
1525 		if (vbus_present != chip->vbus_present) {
1526 			chip->vbus_present = vbus_present;
1527 			tcpm_vbus_change(chip->tcpm_port);
1528 		}
1529 	}
1530 
1531 	if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1532 		fusb302_log(chip, "IRQ: TOGDONE");
1533 		ret = fusb302_handle_togdone(chip);
1534 		if (ret < 0) {
1535 			fusb302_log(chip,
1536 				    "handle togdone error, ret=%d", ret);
1537 			goto done;
1538 		}
1539 	}
1540 
1541 	if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1542 		fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1543 		/*
1544 		 * as BC_LVL interrupt can be affected by PD activity,
1545 		 * apply delay to for the handler to wait for the PD
1546 		 * signaling to finish.
1547 		 */
1548 		mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1549 				 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1550 	}
1551 
1552 	if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1553 		comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1554 		fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1555 			    comp_result ? "true" : "false");
1556 		if (comp_result) {
1557 			/* cc level > Rd_threshold, detach */
1558 			chip->cc1 = TYPEC_CC_OPEN;
1559 			chip->cc2 = TYPEC_CC_OPEN;
1560 			tcpm_cc_change(chip->tcpm_port);
1561 		}
1562 	}
1563 
1564 	if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1565 		fusb302_log(chip, "IRQ: PD collision");
1566 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1567 	}
1568 
1569 	if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1570 		fusb302_log(chip, "IRQ: PD retry failed");
1571 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1572 	}
1573 
1574 	if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1575 		fusb302_log(chip, "IRQ: PD hardreset sent");
1576 		ret = fusb302_pd_reset(chip);
1577 		if (ret < 0) {
1578 			fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1579 			goto done;
1580 		}
1581 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1582 	}
1583 
1584 	if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1585 		fusb302_log(chip, "IRQ: PD tx success");
1586 		ret = fusb302_pd_read_message(chip, &pd_msg);
1587 		if (ret < 0) {
1588 			fusb302_log(chip,
1589 				    "cannot read in PD message, ret=%d", ret);
1590 			goto done;
1591 		}
1592 	}
1593 
1594 	if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1595 		fusb302_log(chip, "IRQ: PD received hardreset");
1596 		ret = fusb302_pd_reset(chip);
1597 		if (ret < 0) {
1598 			fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1599 			goto done;
1600 		}
1601 		tcpm_pd_hard_reset(chip->tcpm_port);
1602 	}
1603 
1604 	if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1605 		fusb302_log(chip, "IRQ: PD sent good CRC");
1606 		ret = fusb302_pd_read_message(chip, &pd_msg);
1607 		if (ret < 0) {
1608 			fusb302_log(chip,
1609 				    "cannot read in PD message, ret=%d", ret);
1610 			goto done;
1611 		}
1612 	}
1613 done:
1614 	mutex_unlock(&chip->lock);
1615 	enable_irq(chip->gpio_int_n_irq);
1616 }
1617 
1618 static int init_gpio(struct fusb302_chip *chip)
1619 {
1620 	struct device *dev = chip->dev;
1621 	int ret = 0;
1622 
1623 	chip->gpio_int_n = devm_gpiod_get(dev, "fcs,int_n", GPIOD_IN);
1624 	if (IS_ERR(chip->gpio_int_n)) {
1625 		dev_err(dev, "failed to request gpio_int_n\n");
1626 		return PTR_ERR(chip->gpio_int_n);
1627 	}
1628 	ret = gpiod_to_irq(chip->gpio_int_n);
1629 	if (ret < 0) {
1630 		dev_err(dev,
1631 			"cannot request IRQ for GPIO Int_N, ret=%d", ret);
1632 		return ret;
1633 	}
1634 	chip->gpio_int_n_irq = ret;
1635 	return 0;
1636 }
1637 
1638 #define PDO_FIXED_FLAGS \
1639 	(PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1640 
1641 static const u32 src_pdo[] = {
1642 	PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1643 };
1644 
1645 static const u32 snk_pdo[] = {
1646 	PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1647 };
1648 
1649 static const struct property_entry port_props[] = {
1650 	PROPERTY_ENTRY_STRING("data-role", "dual"),
1651 	PROPERTY_ENTRY_STRING("power-role", "dual"),
1652 	PROPERTY_ENTRY_STRING("try-power-role", "sink"),
1653 	PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
1654 	PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
1655 	PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
1656 	{ }
1657 };
1658 
1659 static struct fwnode_handle *fusb302_fwnode_get(struct device *dev)
1660 {
1661 	struct fwnode_handle *fwnode;
1662 
1663 	fwnode = device_get_named_child_node(dev, "connector");
1664 	if (!fwnode)
1665 		fwnode = fwnode_create_software_node(port_props, NULL);
1666 
1667 	return fwnode;
1668 }
1669 
1670 static int fusb302_probe(struct i2c_client *client,
1671 			 const struct i2c_device_id *id)
1672 {
1673 	struct fusb302_chip *chip;
1674 	struct i2c_adapter *adapter = client->adapter;
1675 	struct device *dev = &client->dev;
1676 	const char *name;
1677 	int ret = 0;
1678 
1679 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1680 		dev_err(&client->dev,
1681 			"I2C/SMBus block functionality not supported!\n");
1682 		return -ENODEV;
1683 	}
1684 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1685 	if (!chip)
1686 		return -ENOMEM;
1687 
1688 	chip->i2c_client = client;
1689 	chip->dev = &client->dev;
1690 	mutex_init(&chip->lock);
1691 
1692 	/*
1693 	 * Devicetree platforms should get extcon via phandle (not yet
1694 	 * supported). On ACPI platforms, we get the name from a device prop.
1695 	 * This device prop is for kernel internal use only and is expected
1696 	 * to be set by the platform code which also registers the i2c client
1697 	 * for the fusb302.
1698 	 */
1699 	if (device_property_read_string(dev, "linux,extcon-name", &name) == 0) {
1700 		chip->extcon = extcon_get_extcon_dev(name);
1701 		if (!chip->extcon)
1702 			return -EPROBE_DEFER;
1703 	}
1704 
1705 	chip->vbus = devm_regulator_get(chip->dev, "vbus");
1706 	if (IS_ERR(chip->vbus))
1707 		return PTR_ERR(chip->vbus);
1708 
1709 	chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1710 	if (!chip->wq)
1711 		return -ENOMEM;
1712 
1713 	spin_lock_init(&chip->irq_lock);
1714 	INIT_WORK(&chip->irq_work, fusb302_irq_work);
1715 	INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1716 	init_tcpc_dev(&chip->tcpc_dev);
1717 	fusb302_debugfs_init(chip);
1718 
1719 	if (client->irq) {
1720 		chip->gpio_int_n_irq = client->irq;
1721 	} else {
1722 		ret = init_gpio(chip);
1723 		if (ret < 0)
1724 			goto destroy_workqueue;
1725 	}
1726 
1727 	chip->tcpc_dev.fwnode = fusb302_fwnode_get(dev);
1728 	if (IS_ERR(chip->tcpc_dev.fwnode)) {
1729 		ret = PTR_ERR(chip->tcpc_dev.fwnode);
1730 		goto destroy_workqueue;
1731 	}
1732 
1733 	chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1734 	if (IS_ERR(chip->tcpm_port)) {
1735 		fwnode_handle_put(chip->tcpc_dev.fwnode);
1736 		ret = PTR_ERR(chip->tcpm_port);
1737 		if (ret != -EPROBE_DEFER)
1738 			dev_err(dev, "cannot register tcpm port, ret=%d", ret);
1739 		goto destroy_workqueue;
1740 	}
1741 
1742 	ret = request_irq(chip->gpio_int_n_irq, fusb302_irq_intn,
1743 			  IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1744 			  "fsc_interrupt_int_n", chip);
1745 	if (ret < 0) {
1746 		dev_err(dev, "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1747 		goto tcpm_unregister_port;
1748 	}
1749 	enable_irq_wake(chip->gpio_int_n_irq);
1750 	i2c_set_clientdata(client, chip);
1751 
1752 	return ret;
1753 
1754 tcpm_unregister_port:
1755 	tcpm_unregister_port(chip->tcpm_port);
1756 	fwnode_handle_put(chip->tcpc_dev.fwnode);
1757 destroy_workqueue:
1758 	fusb302_debugfs_exit(chip);
1759 	destroy_workqueue(chip->wq);
1760 
1761 	return ret;
1762 }
1763 
1764 static int fusb302_remove(struct i2c_client *client)
1765 {
1766 	struct fusb302_chip *chip = i2c_get_clientdata(client);
1767 
1768 	disable_irq_wake(chip->gpio_int_n_irq);
1769 	free_irq(chip->gpio_int_n_irq, chip);
1770 	cancel_work_sync(&chip->irq_work);
1771 	cancel_delayed_work_sync(&chip->bc_lvl_handler);
1772 	tcpm_unregister_port(chip->tcpm_port);
1773 	fwnode_handle_put(chip->tcpc_dev.fwnode);
1774 	destroy_workqueue(chip->wq);
1775 	fusb302_debugfs_exit(chip);
1776 
1777 	return 0;
1778 }
1779 
1780 static int fusb302_pm_suspend(struct device *dev)
1781 {
1782 	struct fusb302_chip *chip = dev->driver_data;
1783 	unsigned long flags;
1784 
1785 	spin_lock_irqsave(&chip->irq_lock, flags);
1786 	chip->irq_suspended = true;
1787 	spin_unlock_irqrestore(&chip->irq_lock, flags);
1788 
1789 	/* Make sure any pending irq_work is finished before the bus suspends */
1790 	flush_work(&chip->irq_work);
1791 	return 0;
1792 }
1793 
1794 static int fusb302_pm_resume(struct device *dev)
1795 {
1796 	struct fusb302_chip *chip = dev->driver_data;
1797 	unsigned long flags;
1798 
1799 	spin_lock_irqsave(&chip->irq_lock, flags);
1800 	if (chip->irq_while_suspended) {
1801 		schedule_work(&chip->irq_work);
1802 		chip->irq_while_suspended = false;
1803 	}
1804 	chip->irq_suspended = false;
1805 	spin_unlock_irqrestore(&chip->irq_lock, flags);
1806 
1807 	return 0;
1808 }
1809 
1810 static const struct of_device_id fusb302_dt_match[] = {
1811 	{.compatible = "fcs,fusb302"},
1812 	{},
1813 };
1814 MODULE_DEVICE_TABLE(of, fusb302_dt_match);
1815 
1816 static const struct i2c_device_id fusb302_i2c_device_id[] = {
1817 	{"typec_fusb302", 0},
1818 	{},
1819 };
1820 MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
1821 
1822 static const struct dev_pm_ops fusb302_pm_ops = {
1823 	.suspend = fusb302_pm_suspend,
1824 	.resume = fusb302_pm_resume,
1825 };
1826 
1827 static struct i2c_driver fusb302_driver = {
1828 	.driver = {
1829 		   .name = "typec_fusb302",
1830 		   .pm = &fusb302_pm_ops,
1831 		   .of_match_table = of_match_ptr(fusb302_dt_match),
1832 		   },
1833 	.probe = fusb302_probe,
1834 	.remove = fusb302_remove,
1835 	.id_table = fusb302_i2c_device_id,
1836 };
1837 module_i2c_driver(fusb302_driver);
1838 
1839 MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1840 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1841 MODULE_LICENSE("GPL");
1842