• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
fusb302_log_full(struct fusb302_chip * chip)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)
_fusb302_log(struct fusb302_chip * chip,const char * fmt,va_list args)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 __printf(2, 3)
fusb302_log(struct fusb302_chip * chip,const char * fmt,...)182 static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
183 {
184 	va_list args;
185 
186 	va_start(args, fmt);
187 	_fusb302_log(chip, fmt, args);
188 	va_end(args);
189 }
190 
fusb302_debug_show(struct seq_file * s,void * v)191 static int fusb302_debug_show(struct seq_file *s, void *v)
192 {
193 	struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
194 	int tail;
195 
196 	mutex_lock(&chip->logbuffer_lock);
197 	tail = chip->logbuffer_tail;
198 	while (tail != chip->logbuffer_head) {
199 		seq_printf(s, "%s\n", chip->logbuffer[tail]);
200 		tail = (tail + 1) % LOG_BUFFER_ENTRIES;
201 	}
202 	if (!seq_has_overflowed(s))
203 		chip->logbuffer_tail = tail;
204 	mutex_unlock(&chip->logbuffer_lock);
205 
206 	return 0;
207 }
208 DEFINE_SHOW_ATTRIBUTE(fusb302_debug);
209 
fusb302_debugfs_init(struct fusb302_chip * chip)210 static void fusb302_debugfs_init(struct fusb302_chip *chip)
211 {
212 	char name[NAME_MAX];
213 
214 	mutex_init(&chip->logbuffer_lock);
215 	snprintf(name, NAME_MAX, "fusb302-%s", dev_name(chip->dev));
216 	chip->dentry = debugfs_create_file(name, S_IFREG | 0444, usb_debug_root,
217 					   chip, &fusb302_debug_fops);
218 }
219 
fusb302_debugfs_exit(struct fusb302_chip * chip)220 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
221 {
222 	debugfs_remove(chip->dentry);
223 }
224 
225 #else
226 
fusb302_log(const struct fusb302_chip * chip,const char * fmt,...)227 static void fusb302_log(const struct fusb302_chip *chip,
228 			const char *fmt, ...) { }
fusb302_debugfs_init(const struct fusb302_chip * chip)229 static void fusb302_debugfs_init(const struct fusb302_chip *chip) { }
fusb302_debugfs_exit(const struct fusb302_chip * chip)230 static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
231 
232 #endif
233 
fusb302_i2c_write(struct fusb302_chip * chip,u8 address,u8 data)234 static int fusb302_i2c_write(struct fusb302_chip *chip,
235 			     u8 address, u8 data)
236 {
237 	int ret = 0;
238 
239 	ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
240 	if (ret < 0)
241 		fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
242 			    data, address, ret);
243 
244 	return ret;
245 }
246 
fusb302_i2c_block_write(struct fusb302_chip * chip,u8 address,u8 length,const u8 * data)247 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
248 				   u8 length, const u8 *data)
249 {
250 	int ret = 0;
251 
252 	if (length <= 0)
253 		return ret;
254 
255 	ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
256 					     length, data);
257 	if (ret < 0)
258 		fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
259 			    address, length, ret);
260 
261 	return ret;
262 }
263 
fusb302_i2c_read(struct fusb302_chip * chip,u8 address,u8 * data)264 static int fusb302_i2c_read(struct fusb302_chip *chip,
265 			    u8 address, u8 *data)
266 {
267 	int ret = 0;
268 
269 	ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
270 	*data = (u8)ret;
271 	if (ret < 0)
272 		fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
273 
274 	return ret;
275 }
276 
fusb302_i2c_block_read(struct fusb302_chip * chip,u8 address,u8 length,u8 * data)277 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
278 				  u8 length, u8 *data)
279 {
280 	int ret = 0;
281 
282 	if (length <= 0)
283 		return ret;
284 
285 	ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
286 					    length, data);
287 	if (ret < 0) {
288 		fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
289 			    address, length, ret);
290 		goto done;
291 	}
292 	if (ret != length) {
293 		fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
294 			    ret, length, address);
295 		ret = -EIO;
296 	}
297 
298 done:
299 	return ret;
300 }
301 
fusb302_i2c_mask_write(struct fusb302_chip * chip,u8 address,u8 mask,u8 value)302 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
303 				  u8 mask, u8 value)
304 {
305 	int ret = 0;
306 	u8 data;
307 
308 	ret = fusb302_i2c_read(chip, address, &data);
309 	if (ret < 0)
310 		return ret;
311 	data &= ~mask;
312 	data |= value;
313 	ret = fusb302_i2c_write(chip, address, data);
314 	if (ret < 0)
315 		return ret;
316 
317 	return ret;
318 }
319 
fusb302_i2c_set_bits(struct fusb302_chip * chip,u8 address,u8 set_bits)320 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
321 				u8 set_bits)
322 {
323 	return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
324 }
325 
fusb302_i2c_clear_bits(struct fusb302_chip * chip,u8 address,u8 clear_bits)326 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
327 				  u8 clear_bits)
328 {
329 	return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
330 }
331 
fusb302_sw_reset(struct fusb302_chip * chip)332 static int fusb302_sw_reset(struct fusb302_chip *chip)
333 {
334 	int ret = 0;
335 
336 	ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
337 				FUSB_REG_RESET_SW_RESET);
338 	if (ret < 0)
339 		fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
340 	else
341 		fusb302_log(chip, "sw reset");
342 
343 	return ret;
344 }
345 
fusb302_enable_tx_auto_retries(struct fusb302_chip * chip,u8 retry_count)346 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip, u8 retry_count)
347 {
348 	int ret = 0;
349 
350 	ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3, retry_count |
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  */
fusb302_init_interrupt(struct fusb302_chip * chip)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 
fusb302_set_power_mode(struct fusb302_chip * chip,u8 power_mode)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 
tcpm_init(struct tcpc_dev * dev)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, FUSB_REG_CONTROL3_N_RETRIES_3);
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 
tcpm_get_vbus(struct tcpc_dev * dev)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 
tcpm_get_current_limit(struct tcpc_dev * dev)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 
fusb302_set_src_current(struct fusb302_chip * chip,enum src_current_status status)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 
fusb302_set_toggling(struct fusb302_chip * chip,enum toggling_mode mode)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 
tcpm_set_cc(struct tcpc_dev * dev,enum typec_cc_status cc)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_BC_LVL);
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 		chip->intr_bc_lvl = false;
679 		break;
680 	case TYPEC_CC_RD:
681 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
682 					     FUSB_REG_MASK_BC_LVL |
683 					     FUSB_REG_MASK_COMP_CHNG,
684 					     FUSB_REG_MASK_COMP_CHNG);
685 		if (ret < 0) {
686 			fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
687 				    ret);
688 			goto done;
689 		}
690 		chip->intr_bc_lvl = true;
691 		chip->intr_comp_chng = false;
692 		break;
693 	default:
694 		break;
695 	}
696 done:
697 	mutex_unlock(&chip->lock);
698 
699 	return ret;
700 }
701 
tcpm_get_cc(struct tcpc_dev * dev,enum typec_cc_status * cc1,enum typec_cc_status * cc2)702 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
703 		       enum typec_cc_status *cc2)
704 {
705 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
706 						 tcpc_dev);
707 
708 	mutex_lock(&chip->lock);
709 	*cc1 = chip->cc1;
710 	*cc2 = chip->cc2;
711 	fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
712 		    typec_cc_status_name[*cc2]);
713 	mutex_unlock(&chip->lock);
714 
715 	return 0;
716 }
717 
tcpm_set_polarity(struct tcpc_dev * dev,enum typec_cc_polarity polarity)718 static int tcpm_set_polarity(struct tcpc_dev *dev,
719 			     enum typec_cc_polarity polarity)
720 {
721 	return 0;
722 }
723 
tcpm_set_vconn(struct tcpc_dev * dev,bool on)724 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
725 {
726 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
727 						 tcpc_dev);
728 	int ret = 0;
729 	u8 switches0_data = 0x00;
730 	u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
731 			    FUSB_REG_SWITCHES0_VCONN_CC2;
732 
733 	mutex_lock(&chip->lock);
734 	if (chip->vconn_on == on) {
735 		fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
736 		goto done;
737 	}
738 	if (on) {
739 		switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
740 				 FUSB_REG_SWITCHES0_VCONN_CC2 :
741 				 FUSB_REG_SWITCHES0_VCONN_CC1;
742 	}
743 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
744 				     switches0_mask, switches0_data);
745 	if (ret < 0)
746 		goto done;
747 	chip->vconn_on = on;
748 	fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
749 done:
750 	mutex_unlock(&chip->lock);
751 
752 	return ret;
753 }
754 
tcpm_set_vbus(struct tcpc_dev * dev,bool on,bool charge)755 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
756 {
757 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
758 						 tcpc_dev);
759 	int ret = 0;
760 
761 	mutex_lock(&chip->lock);
762 	if (chip->vbus_on == on) {
763 		fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
764 	} else {
765 		if (on)
766 			ret = regulator_enable(chip->vbus);
767 		else
768 			ret = regulator_disable(chip->vbus);
769 		if (ret < 0) {
770 			fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
771 				    on ? "enable" : "disable", ret);
772 			goto done;
773 		}
774 		chip->vbus_on = on;
775 		fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
776 	}
777 	if (chip->charge_on == charge)
778 		fusb302_log(chip, "charge is already %s",
779 			    charge ? "On" : "Off");
780 	else
781 		chip->charge_on = charge;
782 
783 done:
784 	mutex_unlock(&chip->lock);
785 
786 	return ret;
787 }
788 
fusb302_pd_tx_flush(struct fusb302_chip * chip)789 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
790 {
791 	return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
792 				    FUSB_REG_CONTROL0_TX_FLUSH);
793 }
794 
fusb302_pd_rx_flush(struct fusb302_chip * chip)795 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
796 {
797 	return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
798 				    FUSB_REG_CONTROL1_RX_FLUSH);
799 }
800 
fusb302_pd_set_auto_goodcrc(struct fusb302_chip * chip,bool on)801 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
802 {
803 	if (on)
804 		return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
805 					    FUSB_REG_SWITCHES1_AUTO_GCRC);
806 	return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
807 					    FUSB_REG_SWITCHES1_AUTO_GCRC);
808 }
809 
fusb302_pd_set_interrupts(struct fusb302_chip * chip,bool on)810 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
811 {
812 	int ret = 0;
813 	u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
814 	u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
815 			      FUSB_REG_MASKA_HARDSENT |
816 			      FUSB_REG_MASKA_TX_SUCCESS |
817 			      FUSB_REG_MASKA_HARDRESET;
818 	u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
819 
820 	ret = on ?
821 		fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
822 		fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
823 	if (ret < 0)
824 		return ret;
825 	ret = on ?
826 		fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
827 		fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
828 	if (ret < 0)
829 		return ret;
830 	ret = on ?
831 		fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
832 		fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
833 	return ret;
834 }
835 
tcpm_set_pd_rx(struct tcpc_dev * dev,bool on)836 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
837 {
838 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
839 						 tcpc_dev);
840 	int ret = 0;
841 
842 	mutex_lock(&chip->lock);
843 	ret = fusb302_pd_rx_flush(chip);
844 	if (ret < 0) {
845 		fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
846 		goto done;
847 	}
848 	ret = fusb302_pd_tx_flush(chip);
849 	if (ret < 0) {
850 		fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
851 		goto done;
852 	}
853 	ret = fusb302_pd_set_auto_goodcrc(chip, on);
854 	if (ret < 0) {
855 		fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
856 			    on ? "on" : "off", ret);
857 		goto done;
858 	}
859 	ret = fusb302_pd_set_interrupts(chip, on);
860 	if (ret < 0) {
861 		fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
862 			    on ? "on" : "off", ret);
863 		goto done;
864 	}
865 	fusb302_log(chip, "pd := %s", on ? "on" : "off");
866 done:
867 	mutex_unlock(&chip->lock);
868 
869 	return ret;
870 }
871 
872 static const char * const typec_role_name[] = {
873 	[TYPEC_SINK]		= "Sink",
874 	[TYPEC_SOURCE]		= "Source",
875 };
876 
877 static const char * const typec_data_role_name[] = {
878 	[TYPEC_DEVICE]		= "Device",
879 	[TYPEC_HOST]		= "Host",
880 };
881 
tcpm_set_roles(struct tcpc_dev * dev,bool attached,enum typec_role pwr,enum typec_data_role data)882 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
883 			  enum typec_role pwr, enum typec_data_role data)
884 {
885 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
886 						 tcpc_dev);
887 	int ret = 0;
888 	u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
889 			    FUSB_REG_SWITCHES1_DATAROLE;
890 	u8 switches1_data = 0x00;
891 
892 	mutex_lock(&chip->lock);
893 	if (pwr == TYPEC_SOURCE)
894 		switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
895 	if (data == TYPEC_HOST)
896 		switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
897 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
898 				     switches1_mask, switches1_data);
899 	if (ret < 0) {
900 		fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
901 			    typec_role_name[pwr], typec_data_role_name[data],
902 			    ret);
903 		goto done;
904 	}
905 	fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
906 		    typec_data_role_name[data]);
907 done:
908 	mutex_unlock(&chip->lock);
909 
910 	return ret;
911 }
912 
tcpm_start_toggling(struct tcpc_dev * dev,enum typec_port_type port_type,enum typec_cc_status cc)913 static int tcpm_start_toggling(struct tcpc_dev *dev,
914 			       enum typec_port_type port_type,
915 			       enum typec_cc_status cc)
916 {
917 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
918 						 tcpc_dev);
919 	enum toggling_mode mode = TOGGLING_MODE_OFF;
920 	int ret = 0;
921 
922 	switch (port_type) {
923 	case TYPEC_PORT_SRC:
924 		mode = TOGGLING_MODE_SRC;
925 		break;
926 	case TYPEC_PORT_SNK:
927 		mode = TOGGLING_MODE_SNK;
928 		break;
929 	case TYPEC_PORT_DRP:
930 		mode = TOGGLING_MODE_DRP;
931 		break;
932 	}
933 
934 	mutex_lock(&chip->lock);
935 	ret = fusb302_set_src_current(chip, cc_src_current[cc]);
936 	if (ret < 0) {
937 		fusb302_log(chip, "unable to set src current %s, ret=%d",
938 			    typec_cc_status_name[cc], ret);
939 		goto done;
940 	}
941 	ret = fusb302_set_toggling(chip, mode);
942 	if (ret < 0) {
943 		fusb302_log(chip,
944 			    "unable to start drp toggling, ret=%d", ret);
945 		goto done;
946 	}
947 	fusb302_log(chip, "start drp toggling");
948 done:
949 	mutex_unlock(&chip->lock);
950 
951 	return ret;
952 }
953 
fusb302_pd_send_message(struct fusb302_chip * chip,const struct pd_message * msg)954 static int fusb302_pd_send_message(struct fusb302_chip *chip,
955 				   const struct pd_message *msg)
956 {
957 	int ret = 0;
958 	u8 buf[40];
959 	u8 pos = 0;
960 	int len;
961 
962 	/* SOP tokens */
963 	buf[pos++] = FUSB302_TKN_SYNC1;
964 	buf[pos++] = FUSB302_TKN_SYNC1;
965 	buf[pos++] = FUSB302_TKN_SYNC1;
966 	buf[pos++] = FUSB302_TKN_SYNC2;
967 
968 	len = pd_header_cnt_le(msg->header) * 4;
969 	/* plug 2 for header */
970 	len += 2;
971 	if (len > 0x1F) {
972 		fusb302_log(chip,
973 			    "PD message too long %d (incl. header)", len);
974 		return -EINVAL;
975 	}
976 	/* packsym tells the FUSB302 chip that the next X bytes are payload */
977 	buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
978 	memcpy(&buf[pos], &msg->header, sizeof(msg->header));
979 	pos += sizeof(msg->header);
980 
981 	len -= 2;
982 	memcpy(&buf[pos], msg->payload, len);
983 	pos += len;
984 
985 	/* CRC */
986 	buf[pos++] = FUSB302_TKN_JAMCRC;
987 	/* EOP */
988 	buf[pos++] = FUSB302_TKN_EOP;
989 	/* turn tx off after sending message */
990 	buf[pos++] = FUSB302_TKN_TXOFF;
991 	/* start transmission */
992 	buf[pos++] = FUSB302_TKN_TXON;
993 
994 	ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
995 	if (ret < 0)
996 		return ret;
997 	fusb302_log(chip, "sending PD message header: %x", msg->header);
998 	fusb302_log(chip, "sending PD message len: %d", len);
999 
1000 	return ret;
1001 }
1002 
fusb302_pd_send_hardreset(struct fusb302_chip * chip)1003 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1004 {
1005 	return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1006 				    FUSB_REG_CONTROL3_SEND_HARDRESET);
1007 }
1008 
1009 static const char * const transmit_type_name[] = {
1010 	[TCPC_TX_SOP]			= "SOP",
1011 	[TCPC_TX_SOP_PRIME]		= "SOP'",
1012 	[TCPC_TX_SOP_PRIME_PRIME]	= "SOP''",
1013 	[TCPC_TX_SOP_DEBUG_PRIME]	= "DEBUG'",
1014 	[TCPC_TX_SOP_DEBUG_PRIME_PRIME]	= "DEBUG''",
1015 	[TCPC_TX_HARD_RESET]		= "HARD_RESET",
1016 	[TCPC_TX_CABLE_RESET]		= "CABLE_RESET",
1017 	[TCPC_TX_BIST_MODE_2]		= "BIST_MODE_2",
1018 };
1019 
tcpm_pd_transmit(struct tcpc_dev * dev,enum tcpm_transmit_type type,const struct pd_message * msg,unsigned int negotiated_rev)1020 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1021 			    const struct pd_message *msg, unsigned int negotiated_rev)
1022 {
1023 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1024 						 tcpc_dev);
1025 	int ret = 0;
1026 
1027 	mutex_lock(&chip->lock);
1028 	switch (type) {
1029 	case TCPC_TX_SOP:
1030 		/* nRetryCount 3 in P2.0 spec, whereas 2 in PD3.0 spec */
1031 		ret = fusb302_enable_tx_auto_retries(chip, negotiated_rev > PD_REV20 ?
1032 						     FUSB_REG_CONTROL3_N_RETRIES_2 :
1033 						     FUSB_REG_CONTROL3_N_RETRIES_3);
1034 		if (ret < 0)
1035 			fusb302_log(chip, "Cannot update retry count ret=%d", ret);
1036 
1037 		ret = fusb302_pd_send_message(chip, msg);
1038 		if (ret < 0)
1039 			fusb302_log(chip,
1040 				    "cannot send PD message, ret=%d", ret);
1041 		break;
1042 	case TCPC_TX_HARD_RESET:
1043 		ret = fusb302_pd_send_hardreset(chip);
1044 		if (ret < 0)
1045 			fusb302_log(chip,
1046 				    "cannot send hardreset, ret=%d", ret);
1047 		break;
1048 	default:
1049 		fusb302_log(chip, "type %s not supported",
1050 			    transmit_type_name[type]);
1051 		ret = -EINVAL;
1052 	}
1053 	mutex_unlock(&chip->lock);
1054 
1055 	return ret;
1056 }
1057 
fusb302_bc_lvl_to_cc(u8 bc_lvl)1058 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1059 {
1060 	if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1061 		return TYPEC_CC_RP_3_0;
1062 	if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1063 		return TYPEC_CC_RP_1_5;
1064 	if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1065 		return TYPEC_CC_RP_DEF;
1066 	return TYPEC_CC_OPEN;
1067 }
1068 
fusb302_bc_lvl_handler_work(struct work_struct * work)1069 static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1070 {
1071 	struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1072 						 bc_lvl_handler.work);
1073 	int ret = 0;
1074 	u8 status0;
1075 	u8 bc_lvl;
1076 	enum typec_cc_status cc_status;
1077 
1078 	mutex_lock(&chip->lock);
1079 	if (!chip->intr_bc_lvl) {
1080 		fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1081 		goto done;
1082 	}
1083 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1084 	if (ret < 0)
1085 		goto done;
1086 	fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1087 	if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1088 		fusb302_log(chip, "CC activities detected, delay handling");
1089 		mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1090 				 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1091 		goto done;
1092 	}
1093 	bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1094 	cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1095 	if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1096 		if (chip->cc1 != cc_status) {
1097 			fusb302_log(chip, "cc1: %s -> %s",
1098 				    typec_cc_status_name[chip->cc1],
1099 				    typec_cc_status_name[cc_status]);
1100 			chip->cc1 = cc_status;
1101 			tcpm_cc_change(chip->tcpm_port);
1102 		}
1103 	} else {
1104 		if (chip->cc2 != cc_status) {
1105 			fusb302_log(chip, "cc2: %s -> %s",
1106 				    typec_cc_status_name[chip->cc2],
1107 				    typec_cc_status_name[cc_status]);
1108 			chip->cc2 = cc_status;
1109 			tcpm_cc_change(chip->tcpm_port);
1110 		}
1111 	}
1112 
1113 done:
1114 	mutex_unlock(&chip->lock);
1115 }
1116 
init_tcpc_dev(struct tcpc_dev * fusb302_tcpc_dev)1117 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1118 {
1119 	fusb302_tcpc_dev->init = tcpm_init;
1120 	fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1121 	fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
1122 	fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1123 	fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1124 	fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1125 	fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1126 	fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1127 	fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1128 	fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1129 	fusb302_tcpc_dev->start_toggling = tcpm_start_toggling;
1130 	fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1131 }
1132 
1133 static const char * const cc_polarity_name[] = {
1134 	[TYPEC_POLARITY_CC1]	= "Polarity_CC1",
1135 	[TYPEC_POLARITY_CC2]	= "Polarity_CC2",
1136 };
1137 
fusb302_set_cc_polarity_and_pull(struct fusb302_chip * chip,enum typec_cc_polarity cc_polarity,bool pull_up,bool pull_down)1138 static int fusb302_set_cc_polarity_and_pull(struct fusb302_chip *chip,
1139 					    enum typec_cc_polarity cc_polarity,
1140 					    bool pull_up, bool pull_down)
1141 {
1142 	int ret = 0;
1143 	u8 switches0_data = 0x00;
1144 	u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
1145 			    FUSB_REG_SWITCHES1_TXCC2_EN;
1146 	u8 switches1_data = 0x00;
1147 
1148 	if (pull_down)
1149 		switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
1150 				  FUSB_REG_SWITCHES0_CC2_PD_EN;
1151 
1152 	if (cc_polarity == TYPEC_POLARITY_CC1) {
1153 		switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC1;
1154 		if (chip->vconn_on)
1155 			switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1156 		if (pull_up)
1157 			switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1158 		switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1159 	} else {
1160 		switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC2;
1161 		if (chip->vconn_on)
1162 			switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1163 		if (pull_up)
1164 			switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1165 		switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1166 	}
1167 	ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1168 	if (ret < 0)
1169 		return ret;
1170 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1171 				     switches1_mask, switches1_data);
1172 	if (ret < 0)
1173 		return ret;
1174 	chip->cc_polarity = cc_polarity;
1175 
1176 	return ret;
1177 }
1178 
fusb302_handle_togdone_snk(struct fusb302_chip * chip,u8 togdone_result)1179 static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1180 				      u8 togdone_result)
1181 {
1182 	int ret = 0;
1183 	u8 status0;
1184 	u8 bc_lvl;
1185 	enum typec_cc_polarity cc_polarity;
1186 	enum typec_cc_status cc_status_active, cc1, cc2;
1187 
1188 	/* set polarity and pull_up, pull_down */
1189 	cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1190 		      TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1191 	ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, false, true);
1192 	if (ret < 0) {
1193 		fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1194 			    cc_polarity_name[cc_polarity], ret);
1195 		return ret;
1196 	}
1197 	/* fusb302_set_cc_polarity() has set the correct measure block */
1198 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1199 	if (ret < 0)
1200 		return ret;
1201 	bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1202 	cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1203 	/* restart toggling if the cc status on the active line is OPEN */
1204 	if (cc_status_active == TYPEC_CC_OPEN) {
1205 		fusb302_log(chip, "restart toggling as CC_OPEN detected");
1206 		ret = fusb302_set_toggling(chip, chip->toggling_mode);
1207 		return ret;
1208 	}
1209 	/* update tcpm with the new cc value */
1210 	cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1211 	      cc_status_active : TYPEC_CC_OPEN;
1212 	cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1213 	      cc_status_active : TYPEC_CC_OPEN;
1214 	if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1215 		chip->cc1 = cc1;
1216 		chip->cc2 = cc2;
1217 		tcpm_cc_change(chip->tcpm_port);
1218 	}
1219 	/* turn off toggling */
1220 	ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1221 	if (ret < 0) {
1222 		fusb302_log(chip,
1223 			    "cannot set toggling mode off, ret=%d", ret);
1224 		return ret;
1225 	}
1226 	/* unmask bc_lvl interrupt */
1227 	ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1228 	if (ret < 0) {
1229 		fusb302_log(chip,
1230 			    "cannot unmask bc_lcl interrupt, ret=%d", ret);
1231 		return ret;
1232 	}
1233 	chip->intr_bc_lvl = true;
1234 	fusb302_log(chip, "detected cc1=%s, cc2=%s",
1235 		    typec_cc_status_name[cc1],
1236 		    typec_cc_status_name[cc2]);
1237 
1238 	return ret;
1239 }
1240 
1241 /* 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)1242 static int fusb302_get_src_cc_status(struct fusb302_chip *chip,
1243 				     enum typec_cc_polarity cc_polarity,
1244 				     enum typec_cc_status *cc)
1245 {
1246 	u8 ra_mda = ra_mda_value[chip->src_current_status];
1247 	u8 rd_mda = rd_mda_value[chip->src_current_status];
1248 	u8 switches0_data, status0;
1249 	int ret;
1250 
1251 	/* Step 1: Set switches so that we measure the right CC pin */
1252 	switches0_data = (cc_polarity == TYPEC_POLARITY_CC1) ?
1253 		FUSB_REG_SWITCHES0_CC1_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC1 :
1254 		FUSB_REG_SWITCHES0_CC2_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC2;
1255 	ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1256 	if (ret < 0)
1257 		return ret;
1258 
1259 	fusb302_i2c_read(chip, FUSB_REG_SWITCHES0, &status0);
1260 	fusb302_log(chip, "get_src_cc_status switches: 0x%0x", status0);
1261 
1262 	/* Step 2: Set compararator volt to differentiate between Open and Rd */
1263 	ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1264 	if (ret < 0)
1265 		return ret;
1266 
1267 	usleep_range(50, 100);
1268 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1269 	if (ret < 0)
1270 		return ret;
1271 
1272 	fusb302_log(chip, "get_src_cc_status rd_mda status0: 0x%0x", status0);
1273 	if (status0 & FUSB_REG_STATUS0_COMP) {
1274 		*cc = TYPEC_CC_OPEN;
1275 		return 0;
1276 	}
1277 
1278 	/* Step 3: Set compararator input to differentiate between Rd and Ra. */
1279 	ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1280 	if (ret < 0)
1281 		return ret;
1282 
1283 	usleep_range(50, 100);
1284 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1285 	if (ret < 0)
1286 		return ret;
1287 
1288 	fusb302_log(chip, "get_src_cc_status ra_mda status0: 0x%0x", status0);
1289 	if (status0 & FUSB_REG_STATUS0_COMP)
1290 		*cc = TYPEC_CC_RD;
1291 	else
1292 		*cc = TYPEC_CC_RA;
1293 
1294 	return 0;
1295 }
1296 
fusb302_handle_togdone_src(struct fusb302_chip * chip,u8 togdone_result)1297 static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1298 				      u8 togdone_result)
1299 {
1300 	/*
1301 	 * - set polarity (measure cc, vconn, tx)
1302 	 * - set pull_up, pull_down
1303 	 * - set cc1, cc2, and update to tcpm_port
1304 	 * - set I_COMP interrupt on
1305 	 */
1306 	int ret = 0;
1307 	u8 rd_mda = rd_mda_value[chip->src_current_status];
1308 	enum toggling_mode toggling_mode = chip->toggling_mode;
1309 	enum typec_cc_polarity cc_polarity;
1310 	enum typec_cc_status cc1, cc2;
1311 
1312 	/*
1313 	 * The toggle-engine will stop in a src state if it sees either Ra or
1314 	 * Rd. Determine the status for both CC pins, starting with the one
1315 	 * where toggling stopped, as that is where the switches point now.
1316 	 */
1317 	if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1318 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1319 	else
1320 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1321 	if (ret < 0)
1322 		return ret;
1323 	/* we must turn off toggling before we can measure the other pin */
1324 	ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1325 	if (ret < 0) {
1326 		fusb302_log(chip, "cannot set toggling mode off, ret=%d", ret);
1327 		return ret;
1328 	}
1329 	/* get the status of the other pin */
1330 	if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1331 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1332 	else
1333 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1334 	if (ret < 0)
1335 		return ret;
1336 
1337 	/* determine polarity based on the status of both pins */
1338 	if (cc1 == TYPEC_CC_RD &&
1339 			(cc2 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_RA)) {
1340 		cc_polarity = TYPEC_POLARITY_CC1;
1341 	} else if (cc2 == TYPEC_CC_RD &&
1342 		    (cc1 == TYPEC_CC_OPEN || cc1 == TYPEC_CC_RA)) {
1343 		cc_polarity = TYPEC_POLARITY_CC2;
1344 	} else {
1345 		fusb302_log(chip, "unexpected CC status cc1=%s, cc2=%s, restarting toggling",
1346 			    typec_cc_status_name[cc1],
1347 			    typec_cc_status_name[cc2]);
1348 		return fusb302_set_toggling(chip, toggling_mode);
1349 	}
1350 	/* set polarity and pull_up, pull_down */
1351 	ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, true, false);
1352 	if (ret < 0) {
1353 		fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1354 			    cc_polarity_name[cc_polarity], ret);
1355 		return ret;
1356 	}
1357 	/* update tcpm with the new cc value */
1358 	if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1359 		chip->cc1 = cc1;
1360 		chip->cc2 = cc2;
1361 		tcpm_cc_change(chip->tcpm_port);
1362 	}
1363 	/* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1364 	ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1365 	if (ret < 0)
1366 		return ret;
1367 	/* unmask comp_chng interrupt */
1368 	ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1369 				     FUSB_REG_MASK_COMP_CHNG);
1370 	if (ret < 0) {
1371 		fusb302_log(chip,
1372 			    "cannot unmask comp_chng interrupt, ret=%d", ret);
1373 		return ret;
1374 	}
1375 	chip->intr_comp_chng = true;
1376 	fusb302_log(chip, "detected cc1=%s, cc2=%s",
1377 		    typec_cc_status_name[cc1],
1378 		    typec_cc_status_name[cc2]);
1379 
1380 	return ret;
1381 }
1382 
fusb302_handle_togdone(struct fusb302_chip * chip)1383 static int fusb302_handle_togdone(struct fusb302_chip *chip)
1384 {
1385 	int ret = 0;
1386 	u8 status1a;
1387 	u8 togdone_result;
1388 
1389 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1390 	if (ret < 0)
1391 		return ret;
1392 	togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1393 			 FUSB_REG_STATUS1A_TOGSS_MASK;
1394 	switch (togdone_result) {
1395 	case FUSB_REG_STATUS1A_TOGSS_SNK1:
1396 	case FUSB_REG_STATUS1A_TOGSS_SNK2:
1397 		return fusb302_handle_togdone_snk(chip, togdone_result);
1398 	case FUSB_REG_STATUS1A_TOGSS_SRC1:
1399 	case FUSB_REG_STATUS1A_TOGSS_SRC2:
1400 		return fusb302_handle_togdone_src(chip, togdone_result);
1401 	case FUSB_REG_STATUS1A_TOGSS_AA:
1402 		/* doesn't support */
1403 		fusb302_log(chip, "AudioAccessory not supported");
1404 		fusb302_set_toggling(chip, chip->toggling_mode);
1405 		break;
1406 	default:
1407 		fusb302_log(chip, "TOGDONE with an invalid state: %d",
1408 			    togdone_result);
1409 		fusb302_set_toggling(chip, chip->toggling_mode);
1410 		break;
1411 	}
1412 	return ret;
1413 }
1414 
fusb302_pd_reset(struct fusb302_chip * chip)1415 static int fusb302_pd_reset(struct fusb302_chip *chip)
1416 {
1417 	return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1418 				    FUSB_REG_RESET_PD_RESET);
1419 }
1420 
fusb302_pd_read_message(struct fusb302_chip * chip,struct pd_message * msg)1421 static int fusb302_pd_read_message(struct fusb302_chip *chip,
1422 				   struct pd_message *msg)
1423 {
1424 	int ret = 0;
1425 	u8 token;
1426 	u8 crc[4];
1427 	int len;
1428 
1429 	/* first SOP token */
1430 	ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1431 	if (ret < 0)
1432 		return ret;
1433 	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1434 				     (u8 *)&msg->header);
1435 	if (ret < 0)
1436 		return ret;
1437 	len = pd_header_cnt_le(msg->header) * 4;
1438 	/* add 4 to length to include the CRC */
1439 	if (len > PD_MAX_PAYLOAD * 4) {
1440 		fusb302_log(chip, "PD message too long %d", len);
1441 		return -EINVAL;
1442 	}
1443 	if (len > 0) {
1444 		ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1445 					     (u8 *)msg->payload);
1446 		if (ret < 0)
1447 			return ret;
1448 	}
1449 	/* another 4 bytes to read CRC out */
1450 	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1451 	if (ret < 0)
1452 		return ret;
1453 	fusb302_log(chip, "PD message header: %x", msg->header);
1454 	fusb302_log(chip, "PD message len: %d", len);
1455 
1456 	/*
1457 	 * Check if we've read off a GoodCRC message. If so then indicate to
1458 	 * TCPM that the previous transmission has completed. Otherwise we pass
1459 	 * the received message over to TCPM for processing.
1460 	 *
1461 	 * We make this check here instead of basing the reporting decision on
1462 	 * the IRQ event type, as it's possible for the chip to report the
1463 	 * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
1464 	 * to check the message type to ensure correct reporting to TCPM.
1465 	 */
1466 	if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
1467 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1468 	else
1469 		tcpm_pd_receive(chip->tcpm_port, msg);
1470 
1471 	return ret;
1472 }
1473 
fusb302_irq_intn(int irq,void * dev_id)1474 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1475 {
1476 	struct fusb302_chip *chip = dev_id;
1477 	unsigned long flags;
1478 
1479 	/* Disable our level triggered IRQ until our irq_work has cleared it */
1480 	disable_irq_nosync(chip->gpio_int_n_irq);
1481 
1482 	spin_lock_irqsave(&chip->irq_lock, flags);
1483 	if (chip->irq_suspended)
1484 		chip->irq_while_suspended = true;
1485 	else
1486 		schedule_work(&chip->irq_work);
1487 	spin_unlock_irqrestore(&chip->irq_lock, flags);
1488 
1489 	return IRQ_HANDLED;
1490 }
1491 
fusb302_irq_work(struct work_struct * work)1492 static void fusb302_irq_work(struct work_struct *work)
1493 {
1494 	struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1495 						 irq_work);
1496 	int ret = 0;
1497 	u8 interrupt;
1498 	u8 interrupta;
1499 	u8 interruptb;
1500 	u8 status0;
1501 	bool vbus_present;
1502 	bool comp_result;
1503 	bool intr_togdone;
1504 	bool intr_bc_lvl;
1505 	bool intr_comp_chng;
1506 	struct pd_message pd_msg;
1507 
1508 	mutex_lock(&chip->lock);
1509 	/* grab a snapshot of intr flags */
1510 	intr_togdone = chip->intr_togdone;
1511 	intr_bc_lvl = chip->intr_bc_lvl;
1512 	intr_comp_chng = chip->intr_comp_chng;
1513 
1514 	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1515 	if (ret < 0)
1516 		goto done;
1517 	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1518 	if (ret < 0)
1519 		goto done;
1520 	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1521 	if (ret < 0)
1522 		goto done;
1523 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1524 	if (ret < 0)
1525 		goto done;
1526 	fusb302_log(chip,
1527 		    "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1528 		    interrupt, interrupta, interruptb, status0);
1529 
1530 	if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1531 		vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1532 		fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1533 			    vbus_present ? "On" : "Off");
1534 		if (vbus_present != chip->vbus_present) {
1535 			chip->vbus_present = vbus_present;
1536 			tcpm_vbus_change(chip->tcpm_port);
1537 		}
1538 	}
1539 
1540 	if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1541 		fusb302_log(chip, "IRQ: TOGDONE");
1542 		ret = fusb302_handle_togdone(chip);
1543 		if (ret < 0) {
1544 			fusb302_log(chip,
1545 				    "handle togdone error, ret=%d", ret);
1546 			goto done;
1547 		}
1548 	}
1549 
1550 	if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1551 		fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1552 		/*
1553 		 * as BC_LVL interrupt can be affected by PD activity,
1554 		 * apply delay to for the handler to wait for the PD
1555 		 * signaling to finish.
1556 		 */
1557 		mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1558 				 msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1559 	}
1560 
1561 	if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1562 		comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1563 		fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1564 			    comp_result ? "true" : "false");
1565 		if (comp_result) {
1566 			/* cc level > Rd_threshold, detach */
1567 			chip->cc1 = TYPEC_CC_OPEN;
1568 			chip->cc2 = TYPEC_CC_OPEN;
1569 			tcpm_cc_change(chip->tcpm_port);
1570 		}
1571 	}
1572 
1573 	if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1574 		fusb302_log(chip, "IRQ: PD collision");
1575 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1576 	}
1577 
1578 	if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1579 		fusb302_log(chip, "IRQ: PD retry failed");
1580 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1581 	}
1582 
1583 	if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1584 		fusb302_log(chip, "IRQ: PD hardreset sent");
1585 		ret = fusb302_pd_reset(chip);
1586 		if (ret < 0) {
1587 			fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1588 			goto done;
1589 		}
1590 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1591 	}
1592 
1593 	if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1594 		fusb302_log(chip, "IRQ: PD tx success");
1595 		ret = fusb302_pd_read_message(chip, &pd_msg);
1596 		if (ret < 0) {
1597 			fusb302_log(chip,
1598 				    "cannot read in PD message, ret=%d", ret);
1599 			goto done;
1600 		}
1601 	}
1602 
1603 	if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1604 		fusb302_log(chip, "IRQ: PD received hardreset");
1605 		ret = fusb302_pd_reset(chip);
1606 		if (ret < 0) {
1607 			fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1608 			goto done;
1609 		}
1610 		tcpm_pd_hard_reset(chip->tcpm_port);
1611 	}
1612 
1613 	if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1614 		fusb302_log(chip, "IRQ: PD sent good CRC");
1615 		ret = fusb302_pd_read_message(chip, &pd_msg);
1616 		if (ret < 0) {
1617 			fusb302_log(chip,
1618 				    "cannot read in PD message, ret=%d", ret);
1619 			goto done;
1620 		}
1621 	}
1622 done:
1623 	mutex_unlock(&chip->lock);
1624 	enable_irq(chip->gpio_int_n_irq);
1625 }
1626 
init_gpio(struct fusb302_chip * chip)1627 static int init_gpio(struct fusb302_chip *chip)
1628 {
1629 	struct device *dev = chip->dev;
1630 	int ret = 0;
1631 
1632 	chip->gpio_int_n = devm_gpiod_get(dev, "fcs,int_n", GPIOD_IN);
1633 	if (IS_ERR(chip->gpio_int_n)) {
1634 		dev_err(dev, "failed to request gpio_int_n\n");
1635 		return PTR_ERR(chip->gpio_int_n);
1636 	}
1637 	ret = gpiod_to_irq(chip->gpio_int_n);
1638 	if (ret < 0) {
1639 		dev_err(dev,
1640 			"cannot request IRQ for GPIO Int_N, ret=%d", ret);
1641 		return ret;
1642 	}
1643 	chip->gpio_int_n_irq = ret;
1644 	return 0;
1645 }
1646 
1647 #define PDO_FIXED_FLAGS \
1648 	(PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1649 
1650 static const u32 src_pdo[] = {
1651 	PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1652 };
1653 
1654 static const u32 snk_pdo[] = {
1655 	PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1656 };
1657 
1658 static const struct property_entry port_props[] = {
1659 	PROPERTY_ENTRY_STRING("data-role", "dual"),
1660 	PROPERTY_ENTRY_STRING("power-role", "dual"),
1661 	PROPERTY_ENTRY_STRING("try-power-role", "sink"),
1662 	PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
1663 	PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
1664 	PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
1665 	{ }
1666 };
1667 
fusb302_fwnode_get(struct device * dev)1668 static struct fwnode_handle *fusb302_fwnode_get(struct device *dev)
1669 {
1670 	struct fwnode_handle *fwnode;
1671 
1672 	fwnode = device_get_named_child_node(dev, "connector");
1673 	if (!fwnode)
1674 		fwnode = fwnode_create_software_node(port_props, NULL);
1675 
1676 	return fwnode;
1677 }
1678 
fusb302_probe(struct i2c_client * client,const struct i2c_device_id * id)1679 static int fusb302_probe(struct i2c_client *client,
1680 			 const struct i2c_device_id *id)
1681 {
1682 	struct fusb302_chip *chip;
1683 	struct i2c_adapter *adapter = client->adapter;
1684 	struct device *dev = &client->dev;
1685 	const char *name;
1686 	int ret = 0;
1687 
1688 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1689 		dev_err(&client->dev,
1690 			"I2C/SMBus block functionality not supported!\n");
1691 		return -ENODEV;
1692 	}
1693 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1694 	if (!chip)
1695 		return -ENOMEM;
1696 
1697 	chip->i2c_client = client;
1698 	chip->dev = &client->dev;
1699 	mutex_init(&chip->lock);
1700 
1701 	/*
1702 	 * Devicetree platforms should get extcon via phandle (not yet
1703 	 * supported). On ACPI platforms, we get the name from a device prop.
1704 	 * This device prop is for kernel internal use only and is expected
1705 	 * to be set by the platform code which also registers the i2c client
1706 	 * for the fusb302.
1707 	 */
1708 	if (device_property_read_string(dev, "linux,extcon-name", &name) == 0) {
1709 		chip->extcon = extcon_get_extcon_dev(name);
1710 		if (!chip->extcon)
1711 			return -EPROBE_DEFER;
1712 	}
1713 
1714 	chip->vbus = devm_regulator_get(chip->dev, "vbus");
1715 	if (IS_ERR(chip->vbus))
1716 		return PTR_ERR(chip->vbus);
1717 
1718 	chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1719 	if (!chip->wq)
1720 		return -ENOMEM;
1721 
1722 	spin_lock_init(&chip->irq_lock);
1723 	INIT_WORK(&chip->irq_work, fusb302_irq_work);
1724 	INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1725 	init_tcpc_dev(&chip->tcpc_dev);
1726 	fusb302_debugfs_init(chip);
1727 
1728 	if (client->irq) {
1729 		chip->gpio_int_n_irq = client->irq;
1730 	} else {
1731 		ret = init_gpio(chip);
1732 		if (ret < 0)
1733 			goto destroy_workqueue;
1734 	}
1735 
1736 	chip->tcpc_dev.fwnode = fusb302_fwnode_get(dev);
1737 	if (IS_ERR(chip->tcpc_dev.fwnode)) {
1738 		ret = PTR_ERR(chip->tcpc_dev.fwnode);
1739 		goto destroy_workqueue;
1740 	}
1741 
1742 	chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1743 	if (IS_ERR(chip->tcpm_port)) {
1744 		fwnode_handle_put(chip->tcpc_dev.fwnode);
1745 		ret = PTR_ERR(chip->tcpm_port);
1746 		if (ret != -EPROBE_DEFER)
1747 			dev_err(dev, "cannot register tcpm port, ret=%d", ret);
1748 		goto destroy_workqueue;
1749 	}
1750 
1751 	ret = request_irq(chip->gpio_int_n_irq, fusb302_irq_intn,
1752 			  IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1753 			  "fsc_interrupt_int_n", chip);
1754 	if (ret < 0) {
1755 		dev_err(dev, "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1756 		goto tcpm_unregister_port;
1757 	}
1758 	enable_irq_wake(chip->gpio_int_n_irq);
1759 	i2c_set_clientdata(client, chip);
1760 
1761 	return ret;
1762 
1763 tcpm_unregister_port:
1764 	tcpm_unregister_port(chip->tcpm_port);
1765 	fwnode_handle_put(chip->tcpc_dev.fwnode);
1766 destroy_workqueue:
1767 	fusb302_debugfs_exit(chip);
1768 	destroy_workqueue(chip->wq);
1769 
1770 	return ret;
1771 }
1772 
fusb302_remove(struct i2c_client * client)1773 static int fusb302_remove(struct i2c_client *client)
1774 {
1775 	struct fusb302_chip *chip = i2c_get_clientdata(client);
1776 
1777 	disable_irq_wake(chip->gpio_int_n_irq);
1778 	free_irq(chip->gpio_int_n_irq, chip);
1779 	cancel_work_sync(&chip->irq_work);
1780 	cancel_delayed_work_sync(&chip->bc_lvl_handler);
1781 	tcpm_unregister_port(chip->tcpm_port);
1782 	fwnode_handle_put(chip->tcpc_dev.fwnode);
1783 	destroy_workqueue(chip->wq);
1784 	fusb302_debugfs_exit(chip);
1785 
1786 	return 0;
1787 }
1788 
fusb302_pm_suspend(struct device * dev)1789 static int fusb302_pm_suspend(struct device *dev)
1790 {
1791 	struct fusb302_chip *chip = dev->driver_data;
1792 	unsigned long flags;
1793 
1794 	spin_lock_irqsave(&chip->irq_lock, flags);
1795 	chip->irq_suspended = true;
1796 	spin_unlock_irqrestore(&chip->irq_lock, flags);
1797 
1798 	/* Make sure any pending irq_work is finished before the bus suspends */
1799 	flush_work(&chip->irq_work);
1800 	return 0;
1801 }
1802 
fusb302_pm_resume(struct device * dev)1803 static int fusb302_pm_resume(struct device *dev)
1804 {
1805 	struct fusb302_chip *chip = dev->driver_data;
1806 	unsigned long flags;
1807 
1808 	spin_lock_irqsave(&chip->irq_lock, flags);
1809 	if (chip->irq_while_suspended) {
1810 		schedule_work(&chip->irq_work);
1811 		chip->irq_while_suspended = false;
1812 	}
1813 	chip->irq_suspended = false;
1814 	spin_unlock_irqrestore(&chip->irq_lock, flags);
1815 
1816 	return 0;
1817 }
1818 
1819 static const struct of_device_id fusb302_dt_match[] = {
1820 	{.compatible = "fcs,fusb302"},
1821 	{},
1822 };
1823 MODULE_DEVICE_TABLE(of, fusb302_dt_match);
1824 
1825 static const struct i2c_device_id fusb302_i2c_device_id[] = {
1826 	{"typec_fusb302", 0},
1827 	{},
1828 };
1829 MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
1830 
1831 static const struct dev_pm_ops fusb302_pm_ops = {
1832 	.suspend = fusb302_pm_suspend,
1833 	.resume = fusb302_pm_resume,
1834 };
1835 
1836 static struct i2c_driver fusb302_driver = {
1837 	.driver = {
1838 		   .name = "typec_fusb302",
1839 		   .pm = &fusb302_pm_ops,
1840 		   .of_match_table = of_match_ptr(fusb302_dt_match),
1841 		   },
1842 	.probe = fusb302_probe,
1843 	.remove = fusb302_remove,
1844 	.id_table = fusb302_i2c_device_id,
1845 };
1846 module_i2c_driver(fusb302_driver);
1847 
1848 MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1849 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1850 MODULE_LICENSE("GPL");
1851