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