xref: /OK3568_Linux_fs/u-boot/drivers/power/power_delivery/fusb302.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 <dm.h>
9 #include <i2c.h>
10 #include <asm/gpio.h>
11 #include <power/power_delivery/tcpm.h>
12 #include <power/power_delivery/power_delivery.h>
13 #include "fusb302_reg.h"
14 
15 /*
16  * When the device is SNK, BC_LVL interrupt is used to monitor cc pins
17  * for the current capability offered by the SRC. As FUSB302 chip fires
18  * the BC_LVL interrupt on PD signalings, cc lvl should be handled after
19  * a delay to avoid measuring on PD activities. The delay is slightly
20  * longer than PD_T_PD_DEBPUNCE (10-20ms).
21  */
22 #define T_BC_LVL_DEBOUNCE_DELAY_MS 30
23 #define msleep(a)	udelay(a * 1000)
24 #define usleep_range(a, b) udelay((b))
25 
26 enum toggling_mode {
27 	TOGGLING_MODE_OFF,
28 	TOGGLING_MODE_DRP,
29 	TOGGLING_MODE_SNK,
30 	TOGGLING_MODE_SRC,
31 };
32 
33 enum src_current_status {
34 	SRC_CURRENT_DEFAULT,
35 	SRC_CURRENT_MEDIUM,
36 	SRC_CURRENT_HIGH,
37 };
38 
39 static const u8 ra_mda_value[] = {
40 	[SRC_CURRENT_DEFAULT] = 4,	/* 210mV */
41 	[SRC_CURRENT_MEDIUM] = 9,	/* 420mV */
42 	[SRC_CURRENT_HIGH] = 18,	/* 798mV */
43 };
44 
45 static const u8 rd_mda_value[] = {
46 	[SRC_CURRENT_DEFAULT] = 38,	/* 1638mV */
47 	[SRC_CURRENT_MEDIUM] = 38,	/* 1638mV */
48 	[SRC_CURRENT_HIGH] = 61,	/* 2604mV */
49 };
50 
51 #define LOG_BUFFER_ENTRIES	1024
52 #define LOG_BUFFER_ENTRY_SIZE	128
53 
54 struct fusb302_chip {
55 	struct udevice *udev;
56 	struct udevice *vbus_regulator;
57 	struct ofnode *child_node;
58 	struct tcpm_port *tcpm_port;
59 	struct tcpc_dev tcpc_dev;
60 	struct gpio_desc gpio_cc_int;
61 
62 	int irq;
63 
64 	enum toggling_mode toggling_mode;
65 	enum src_current_status src_current_status;
66 	bool intr_togdone;
67 	bool intr_bc_lvl;
68 	bool intr_comp_chng;
69 
70 	/* port status */
71 	bool vconn_on;
72 	bool vbus_on;
73 	bool charge_on;
74 	bool vbus_present;
75 	bool gpio_cc_int_present;
76 	enum typec_cc_polarity cc_polarity;
77 	enum typec_cc_status cc1;
78 	enum typec_cc_status cc2;
79 };
80 
fusb302_i2c_write(struct fusb302_chip * chip,u8 address,u8 data)81 static int fusb302_i2c_write(struct fusb302_chip *chip,
82 			     u8 address, u8 data)
83 {
84 	int ret = 0;
85 
86 	ret = dm_i2c_write(chip->udev, address, &data, 1);
87 	if (ret)
88 		printf("%s: cannot write 0x%02x to 0x%02x, ret=%d\n",
89 			__func__, data, address, ret);
90 
91 	return ret;
92 }
93 
fusb302_i2c_block_write(struct fusb302_chip * chip,u8 address,u8 length,const u8 * data)94 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
95 				   u8 length, const u8 *data)
96 {
97 	int ret = 0;
98 
99 	if (length <= 0)
100 		return ret;
101 
102 	ret = dm_i2c_write(chip->udev, address, data, length);
103 	if (ret)
104 		printf("%s: cannot block write 0x%02x, len=%d, ret=%d\n",
105 			__func__, address, length, ret);
106 
107 	return ret;
108 }
109 
fusb302_i2c_read(struct fusb302_chip * chip,u8 address,u8 * data)110 static int fusb302_i2c_read(struct fusb302_chip *chip,
111 			    u8 address, u8 *data)
112 {
113 	int ret = 0;
114 
115 	ret = dm_i2c_read(chip->udev, address, data, 1);
116 	if (ret)
117 		printf("%s: cannot read %02x, ret=%d\n",
118 			__func__, address, ret);
119 
120 	return ret;
121 }
122 
fusb302_i2c_block_read(struct fusb302_chip * chip,u8 address,u8 length,u8 * data)123 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
124 				  u8 length, u8 *data)
125 {
126 	int ret = 0;
127 
128 	if (length <= 0)
129 		return ret;
130 
131 	ret = dm_i2c_read(chip->udev, address, data, length);
132 	if (ret)
133 		printf("%s: cannot block read 0x%02x, len=%d, ret=%d\n",
134 			__func__, address, length, ret);
135 	return ret;
136 }
137 
fusb302_i2c_mask_write(struct fusb302_chip * chip,u8 address,u8 mask,u8 value)138 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
139 				  u8 mask, u8 value)
140 {
141 	int ret = 0;
142 	u8 data;
143 
144 	ret = fusb302_i2c_read(chip, address, &data);
145 	if (ret)
146 		return ret;
147 	data &= ~mask;
148 	data |= value;
149 	ret = fusb302_i2c_write(chip, address, data);
150 	if (ret)
151 		return ret;
152 
153 	return ret;
154 }
155 
fusb302_i2c_set_bits(struct fusb302_chip * chip,u8 address,u8 set_bits)156 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
157 				u8 set_bits)
158 {
159 	return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
160 }
161 
fusb302_i2c_clear_bits(struct fusb302_chip * chip,u8 address,u8 clear_bits)162 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
163 				  u8 clear_bits)
164 {
165 	return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
166 }
167 
fusb302_sw_reset(struct fusb302_chip * chip)168 static int fusb302_sw_reset(struct fusb302_chip *chip)
169 {
170 	int ret = 0;
171 
172 	ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
173 				FUSB_REG_RESET_SW_RESET);
174 	if (ret)
175 		printf("cannot sw reset the fusb302(%d)\n", ret);
176 	else
177 		debug("fusb302 sw reset finished\n");
178 
179 	return ret;
180 }
181 
fusb302_enable_tx_auto_retries(struct fusb302_chip * chip,u8 retry_count)182 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip, u8 retry_count)
183 {
184 	int ret = 0;
185 
186 	ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3, retry_count |
187 				   FUSB_REG_CONTROL3_AUTO_RETRY);
188 
189 	return ret;
190 }
191 
192 /*
193  * mask all interrupt on the chip
194  */
fusb302_mask_interrupt(struct fusb302_chip * chip)195 static int fusb302_mask_interrupt(struct fusb302_chip *chip)
196 {
197 	int ret = 0;
198 
199 	ret = fusb302_i2c_write(chip, FUSB_REG_MASK, 0xFF);
200 	if (ret)
201 		return ret;
202 	ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
203 	if (ret)
204 		return ret;
205 	ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
206 	if (ret)
207 		return ret;
208 	ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
209 				   FUSB_REG_CONTROL0_INT_MASK);
210 		return ret;
211 }
212 
213 /*
214  * initialize interrupt on the chip
215  * - unmasked interrupt: VBUS_OK
216  */
fusb302_init_interrupt(struct fusb302_chip * chip)217 static int fusb302_init_interrupt(struct fusb302_chip *chip)
218 {
219 	int ret = 0;
220 
221 	ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
222 				0xFF & ~FUSB_REG_MASK_VBUSOK);
223 	if (ret)
224 		return ret;
225 	ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
226 	if (ret)
227 		return ret;
228 	ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
229 	if (ret)
230 		return ret;
231 	ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
232 				     FUSB_REG_CONTROL0_INT_MASK);
233 	return ret;
234 }
235 
fusb302_set_power_mode(struct fusb302_chip * chip,u8 power_mode)236 static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
237 {
238 	int ret = 0;
239 
240 	ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
241 
242 	return ret;
243 }
244 
tcpm_init(struct tcpc_dev * dev)245 static int tcpm_init(struct tcpc_dev *dev)
246 {
247 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
248 						 tcpc_dev);
249 	int ret = 0;
250 	u8 data;
251 
252 	ret = fusb302_sw_reset(chip);
253 	if (ret)
254 		return ret;
255 	ret = fusb302_enable_tx_auto_retries(chip, FUSB_REG_CONTROL3_N_RETRIES_3);
256 	if (ret)
257 		return ret;
258 	ret = fusb302_init_interrupt(chip);
259 	if (ret)
260 		return ret;
261 	ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
262 	if (ret)
263 		return ret;
264 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
265 	if (ret)
266 		return ret;
267 	chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
268 	ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
269 	if (ret)
270 		return ret;
271 	printf("fusb302 device ID: 0x%02x\n", data);
272 
273 	return ret;
274 }
275 
tcpm_get_vbus(struct tcpc_dev * dev)276 static int tcpm_get_vbus(struct tcpc_dev *dev)
277 {
278 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
279 						 tcpc_dev);
280 	return chip->vbus_present ? 1 : 0;
281 }
282 
283 #if 0
284 static int tcpm_get_current_limit(struct tcpc_dev *dev)
285 {
286 
287 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
288 						 tcpc_dev);
289 	int current_limit = 0;
290 	unsigned long timeout;
291 
292 	if (!chip->extcon)
293 		return 0;
294 
295 	/*
296 	 * USB2 Charger detection may still be in progress when we get here,
297 	 * this can take upto 600ms, wait 800ms max.
298 	 */
299 	timeout = jiffies + msecs_to_jiffies(800);
300 	do {
301 		if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_SDP) == 1)
302 			current_limit = 500;
303 
304 		if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_CDP) == 1 ||
305 		    extcon_get_state(chip->extcon, EXTCON_CHG_USB_ACA) == 1)
306 			current_limit = 1500;
307 
308 		if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_DCP) == 1)
309 			current_limit = 2000;
310 
311 		msleep(50);
312 	} while (current_limit == 0 && time_before(jiffies, timeout));
313 
314 	return current_limit;
315 }
316 #endif
317 
fusb302_set_src_current(struct fusb302_chip * chip,enum src_current_status status)318 static int fusb302_set_src_current(struct fusb302_chip *chip,
319 				   enum src_current_status status)
320 {
321 	int ret = 0;
322 
323 	chip->src_current_status = status;
324 	switch (status) {
325 	case SRC_CURRENT_DEFAULT:
326 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
327 					     FUSB_REG_CONTROL0_HOST_CUR_MASK,
328 					     FUSB_REG_CONTROL0_HOST_CUR_DEF);
329 		break;
330 	case SRC_CURRENT_MEDIUM:
331 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
332 					     FUSB_REG_CONTROL0_HOST_CUR_MASK,
333 					     FUSB_REG_CONTROL0_HOST_CUR_MED);
334 		break;
335 	case SRC_CURRENT_HIGH:
336 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
337 					     FUSB_REG_CONTROL0_HOST_CUR_MASK,
338 					     FUSB_REG_CONTROL0_HOST_CUR_HIGH);
339 		break;
340 	default:
341 		break;
342 	}
343 
344 	return ret;
345 }
346 
fusb302_set_toggling(struct fusb302_chip * chip,enum toggling_mode mode)347 static int fusb302_set_toggling(struct fusb302_chip *chip,
348 				enum toggling_mode mode)
349 {
350 	int ret = 0;
351 
352 	/* first disable toggling */
353 	ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
354 				     FUSB_REG_CONTROL2_TOGGLE);
355 	if (ret)
356 		return ret;
357 	/* mask interrupts for SRC or SNK */
358 	ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
359 				   FUSB_REG_MASK_BC_LVL |
360 				   FUSB_REG_MASK_COMP_CHNG);
361 	if (ret)
362 		return ret;
363 	chip->intr_bc_lvl = false;
364 	chip->intr_comp_chng = false;
365 	/* configure toggling mode: none/snk/src/drp */
366 	switch (mode) {
367 	case TOGGLING_MODE_OFF:
368 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
369 					     FUSB_REG_CONTROL2_MODE_MASK,
370 					     FUSB_REG_CONTROL2_MODE_NONE);
371 		break;
372 	case TOGGLING_MODE_SNK:
373 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
374 					     FUSB_REG_CONTROL2_MODE_MASK,
375 					     FUSB_REG_CONTROL2_MODE_UFP);
376 		break;
377 	case TOGGLING_MODE_SRC:
378 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
379 					     FUSB_REG_CONTROL2_MODE_MASK,
380 					     FUSB_REG_CONTROL2_MODE_DFP);
381 		break;
382 	case TOGGLING_MODE_DRP:
383 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
384 					     FUSB_REG_CONTROL2_MODE_MASK,
385 					     FUSB_REG_CONTROL2_MODE_DRP);
386 		break;
387 	default:
388 		break;
389 	}
390 
391 	if (ret)
392 		return ret;
393 
394 	if (mode == TOGGLING_MODE_OFF) {
395 		/* mask TOGDONE interrupt */
396 		ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
397 					   FUSB_REG_MASKA_TOGDONE);
398 		if (ret)
399 			return ret;
400 		chip->intr_togdone = false;
401 	} else {
402 		/* Datasheet says vconn MUST be off when toggling */
403 		if (chip->vconn_on)
404 			printf("%s: Vconn is on during toggle start\n", __func__);
405 		/* unmask TOGDONE interrupt */
406 		ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
407 					     FUSB_REG_MASKA_TOGDONE);
408 		if (ret)
409 			return ret;
410 		chip->intr_togdone = true;
411 		/* start toggling */
412 		ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
413 					   FUSB_REG_CONTROL2_TOGGLE);
414 		if (ret)
415 			return ret;
416 		/* during toggling, consider cc as Open */
417 		chip->cc1 = TYPEC_CC_OPEN;
418 		chip->cc2 = TYPEC_CC_OPEN;
419 	}
420 	chip->toggling_mode = mode;
421 
422 	return ret;
423 }
424 
425 static const char * const typec_cc_status_name[] = {
426 	[TYPEC_CC_OPEN]		= "Open",
427 	[TYPEC_CC_RA]		= "Ra",
428 	[TYPEC_CC_RD]		= "Rd",
429 	[TYPEC_CC_RP_DEF]	= "Rp-def",
430 	[TYPEC_CC_RP_1_5]	= "Rp-1.5",
431 	[TYPEC_CC_RP_3_0]	= "Rp-3.0",
432 };
433 
434 static const enum src_current_status cc_src_current[] = {
435 	[TYPEC_CC_OPEN]		= SRC_CURRENT_DEFAULT,
436 	[TYPEC_CC_RA]		= SRC_CURRENT_DEFAULT,
437 	[TYPEC_CC_RD]		= SRC_CURRENT_DEFAULT,
438 	[TYPEC_CC_RP_DEF]	= SRC_CURRENT_DEFAULT,
439 	[TYPEC_CC_RP_1_5]	= SRC_CURRENT_MEDIUM,
440 	[TYPEC_CC_RP_3_0]	= SRC_CURRENT_HIGH,
441 };
442 
tcpm_set_cc(struct tcpc_dev * dev,enum typec_cc_status cc)443 static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
444 {
445 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
446 						 tcpc_dev);
447 	u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
448 			    FUSB_REG_SWITCHES0_CC2_PU_EN |
449 			    FUSB_REG_SWITCHES0_CC1_PD_EN |
450 			    FUSB_REG_SWITCHES0_CC2_PD_EN;
451 	u8 rd_mda, switches0_data = 0x00;
452 	int ret = 0;
453 
454 	switch (cc) {
455 	case TYPEC_CC_OPEN:
456 		break;
457 	case TYPEC_CC_RD:
458 		switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
459 				  FUSB_REG_SWITCHES0_CC2_PD_EN;
460 		break;
461 	case TYPEC_CC_RP_DEF:
462 	case TYPEC_CC_RP_1_5:
463 	case TYPEC_CC_RP_3_0:
464 		switches0_data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
465 				  FUSB_REG_SWITCHES0_CC1_PU_EN :
466 				  FUSB_REG_SWITCHES0_CC2_PU_EN;
467 		break;
468 	default:
469 		printf("%s: unsupported cc value %s\n",
470 			__func__, typec_cc_status_name[cc]);
471 		ret = -EINVAL;
472 		goto done;
473 	}
474 
475 	ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
476 	if (ret) {
477 		printf("%s: cannot set toggling mode(%d)\n", __func__, ret);
478 		goto done;
479 	}
480 
481 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
482 				     switches0_mask, switches0_data);
483 	if (ret) {
484 		printf("%s: cannot set pull-up/-down(%d)\n", __func__, ret);
485 		goto done;
486 	}
487 	/* reset the cc status */
488 	chip->cc1 = TYPEC_CC_OPEN;
489 	chip->cc2 = TYPEC_CC_OPEN;
490 
491 	/* adjust current for SRC */
492 	ret = fusb302_set_src_current(chip, cc_src_current[cc]);
493 	if (ret) {
494 		printf("%s: cannot set src current %s(%d)\n",
495 			__func__, typec_cc_status_name[cc], ret);
496 		goto done;
497 	}
498 
499 	/* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
500 	switch (cc) {
501 	case TYPEC_CC_RP_DEF:
502 	case TYPEC_CC_RP_1_5:
503 	case TYPEC_CC_RP_3_0:
504 		rd_mda = rd_mda_value[cc_src_current[cc]];
505 		ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
506 		if (ret) {
507 			printf("%s: cannot set SRC measure value(%d)\n",
508 				__func__, ret);
509 			goto done;
510 		}
511 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
512 					     FUSB_REG_MASK_BC_LVL |
513 					     FUSB_REG_MASK_COMP_CHNG,
514 					     FUSB_REG_MASK_BC_LVL);
515 		if (ret) {
516 			printf("%s: cannot set SRC interrupt(%d)\n",
517 				__func__, ret);
518 			goto done;
519 		}
520 		chip->intr_comp_chng = true;
521 		break;
522 	case TYPEC_CC_RD:
523 		ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
524 					     FUSB_REG_MASK_BC_LVL |
525 					     FUSB_REG_MASK_COMP_CHNG,
526 					     FUSB_REG_MASK_COMP_CHNG);
527 		if (ret) {
528 			printf("%s: cannot set SRC interrupt(%d)\n",
529 				__func__, ret);
530 			goto done;
531 		}
532 		chip->intr_bc_lvl = true;
533 		break;
534 	default:
535 		break;
536 	}
537 done:
538 	return ret;
539 }
540 
tcpm_get_cc(struct tcpc_dev * dev,enum typec_cc_status * cc1,enum typec_cc_status * cc2)541 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
542 		       enum typec_cc_status *cc2)
543 {
544 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
545 						 tcpc_dev);
546 
547 	*cc1 = chip->cc1;
548 	*cc2 = chip->cc2;
549 	debug("get cc1 = %s, cc2 = %s\n", typec_cc_status_name[*cc1],
550 	       typec_cc_status_name[*cc2]);
551 
552 	return 0;
553 }
554 
tcpm_set_polarity(struct tcpc_dev * dev,enum typec_cc_polarity polarity)555 static int tcpm_set_polarity(struct tcpc_dev *dev,
556 			     enum typec_cc_polarity polarity)
557 {
558 	return 0;
559 }
560 
tcpm_set_vconn(struct tcpc_dev * dev,bool on)561 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
562 {
563 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
564 						 tcpc_dev);
565 	int ret = 0;
566 	u8 switches0_data = 0x00;
567 	u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
568 			    FUSB_REG_SWITCHES0_VCONN_CC2;
569 
570 	if (chip->vconn_on == on) {
571 		printf("vconn is already %s\n", on ? "On" : "Off");
572 		goto done;
573 	}
574 	if (on) {
575 		switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
576 				 FUSB_REG_SWITCHES0_VCONN_CC2 :
577 				 FUSB_REG_SWITCHES0_VCONN_CC1;
578 	}
579 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
580 				     switches0_mask, switches0_data);
581 	if (ret)
582 		goto done;
583 	debug("%s: vconn := %s\n", __func__, on ? "On" : "Off");
584 done:
585 	return ret;
586 }
587 
tcpm_set_vbus(struct tcpc_dev * dev,bool on,bool charge)588 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
589 {
590 #if 0
591 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
592 						 tcpc_dev);
593 	int ret = 0;
594 
595 	mutex_lock(&chip->lock);
596 	if (chip->vbus_on == on) {
597 		printf("%s: vbus is already %s\n", __func__, on ? "On" : "Off");
598 	} else {
599 		if (on)
600 			ret = regulator_enable(chip->vbus);
601 		else
602 			ret = regulator_disable(chip->vbus);
603 		if (ret < 0) {
604 			printf("%s: cannot %s vbus regulator(%d)\n",
605 				__func__, on ? "enable" : "disable", ret);
606 			goto done;
607 		}
608 		chip->vbus_on = on;
609 		debug("%s: vbus := %s\n", __func__, on ? "On" : "Off");
610 	}
611 	if (chip->charge_on == charge)
612 		debug("%s: charge is already %s\n",
613 			__func__, charge ? "On" : "Off");
614 	else
615 		chip->charge_on = charge;
616 
617 done:
618 	mutex_unlock(&chip->lock);
619 #endif
620 
621 	return 0;
622 }
623 
fusb302_pd_tx_flush(struct fusb302_chip * chip)624 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
625 {
626 	return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
627 				    FUSB_REG_CONTROL0_TX_FLUSH);
628 }
629 
fusb302_pd_rx_flush(struct fusb302_chip * chip)630 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
631 {
632 	return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
633 				    FUSB_REG_CONTROL1_RX_FLUSH);
634 }
635 
fusb302_pd_set_auto_goodcrc(struct fusb302_chip * chip,bool on)636 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
637 {
638 	if (on)
639 		return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
640 					    FUSB_REG_SWITCHES1_AUTO_GCRC);
641 	return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
642 					    FUSB_REG_SWITCHES1_AUTO_GCRC);
643 }
644 
fusb302_pd_set_interrupts(struct fusb302_chip * chip,bool on)645 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
646 {
647 	int ret = 0;
648 	u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
649 	u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
650 			      FUSB_REG_MASKA_HARDSENT |
651 			      FUSB_REG_MASKA_TX_SUCCESS |
652 			      FUSB_REG_MASKA_HARDRESET;
653 	u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
654 
655 	ret = on ?
656 		fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
657 		fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
658 	if (ret)
659 		return ret;
660 	ret = on ?
661 		fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
662 		fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
663 	if (ret)
664 		return ret;
665 	ret = on ?
666 		fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
667 		fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
668 	return ret;
669 }
670 
tcpm_set_pd_rx(struct tcpc_dev * dev,bool on)671 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
672 {
673 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
674 						 tcpc_dev);
675 	int ret = 0;
676 
677 	ret = fusb302_pd_rx_flush(chip);
678 	if (ret) {
679 		printf("%s: cannot flush pd rx buffer(%d)\n", __func__, ret);
680 		goto done;
681 	}
682 	ret = fusb302_pd_tx_flush(chip);
683 	if (ret) {
684 		printf("%s: cannot flush pd tx buffer(%d)\n", __func__, ret);
685 		goto done;
686 	}
687 	ret = fusb302_pd_set_auto_goodcrc(chip, on);
688 	if (ret) {
689 		printf("%s: cannot turn %s auto GCRC(%d)\n",
690 			__func__, on ? "on" : "off", ret);
691 		goto done;
692 	}
693 	ret = fusb302_pd_set_interrupts(chip, on);
694 	if (ret) {
695 		printf("%s: cannot turn %s pd interrupts(%d)\n",
696 			__func__, on ? "on" : "off", ret);
697 		goto done;
698 	}
699 	debug("%s: pd := %s\n", __func__, on ? "on" : "off");
700 done:
701 	return ret;
702 }
703 
704 static const char * const typec_role_name[] = {
705 	[TYPEC_SINK]		= "Sink",
706 	[TYPEC_SOURCE]		= "Source",
707 };
708 
709 static const char * const typec_data_role_name[] = {
710 	[TYPEC_DEVICE]		= "Device",
711 	[TYPEC_HOST]		= "Host",
712 };
713 
tcpm_set_roles(struct tcpc_dev * dev,bool attached,enum typec_role pwr,enum typec_data_role data)714 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
715 			  enum typec_role pwr, enum typec_data_role data)
716 {
717 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
718 						 tcpc_dev);
719 	int ret = 0;
720 	u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
721 			    FUSB_REG_SWITCHES1_DATAROLE;
722 	u8 switches1_data = 0x00;
723 
724 	if (pwr == TYPEC_SOURCE)
725 		switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
726 	if (data == TYPEC_HOST)
727 		switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
728 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
729 				     switches1_mask, switches1_data);
730 	if (ret) {
731 		printf("unable to set pd header %s, %s, ret= %d\n",
732 			typec_role_name[pwr], typec_data_role_name[data], ret);
733 		goto done;
734 	}
735 	debug("%s: pd header : %s, %s\n", __func__, typec_role_name[pwr],
736 		typec_data_role_name[data]);
737 done:
738 
739 	return ret;
740 }
741 
tcpm_start_toggling(struct tcpc_dev * dev,enum typec_port_type port_type,enum typec_cc_status cc)742 static int tcpm_start_toggling(struct tcpc_dev *dev,
743 			       enum typec_port_type port_type,
744 			       enum typec_cc_status cc)
745 {
746 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
747 						 tcpc_dev);
748 	enum toggling_mode mode = TOGGLING_MODE_OFF;
749 	int ret = 0;
750 
751 	switch (port_type) {
752 	case TYPEC_PORT_SRC:
753 		mode = TOGGLING_MODE_SRC;
754 		break;
755 	case TYPEC_PORT_SNK:
756 		mode = TOGGLING_MODE_SNK;
757 		break;
758 	case TYPEC_PORT_DRP:
759 		mode = TOGGLING_MODE_DRP;
760 		break;
761 	}
762 
763 	ret = fusb302_set_src_current(chip, cc_src_current[cc]);
764 	if (ret) {
765 		printf("%s: unable to set src current %s, ret=%d",
766 			__func__, typec_cc_status_name[cc], ret);
767 		goto done;
768 	}
769 	ret = fusb302_set_toggling(chip, mode);
770 	if (ret) {
771 		printf("%s: unable to start drp toggling(%d)\n", __func__, ret);
772 		goto done;
773 	}
774 	printf("fusb302 start drp toggling\n");
775 done:
776 
777 	return ret;
778 }
779 
fusb302_pd_send_message(struct fusb302_chip * chip,const struct pd_message * msg)780 static int fusb302_pd_send_message(struct fusb302_chip *chip,
781 				   const struct pd_message *msg)
782 {
783 	int ret = 0;
784 	u8 buf[40];
785 	u8 pos = 0;
786 	int len;
787 
788 	/* SOP tokens */
789 	buf[pos++] = FUSB302_TKN_SYNC1;
790 	buf[pos++] = FUSB302_TKN_SYNC1;
791 	buf[pos++] = FUSB302_TKN_SYNC1;
792 	buf[pos++] = FUSB302_TKN_SYNC2;
793 
794 	len = pd_header_cnt_le(msg->header) * 4;
795 	/* plug 2 for header */
796 	len += 2;
797 	if (len > 0x1F) {
798 		printf("PD message too long %d (incl. header)", len);
799 		return -EINVAL;
800 	}
801 	/* packsym tells the FUSB302 chip that the next X bytes are payload */
802 	buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
803 	memcpy(&buf[pos], &msg->header, sizeof(msg->header));
804 	pos += sizeof(msg->header);
805 
806 	len -= 2;
807 	memcpy(&buf[pos], msg->payload, len);
808 	pos += len;
809 
810 	/* CRC */
811 	buf[pos++] = FUSB302_TKN_JAMCRC;
812 	/* EOP */
813 	buf[pos++] = FUSB302_TKN_EOP;
814 	/* turn tx off after sending message */
815 	buf[pos++] = FUSB302_TKN_TXOFF;
816 	/* start transmission */
817 	buf[pos++] = FUSB302_TKN_TXON;
818 
819 	ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
820 	if (ret)
821 		return ret;
822 	debug("sending PD message header: %x\n", msg->header);
823 	debug("sending PD message len: %d\n", len);
824 
825 	return ret;
826 }
827 
fusb302_pd_send_hardreset(struct fusb302_chip * chip)828 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
829 {
830 	return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
831 				    FUSB_REG_CONTROL3_SEND_HARDRESET);
832 }
833 
834 static const char * const transmit_type_name[] = {
835 	[TCPC_TX_SOP]			= "SOP",
836 	[TCPC_TX_SOP_PRIME]		= "SOP'",
837 	[TCPC_TX_SOP_PRIME_PRIME]	= "SOP''",
838 	[TCPC_TX_SOP_DEBUG_PRIME]	= "DEBUG'",
839 	[TCPC_TX_SOP_DEBUG_PRIME_PRIME]	= "DEBUG''",
840 	[TCPC_TX_HARD_RESET]		= "HARD_RESET",
841 	[TCPC_TX_CABLE_RESET]		= "CABLE_RESET",
842 	[TCPC_TX_BIST_MODE_2]		= "BIST_MODE_2",
843 };
844 
tcpm_pd_transmit(struct tcpc_dev * dev,enum tcpm_transmit_type type,const struct pd_message * msg,unsigned int negotiated_rev)845 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
846 			    const struct pd_message *msg, unsigned int negotiated_rev)
847 {
848 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
849 						 tcpc_dev);
850 	int ret = 0;
851 
852 	switch (type) {
853 	case TCPC_TX_SOP:
854 		/* nRetryCount 3 in P2.0 spec, whereas 2 in PD3.0 spec */
855 		ret = fusb302_enable_tx_auto_retries(chip, negotiated_rev > PD_REV20 ?
856 						     FUSB_REG_CONTROL3_N_RETRIES_2 :
857 						     FUSB_REG_CONTROL3_N_RETRIES_3);
858 		if (ret)
859 			printf("%s: Cannot update retry count(%d)\n",
860 				__func__, ret);
861 
862 		ret = fusb302_pd_send_message(chip, msg);
863 		if (ret)
864 			printf("%s: cannot send PD message(%d)\n",
865 				__func__, ret);
866 		break;
867 	case TCPC_TX_HARD_RESET:
868 		ret = fusb302_pd_send_hardreset(chip);
869 		if (ret)
870 			printf("%s: cannot send hardreset(%d)\n",
871 				__func__, ret);
872 		break;
873 	default:
874 		printf("%s: type %s not supported",
875 			__func__, transmit_type_name[type]);
876 		ret = -EINVAL;
877 	}
878 
879 	return ret;
880 }
881 
fusb302_bc_lvl_to_cc(u8 bc_lvl)882 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
883 {
884 	if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
885 		return TYPEC_CC_RP_3_0;
886 	if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
887 		return TYPEC_CC_RP_1_5;
888 	if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
889 		return TYPEC_CC_RP_DEF;
890 	return TYPEC_CC_OPEN;
891 }
892 
fusb302_bc_lvl_handler(struct fusb302_chip * chip)893 static void fusb302_bc_lvl_handler(struct fusb302_chip *chip)
894 {
895 	int ret = 0;
896 	u8 status0;
897 	u8 bc_lvl;
898 	enum typec_cc_status cc_status;
899 
900 	if (!chip->intr_bc_lvl) {
901 		printf("BC_LVL interrupt is turned off, abort\n");
902 		goto done;
903 	}
904 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
905 	if (ret)
906 		goto done;
907 
908 	debug("BC_LVL handler, status0 = 0x%02x\n", status0);
909 	if (status0 & FUSB_REG_STATUS0_ACTIVITY)
910 		printf("CC activities detected, delay handling\n");
911 	bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
912 	cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
913 	if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
914 		if (chip->cc1 != cc_status) {
915 			debug("cc1: %s -> %s\n",
916 				typec_cc_status_name[chip->cc1],
917 				typec_cc_status_name[cc_status]);
918 			chip->cc1 = cc_status;
919 			tcpm_cc_change(chip->tcpm_port);
920 		}
921 	} else {
922 		if (chip->cc2 != cc_status) {
923 			debug("cc2: %s -> %s\n",
924 				typec_cc_status_name[chip->cc2],
925 				typec_cc_status_name[cc_status]);
926 			chip->cc2 = cc_status;
927 			tcpm_cc_change(chip->tcpm_port);
928 		}
929 	}
930 
931 done:
932 	return;
933 }
934 
935 static void fusb302_interrupt_handle(struct fusb302_chip *chip);
fusb302_poll_event(struct tcpc_dev * dev)936 static void fusb302_poll_event(struct tcpc_dev *dev)
937 {
938 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
939 						 tcpc_dev);
940 
941 	fusb302_interrupt_handle(chip);
942 }
943 
fusb302_enter_low_power_mode(struct tcpc_dev * dev,bool attached,bool pd_capable)944 static int fusb302_enter_low_power_mode(struct tcpc_dev *dev,
945 					bool attached, bool pd_capable)
946 {
947 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
948 						 tcpc_dev);
949 	int ret = 0;
950 	unsigned int reg;
951 
952 	ret = fusb302_mask_interrupt(chip);
953 	if (ret)
954 		return ret;
955 	if (attached && pd_capable)
956 		reg = FUSB_REG_POWER_PWR_MEDIUM;
957 	else if (attached)
958 		reg = FUSB_REG_POWER_PWR_LOW;
959 	else
960 		reg = 0;
961 
962 	return fusb302_set_power_mode(chip, reg);
963 }
964 
init_tcpc_dev(struct tcpc_dev * fusb302_tcpc_dev)965 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
966 {
967 	fusb302_tcpc_dev->init = tcpm_init;
968 	fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
969 	//fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
970 	fusb302_tcpc_dev->set_cc = tcpm_set_cc;
971 	fusb302_tcpc_dev->get_cc = tcpm_get_cc;
972 	fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
973 	fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
974 	fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
975 	fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
976 	fusb302_tcpc_dev->set_roles = tcpm_set_roles;
977 	fusb302_tcpc_dev->start_toggling = tcpm_start_toggling;
978 	fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
979 	fusb302_tcpc_dev->poll_event = fusb302_poll_event;
980 	fusb302_tcpc_dev->enter_low_power_mode = fusb302_enter_low_power_mode;
981 }
982 
983 static const char * const cc_polarity_name[] = {
984 	[TYPEC_POLARITY_CC1]	= "Polarity_CC1",
985 	[TYPEC_POLARITY_CC2]	= "Polarity_CC2",
986 };
987 
fusb302_set_cc_polarity_and_pull(struct fusb302_chip * chip,enum typec_cc_polarity cc_polarity,bool pull_up,bool pull_down)988 static int fusb302_set_cc_polarity_and_pull(struct fusb302_chip *chip,
989 					    enum typec_cc_polarity cc_polarity,
990 					    bool pull_up, bool pull_down)
991 {
992 	int ret = 0;
993 	u8 switches0_data = 0x00;
994 	u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
995 			    FUSB_REG_SWITCHES1_TXCC2_EN;
996 	u8 switches1_data = 0x00;
997 
998 	if (pull_down)
999 		switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
1000 				  FUSB_REG_SWITCHES0_CC2_PD_EN;
1001 
1002 	if (cc_polarity == TYPEC_POLARITY_CC1) {
1003 		switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC1;
1004 		if (chip->vconn_on)
1005 			switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1006 		if (pull_up)
1007 			switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1008 		switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1009 	} else {
1010 		switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC2;
1011 		if (chip->vconn_on)
1012 			switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1013 		if (pull_up)
1014 			switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1015 		switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1016 	}
1017 	ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1018 	if (ret)
1019 		return ret;
1020 	ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1021 				     switches1_mask, switches1_data);
1022 	if (ret)
1023 		return ret;
1024 	chip->cc_polarity = cc_polarity;
1025 
1026 	return ret;
1027 }
1028 
fusb302_handle_togdone_snk(struct fusb302_chip * chip,u8 togdone_result)1029 static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1030 				      u8 togdone_result)
1031 {
1032 	int ret = 0;
1033 	u8 status0;
1034 	u8 bc_lvl;
1035 	enum typec_cc_polarity cc_polarity;
1036 	enum typec_cc_status cc_status_active, cc1, cc2;
1037 
1038 	/* set polarity and pull_up, pull_down */
1039 	cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1040 		      TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1041 	ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, false, true);
1042 	if (ret) {
1043 		printf("cannot set cc polarity %s, ret = %d\n",
1044 			cc_polarity_name[cc_polarity], ret);
1045 		return ret;
1046 	}
1047 	/* fusb302_set_cc_polarity() has set the correct measure block */
1048 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1049 	if (ret < 0)
1050 		return ret;
1051 	bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1052 	cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1053 	/* restart toggling if the cc status on the active line is OPEN */
1054 	if (cc_status_active == TYPEC_CC_OPEN) {
1055 		printf("restart toggling as CC_OPEN detected\n");
1056 		ret = fusb302_set_toggling(chip, chip->toggling_mode);
1057 		return ret;
1058 	}
1059 	/* update tcpm with the new cc value */
1060 	cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1061 	      cc_status_active : TYPEC_CC_OPEN;
1062 	cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1063 	      cc_status_active : TYPEC_CC_OPEN;
1064 	if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1065 		chip->cc1 = cc1;
1066 		chip->cc2 = cc2;
1067 		tcpm_cc_change(chip->tcpm_port);
1068 	}
1069 	/* turn off toggling */
1070 	ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1071 	if (ret) {
1072 		printf("cannot set toggling mode off, ret=%d\n", ret);
1073 		return ret;
1074 	}
1075 	/* unmask bc_lvl interrupt */
1076 	ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1077 	if (ret) {
1078 		printf("cannot unmask bc_lcl interrupt, ret=%d\n", ret);
1079 		return ret;
1080 	}
1081 	chip->intr_bc_lvl = true;
1082 	debug("detected cc1=%s, cc2=%s\n",
1083 		typec_cc_status_name[cc1],
1084 		typec_cc_status_name[cc2]);
1085 
1086 	return ret;
1087 }
1088 
1089 /* 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)1090 static int fusb302_get_src_cc_status(struct fusb302_chip *chip,
1091 				     enum typec_cc_polarity cc_polarity,
1092 				     enum typec_cc_status *cc)
1093 {
1094 	u8 ra_mda = ra_mda_value[chip->src_current_status];
1095 	u8 rd_mda = rd_mda_value[chip->src_current_status];
1096 	u8 switches0_data, status0;
1097 	int ret;
1098 
1099 	/* Step 1: Set switches so that we measure the right CC pin */
1100 	switches0_data = (cc_polarity == TYPEC_POLARITY_CC1) ?
1101 		FUSB_REG_SWITCHES0_CC1_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC1 :
1102 		FUSB_REG_SWITCHES0_CC2_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC2;
1103 	ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1104 	if (ret < 0)
1105 		return ret;
1106 
1107 	fusb302_i2c_read(chip, FUSB_REG_SWITCHES0, &status0);
1108 	debug("get_src_cc_status switches: 0x%0x", status0);
1109 
1110 	/* Step 2: Set compararator volt to differentiate between Open and Rd */
1111 	ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1112 	if (ret)
1113 		return ret;
1114 
1115 	usleep_range(50, 100);
1116 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1117 	if (ret)
1118 		return ret;
1119 
1120 	debug("get_src_cc_status rd_mda status0: 0x%0x", status0);
1121 	if (status0 & FUSB_REG_STATUS0_COMP) {
1122 		*cc = TYPEC_CC_OPEN;
1123 		return 0;
1124 	}
1125 
1126 	/* Step 3: Set compararator input to differentiate between Rd and Ra. */
1127 	ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1128 	if (ret)
1129 		return ret;
1130 
1131 	usleep_range(50, 100);
1132 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1133 	if (ret)
1134 		return ret;
1135 
1136 	debug("get_src_cc_status ra_mda status0: 0x%0x", status0);
1137 	if (status0 & FUSB_REG_STATUS0_COMP)
1138 		*cc = TYPEC_CC_RD;
1139 	else
1140 		*cc = TYPEC_CC_RA;
1141 
1142 	return 0;
1143 }
1144 
fusb302_handle_togdone_src(struct fusb302_chip * chip,u8 togdone_result)1145 static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1146 				      u8 togdone_result)
1147 {
1148 	/*
1149 	 * - set polarity (measure cc, vconn, tx)
1150 	 * - set pull_up, pull_down
1151 	 * - set cc1, cc2, and update to tcpm_port
1152 	 * - set I_COMP interrupt on
1153 	 */
1154 	int ret = 0;
1155 	u8 rd_mda = rd_mda_value[chip->src_current_status];
1156 	enum toggling_mode toggling_mode = chip->toggling_mode;
1157 	enum typec_cc_polarity cc_polarity;
1158 	enum typec_cc_status cc1, cc2;
1159 
1160 	/*
1161 	 * The toggle-engine will stop in a src state if it sees either Ra or
1162 	 * Rd. Determine the status for both CC pins, starting with the one
1163 	 * where toggling stopped, as that is where the switches point now.
1164 	 */
1165 	if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1166 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1167 	else
1168 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1169 	if (ret)
1170 		return ret;
1171 	/* we must turn off toggling before we can measure the other pin */
1172 	ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1173 	if (ret) {
1174 		printf("cannot set toggling mode off, ret=%d\n", ret);
1175 		return ret;
1176 	}
1177 	/* get the status of the other pin */
1178 	if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1179 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1180 	else
1181 		ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1182 	if (ret)
1183 		return ret;
1184 
1185 	/* determine polarity based on the status of both pins */
1186 	if (cc1 == TYPEC_CC_RD &&
1187 			(cc2 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_RA)) {
1188 		cc_polarity = TYPEC_POLARITY_CC1;
1189 	} else if (cc2 == TYPEC_CC_RD &&
1190 		    (cc1 == TYPEC_CC_OPEN || cc1 == TYPEC_CC_RA)) {
1191 		cc_polarity = TYPEC_POLARITY_CC2;
1192 	} else {
1193 		printf("unexpected CC status cc1=%s, cc2=%s, restarting toggling\n",
1194 			typec_cc_status_name[cc1],
1195 			typec_cc_status_name[cc2]);
1196 		return fusb302_set_toggling(chip, toggling_mode);
1197 	}
1198 	/* set polarity and pull_up, pull_down */
1199 	ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, true, false);
1200 	if (ret < 0) {
1201 		printf("cannot set cc polarity %s, ret=%d\n",
1202 			cc_polarity_name[cc_polarity], ret);
1203 		return ret;
1204 	}
1205 	/* update tcpm with the new cc value */
1206 	if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1207 		chip->cc1 = cc1;
1208 		chip->cc2 = cc2;
1209 		tcpm_cc_change(chip->tcpm_port);
1210 	}
1211 	/* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1212 	ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1213 	if (ret)
1214 		return ret;
1215 	/* unmask comp_chng interrupt */
1216 	ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1217 				     FUSB_REG_MASK_COMP_CHNG);
1218 	if (ret) {
1219 		printf("cannot unmask comp_chng interrupt, ret=%d\n", ret);
1220 		return ret;
1221 	}
1222 	chip->intr_comp_chng = true;
1223 	debug("detected cc1=%s, cc2=%s\n",
1224 		typec_cc_status_name[cc1],
1225 		typec_cc_status_name[cc2]);
1226 
1227 	return ret;
1228 }
1229 
fusb302_handle_togdone(struct fusb302_chip * chip)1230 static int fusb302_handle_togdone(struct fusb302_chip *chip)
1231 {
1232 	int ret = 0;
1233 	u8 status1a;
1234 	u8 togdone_result;
1235 
1236 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1237 	if (ret < 0)
1238 		return ret;
1239 	togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1240 			 FUSB_REG_STATUS1A_TOGSS_MASK;
1241 	switch (togdone_result) {
1242 	case FUSB_REG_STATUS1A_TOGSS_SNK1:
1243 	case FUSB_REG_STATUS1A_TOGSS_SNK2:
1244 		return fusb302_handle_togdone_snk(chip, togdone_result);
1245 	case FUSB_REG_STATUS1A_TOGSS_SRC1:
1246 	case FUSB_REG_STATUS1A_TOGSS_SRC2:
1247 		return fusb302_handle_togdone_src(chip, togdone_result);
1248 	case FUSB_REG_STATUS1A_TOGSS_AA:
1249 		/* doesn't support */
1250 		printf("AudioAccessory not supported\n");
1251 		fusb302_set_toggling(chip, chip->toggling_mode);
1252 		break;
1253 	default:
1254 		printf("TOGDONE with an invalid state: %d\n", togdone_result);
1255 		fusb302_set_toggling(chip, chip->toggling_mode);
1256 		break;
1257 	}
1258 	return ret;
1259 }
1260 
fusb302_pd_reset(struct fusb302_chip * chip)1261 static int fusb302_pd_reset(struct fusb302_chip *chip)
1262 {
1263 	return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1264 				    FUSB_REG_RESET_PD_RESET);
1265 }
1266 
fusb302_pd_read_message(struct fusb302_chip * chip,struct pd_message * msg)1267 static int fusb302_pd_read_message(struct fusb302_chip *chip,
1268 				   struct pd_message *msg)
1269 {
1270 	int ret = 0;
1271 	u8 token;
1272 	u8 crc[4];
1273 	int len;
1274 
1275 	/* first SOP token */
1276 	ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1277 	if (ret)
1278 		return ret;
1279 	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1280 				     (u8 *)&msg->header);
1281 	if (ret)
1282 		return ret;
1283 	len = pd_header_cnt_le(msg->header) * 4;
1284 	/* add 4 to length to include the CRC */
1285 	if (len > PD_MAX_PAYLOAD * 4) {
1286 		printf("%s: PD message too long %d\n", __func__, len);
1287 		return -EINVAL;
1288 	}
1289 	if (len > 0) {
1290 		ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1291 					     (u8 *)msg->payload);
1292 		if (ret)
1293 			return ret;
1294 	}
1295 	/* another 4 bytes to read CRC out */
1296 	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1297 	if (ret)
1298 		return ret;
1299 	debug("%s: PD message header: %x\n", __func__, msg->header);
1300 	debug("%s: PD message len: %d\n", __func__, len);
1301 
1302 	/*
1303 	 * Check if we've read off a GoodCRC message. If so then indicate to
1304 	 * TCPM that the previous transmission has completed. Otherwise we pass
1305 	 * the received message over to TCPM for processing.
1306 	 *
1307 	 * We make this check here instead of basing the reporting decision on
1308 	 * the IRQ event type, as it's possible for the chip to report the
1309 	 * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
1310 	 * to check the message type to ensure correct reporting to TCPM.
1311 	 */
1312 	if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
1313 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1314 	else
1315 		tcpm_pd_receive(chip->tcpm_port, msg);
1316 
1317 	return ret;
1318 }
1319 
fusb302_interrupt_handle(struct fusb302_chip * chip)1320 static void fusb302_interrupt_handle(struct fusb302_chip *chip)
1321 {
1322 	int ret = 0;
1323 	u8 interrupt;
1324 	u8 interrupta;
1325 	u8 interruptb;
1326 	u8 status0;
1327 	bool vbus_present;
1328 	bool comp_result;
1329 	bool intr_togdone;
1330 	bool intr_bc_lvl;
1331 	bool intr_comp_chng;
1332 	struct pd_message pd_msg;
1333 
1334 	/* grab a snapshot of intr flags */
1335 	intr_togdone = chip->intr_togdone;
1336 	intr_bc_lvl = chip->intr_bc_lvl;
1337 	intr_comp_chng = chip->intr_comp_chng;
1338 
1339 	if (chip->gpio_cc_int_present)
1340 		if (!dm_gpio_get_value(&chip->gpio_cc_int))
1341 			return;
1342 
1343 	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1344 	if (ret)
1345 		return;
1346 	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1347 	if (ret)
1348 		return;
1349 	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1350 	if (ret)
1351 		return;
1352 	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1353 	if (ret)
1354 		return;
1355 	debug("IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x\n",
1356 		interrupt, interrupta, interruptb, status0);
1357 
1358 	if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1359 		vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1360 		debug("IRQ: VBUS_OK, vbus=%s\n", vbus_present ? "On" : "Off");
1361 		if (vbus_present != chip->vbus_present) {
1362 			chip->vbus_present = vbus_present;
1363 			tcpm_vbus_change(chip->tcpm_port);
1364 		}
1365 	}
1366 
1367 	if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1368 		debug("IRQ: TOGDONE\n");
1369 		ret = fusb302_handle_togdone(chip);
1370 		if (ret) {
1371 			printf("%s: handle togdone error(%d)\n", __func__, ret);
1372 			return;
1373 		}
1374 	}
1375 
1376 	if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1377 		debug("IRQ: BC_LVL, handler pending\n");
1378 		/*
1379 		 * as BC_LVL interrupt can be affected by PD activity,
1380 		 * apply delay to for the handler to wait for the PD
1381 		 * signaling to finish.
1382 		 */
1383 		//msleep(T_BC_LVL_DEBOUNCE_DELAY_MS);
1384 		fusb302_bc_lvl_handler(chip);
1385 	}
1386 
1387 	if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1388 		comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1389 		debug("IRQ: COMP_CHNG, comp=%s\n", comp_result ? "true" : "false");
1390 		if (comp_result) {
1391 			/* cc level > Rd_threshold, detach */
1392 			chip->cc1 = TYPEC_CC_OPEN;
1393 			chip->cc2 = TYPEC_CC_OPEN;
1394 			tcpm_cc_change(chip->tcpm_port);
1395 		}
1396 	}
1397 
1398 	if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1399 		debug("IRQ: PD collision\n");
1400 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1401 	}
1402 
1403 	if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1404 		debug("IRQ: PD retry failed\n");
1405 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1406 	}
1407 
1408 	if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1409 		debug("IRQ: PD hardreset sent\n");
1410 		ret = fusb302_pd_reset(chip);
1411 		if (ret) {
1412 			printf("cannot PD reset, ret=%d\n", ret);
1413 			return;
1414 		}
1415 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1416 	}
1417 
1418 	if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1419 		debug("IRQ: PD tx success\n");
1420 		ret = fusb302_pd_read_message(chip, &pd_msg);
1421 		if (ret) {
1422 			printf("cannot read in PD message, ret=%d\n", ret);
1423 			return;
1424 		}
1425 	}
1426 
1427 	if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1428 		debug("IRQ: PD received hardreset\n");
1429 		ret = fusb302_pd_reset(chip);
1430 		if (ret) {
1431 			printf("cannot PD reset, ret=%d\n", ret);
1432 			return;
1433 		}
1434 		tcpm_pd_hard_reset(chip->tcpm_port);
1435 	}
1436 
1437 	if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1438 		debug("IRQ: PD sent good CRC\n");
1439 		ret = fusb302_pd_read_message(chip, &pd_msg);
1440 		if (ret) {
1441 			printf("cannot read in PD message, ret=%d\n", ret);
1442 			return;
1443 		}
1444 	}
1445 }
1446 
fusb302_probe(struct udevice * dev)1447 static int fusb302_probe(struct udevice *dev)
1448 {
1449 	struct fusb302_chip *chip = dev_get_priv(dev);
1450 	int ret = 0;
1451 
1452 	chip->udev = dev;
1453 
1454 #if 0
1455 	/* get vbus regulator */
1456 	ret = regulator_get_by_platname("vbus5v0_typec", chip->vbus_regulator);
1457 	if (ret) {
1458 		printf("Can get the regulator: vbus5v0_typec (err=%d)\n", ret);
1459 		chip->vbus_regulator = NULL;
1460 	}
1461 #endif
1462 
1463 	chip->tcpc_dev.connector_node = dev_read_subnode(dev, "connector");
1464 	if (!ofnode_valid(chip->tcpc_dev.connector_node)) {
1465 		printf("%s: 'connector' node is not found\n", __func__);
1466 		return -ENODEV;
1467 	}
1468 
1469 	init_tcpc_dev(&chip->tcpc_dev);
1470 
1471 	ret = gpio_request_by_name(dev, "int-n-gpios", 0,
1472 				   &chip->gpio_cc_int, GPIOD_IS_IN);
1473 	if (ret) {
1474 		printf("%s: fail to get int GPIO: ret=%d\n", __func__, ret);
1475 		chip->gpio_cc_int_present = false;
1476 	} else {
1477 		chip->gpio_cc_int_present = true;
1478 	}
1479 
1480 	chip->tcpm_port = tcpm_port_init(dev, &chip->tcpc_dev);
1481 	if (IS_ERR(chip->tcpm_port)) {
1482 		printf("%s: failed to tcpm port init\n", __func__);
1483 		return PTR_ERR(chip->tcpm_port);
1484 	}
1485 
1486 	tcpm_poll_event(chip->tcpm_port);
1487 
1488 	return 0;
1489 }
1490 
1491 
fusb302_get_voltage(struct udevice * dev)1492 static int fusb302_get_voltage(struct udevice *dev)
1493 {
1494 	struct fusb302_chip *chip = dev_get_priv(dev);
1495 
1496 	return tcpm_get_voltage(chip->tcpm_port);
1497 }
1498 
fusb302_get_current(struct udevice * dev)1499 static int fusb302_get_current(struct udevice *dev)
1500 {
1501 	struct fusb302_chip *chip = dev_get_priv(dev);
1502 
1503 	return tcpm_get_current(chip->tcpm_port);
1504 }
1505 
fusb302_get_online(struct udevice * dev)1506 static int fusb302_get_online(struct udevice *dev)
1507 {
1508 	struct fusb302_chip *chip = dev_get_priv(dev);
1509 
1510 	return tcpm_get_online(chip->tcpm_port);
1511 }
1512 
1513 static struct dm_power_delivery_ops fusb302_ops = {
1514 	.get_voltage = fusb302_get_voltage,
1515 	.get_current = fusb302_get_current,
1516 	.get_online = fusb302_get_online,
1517 };
1518 
1519 static const struct udevice_id fusb302_ids[] = {
1520 	{ .compatible = "fcs,fusb302" },
1521 	{ }
1522 };
1523 
1524 U_BOOT_DRIVER(fusb302) = {
1525 	.name = "fusb302",
1526 	.id = UCLASS_PD,
1527 	.of_match = fusb302_ids,
1528 	.ops = &fusb302_ops,
1529 	.probe = fusb302_probe,
1530 	.priv_auto_alloc_size = sizeof(struct fusb302_chip),
1531 };
1532