2 * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
3 * Author: Zain Wang <zain.wang@rock-chips.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * Some ideas are from chrome ec and fairchild GPL fusb302 driver.
12 #include <linux/delay.h>
13 #include <linux/extcon.h>
14 #include <linux/freezer.h>
15 #include <linux/gpio.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/of_gpio.h>
19 #include <linux/regmap.h>
20 #include <linux/power_supply.h>
24 #define FUSB302_MAX_REG (FUSB_REG_FIFO + 50)
25 #define FUSB_MS_TO_NS(x) ((s64)x * 1000 * 1000)
27 #define FUSB_MODE_DRP 0
28 #define FUSB_MODE_UFP 1
29 #define FUSB_MODE_DFP 2
30 #define FUSB_MODE_ASS 3
32 #define TYPEC_CC_VOLT_OPEN 0
33 #define TYPEC_CC_VOLT_RA 1
34 #define TYPEC_CC_VOLT_RD 2
35 #define TYPEC_CC_VOLT_RP 3
37 #define EVENT_CC BIT(0)
38 #define EVENT_RX BIT(1)
39 #define EVENT_TX BIT(2)
40 #define EVENT_REC_RESET BIT(3)
41 #define EVENT_WORK_CONTINUE BIT(5)
42 #define EVENT_TIMER_MUX BIT(6)
43 #define EVENT_TIMER_STATE BIT(7)
44 #define FLAG_EVENT (EVENT_RX | EVENT_TIMER_MUX | \
47 #define PIN_MAP_A BIT(0)
48 #define PIN_MAP_B BIT(1)
49 #define PIN_MAP_C BIT(2)
50 #define PIN_MAP_D BIT(3)
51 #define PIN_MAP_E BIT(4)
52 #define PIN_MAP_F BIT(5)
54 static u8 fusb30x_port_used;
55 static struct fusb30x_chip *fusb30x_port_info[256];
57 static bool is_write_reg(struct device *dev, unsigned int reg)
59 if (reg >= FUSB_REG_FIFO)
62 return ((reg < (FUSB_REG_CONTROL4 + 1)) && (reg > 0x01)) ?
66 static bool is_volatile_reg(struct device *dev, unsigned int reg)
68 if (reg > FUSB_REG_CONTROL4)
72 case FUSB_REG_CONTROL0:
73 case FUSB_REG_CONTROL1:
74 case FUSB_REG_CONTROL3:
81 struct regmap_config fusb302_regmap_config = {
84 .writeable_reg = is_write_reg,
85 .volatile_reg = is_volatile_reg,
86 .max_register = FUSB302_MAX_REG,
87 .cache_type = REGCACHE_RBTREE,
90 static void dump_notify_info(struct fusb30x_chip *chip)
92 dev_dbg(chip->dev, "port %d\n", chip->port_num);
93 dev_dbg(chip->dev, "orientation %d\n", chip->notify.orientation);
94 dev_dbg(chip->dev, "power_role %d\n", chip->notify.power_role);
95 dev_dbg(chip->dev, "data_role %d\n", chip->notify.data_role);
96 dev_dbg(chip->dev, "cc %d\n", chip->notify.is_cc_connected);
97 dev_dbg(chip->dev, "pd %d\n", chip->notify.is_pd_connected);
98 dev_dbg(chip->dev, "enter_mode %d\n", chip->notify.is_enter_mode);
99 dev_dbg(chip->dev, "pin support %d\n",
100 chip->notify.pin_assignment_support);
101 dev_dbg(chip->dev, "pin def %d\n", chip->notify.pin_assignment_def);
102 dev_dbg(chip->dev, "attention %d\n", chip->notify.attention);
105 static const unsigned int fusb302_cable[] = {
118 static void fusb_set_pos_power(struct fusb30x_chip *chip, int max_vol,
126 for (i = PD_HEADER_CNT(chip->rec_head) - 1; i >= 0; i--) {
127 switch (CAP_POWER_TYPE(chip->rec_load[i])) {
130 if ((CAP_FPDO_VOLTAGE(chip->rec_load[i]) * 50) <=
132 (CAP_FPDO_CURRENT(chip->rec_load[i]) * 10) <=
134 chip->pos_power = i + 1;
135 tmp = CAP_FPDO_VOLTAGE(chip->rec_load[i]);
136 chip->pd_output_vol = tmp * 50;
137 tmp = CAP_FPDO_CURRENT(chip->rec_load[i]);
138 chip->pd_output_cur = tmp * 10;
144 if ((CAP_VPDO_VOLTAGE(chip->rec_load[i]) * 50) <=
146 (CAP_VPDO_CURRENT(chip->rec_load[i]) * 10) <=
148 chip->pos_power = i + 1;
149 tmp = CAP_VPDO_VOLTAGE(chip->rec_load[i]);
150 chip->pd_output_vol = tmp * 50;
151 tmp = CAP_VPDO_CURRENT(chip->rec_load[i]);
152 chip->pd_output_cur = tmp * 10;
157 /* not meet battery caps */
165 static int fusb302_set_pos_power_by_charge_ic(struct fusb30x_chip *chip)
167 struct power_supply *psy = NULL;
168 union power_supply_propval val;
169 enum power_supply_property psp;
170 int max_vol, max_cur;
174 psy = power_supply_get_by_phandle(chip->dev->of_node, "charge-dev");
175 if (!psy || IS_ERR(psy))
178 psp = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX;
179 if (power_supply_get_property(psy, psp, &val) == 0)
180 max_vol = val.intval / 1000;
182 psp = POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT;
183 if (power_supply_get_property(psy, psp, &val) == 0)
184 max_cur = val.intval / 1000;
186 if (max_vol > 0 && max_cur > 0)
187 fusb_set_pos_power(chip, max_vol, max_cur);
192 void fusb_irq_disable(struct fusb30x_chip *chip)
194 unsigned long irqflags = 0;
196 spin_lock_irqsave(&chip->irq_lock, irqflags);
197 if (chip->enable_irq) {
198 disable_irq_nosync(chip->gpio_int_irq);
199 chip->enable_irq = 0;
201 dev_warn(chip->dev, "irq have already disabled\n");
203 spin_unlock_irqrestore(&chip->irq_lock, irqflags);
206 void fusb_irq_enable(struct fusb30x_chip *chip)
208 unsigned long irqflags = 0;
210 spin_lock_irqsave(&chip->irq_lock, irqflags);
211 if (!chip->enable_irq) {
212 enable_irq(chip->gpio_int_irq);
213 chip->enable_irq = 1;
215 spin_unlock_irqrestore(&chip->irq_lock, irqflags);
218 static void platform_fusb_notify(struct fusb30x_chip *chip)
220 bool plugged = 0, flip = 0, dfp = 0, ufp = 0, dp = 0, usb_ss = 0;
221 union extcon_property_value property;
223 if (chip->notify.is_cc_connected)
224 chip->notify.orientation = chip->cc_polarity + 1;
226 /* avoid notify repeated */
227 if (memcmp(&chip->notify, &chip->notify_cmp,
228 sizeof(struct notify_info))) {
229 dump_notify_info(chip);
230 chip->notify.attention = 0;
231 memcpy(&chip->notify_cmp, &chip->notify,
232 sizeof(struct notify_info));
234 plugged = chip->notify.is_cc_connected ||
235 chip->notify.is_pd_connected;
236 flip = chip->notify.orientation ?
237 (chip->notify.orientation - 1) : 0;
238 dp = chip->notify.is_enter_mode;
242 usb_ss = (chip->notify.pin_assignment_def &
243 (PIN_MAP_B | PIN_MAP_D | PIN_MAP_F)) ? 1 : 0;
244 } else if (chip->notify.data_role) {
247 } else if (plugged) {
252 if (chip->notify.power_role == 0 &&
253 chip->notify.is_pd_connected &&
254 chip->pd_output_vol > 0 && chip->pd_output_cur > 0) {
255 extcon_set_state(chip->extcon, EXTCON_CHG_USB_FAST,
258 (chip->pd_output_cur << 15 |
259 chip->pd_output_vol);
260 extcon_set_property(chip->extcon, EXTCON_CHG_USB_FAST,
261 EXTCON_PROP_USB_TYPEC_POLARITY,
263 extcon_sync(chip->extcon, EXTCON_CHG_USB_FAST);
266 #ifdef CONFIG_FREEZER
268 * If system enter PM suspend, we need to wait until
269 * PM resume all of devices completion, then the flag
270 * pm_freezing will be set to false, and we can send
271 * notifier to USB/DP module safety, it make sure that
272 * USB/DP can enable power domain successfully.
275 usleep_range(10000, 11000);
278 property.intval = flip;
279 extcon_set_property(chip->extcon, EXTCON_USB,
280 EXTCON_PROP_USB_TYPEC_POLARITY, property);
281 extcon_set_property(chip->extcon, EXTCON_USB_HOST,
282 EXTCON_PROP_USB_TYPEC_POLARITY, property);
283 extcon_set_property(chip->extcon, EXTCON_DISP_DP,
284 EXTCON_PROP_USB_TYPEC_POLARITY, property);
286 property.intval = usb_ss;
287 extcon_set_property(chip->extcon, EXTCON_USB,
288 EXTCON_PROP_USB_SS, property);
289 extcon_set_property(chip->extcon, EXTCON_USB_HOST,
290 EXTCON_PROP_USB_SS, property);
291 extcon_set_property(chip->extcon, EXTCON_DISP_DP,
292 EXTCON_PROP_USB_SS, property);
293 extcon_set_state(chip->extcon, EXTCON_USB, ufp);
294 extcon_set_state(chip->extcon, EXTCON_USB_HOST, dfp);
295 extcon_set_state(chip->extcon, EXTCON_DISP_DP, dp);
296 extcon_sync(chip->extcon, EXTCON_USB);
297 extcon_sync(chip->extcon, EXTCON_USB_HOST);
298 extcon_sync(chip->extcon, EXTCON_DISP_DP);
302 static bool platform_get_device_irq_state(struct fusb30x_chip *chip)
304 return !gpiod_get_value(chip->gpio_int);
307 static void fusb_timer_start(struct hrtimer *timer, int ms)
311 ktime = ktime_set(0, FUSB_MS_TO_NS(ms));
312 hrtimer_start(timer, ktime, HRTIMER_MODE_REL);
315 static void platform_set_vbus_lvl_enable(struct fusb30x_chip *chip, int vbus_5v,
318 bool gpio_vbus_value = 0;
320 gpio_vbus_value = gpiod_get_value(chip->gpio_vbus_5v);
321 if (chip->gpio_vbus_5v) {
322 gpiod_set_raw_value(chip->gpio_vbus_5v, vbus_5v);
323 /* Only set state here, don't sync notifier to PMIC */
324 extcon_set_state(chip->extcon, EXTCON_USB_VBUS_EN, vbus_5v);
326 extcon_set_state(chip->extcon, EXTCON_USB_VBUS_EN, vbus_5v);
327 extcon_sync(chip->extcon, EXTCON_USB_VBUS_EN);
328 dev_info(chip->dev, "fusb302 send extcon to %s vbus 5v\n", vbus_5v ? "enable" : "disable");
331 if (chip->gpio_vbus_other)
332 gpiod_set_raw_value(chip->gpio_vbus_5v, vbus_other);
334 if (chip->gpio_discharge && !vbus_5v && gpio_vbus_value) {
335 gpiod_set_value(chip->gpio_discharge, 1);
337 gpiod_set_value(chip->gpio_discharge, 0);
341 static void set_state(struct fusb30x_chip *chip, enum connection_state state)
343 dev_dbg(chip->dev, "port %d, state %d\n", chip->port_num, state);
345 dev_info(chip->dev, "PD disabled\n");
346 chip->conn_state = state;
349 chip->work_continue = 1;
352 static int tcpm_get_message(struct fusb30x_chip *chip)
357 regmap_raw_read(chip->regmap, FUSB_REG_FIFO, buf, 3);
358 chip->rec_head = (buf[1] & 0xff) | ((buf[2] << 8) & 0xff00);
360 len = PD_HEADER_CNT(chip->rec_head) << 2;
361 regmap_raw_read(chip->regmap, FUSB_REG_FIFO, buf, len + 4);
363 memcpy(chip->rec_load, buf, len);
368 static void fusb302_flush_rx_fifo(struct fusb30x_chip *chip)
370 tcpm_get_message(chip);
373 static int tcpm_get_cc(struct fusb30x_chip *chip, int *CC1, int *CC2)
379 *CC1 = TYPEC_CC_VOLT_OPEN;
380 *CC2 = TYPEC_CC_VOLT_OPEN;
382 if (chip->cc_state & 0x01)
387 if (chip->cc_state & 0x04) {
388 regmap_read(chip->regmap, FUSB_REG_SWITCHES0, &store);
389 /* measure cc1 first */
390 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES0,
391 SWITCHES0_MEAS_CC1 | SWITCHES0_MEAS_CC2 |
392 SWITCHES0_PU_EN1 | SWITCHES0_PU_EN2 |
393 SWITCHES0_PDWN1 | SWITCHES0_PDWN2,
394 SWITCHES0_PDWN1 | SWITCHES0_PDWN2 |
396 usleep_range(250, 300);
398 regmap_read(chip->regmap, FUSB_REG_STATUS0, &val);
399 val &= STATUS0_BC_LVL;
403 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES0,
404 SWITCHES0_MEAS_CC1 | SWITCHES0_MEAS_CC2 |
405 SWITCHES0_PU_EN1 | SWITCHES0_PU_EN2 |
406 SWITCHES0_PDWN1 | SWITCHES0_PDWN2,
407 SWITCHES0_PDWN1 | SWITCHES0_PDWN2 |
409 usleep_range(250, 300);
411 regmap_read(chip->regmap, FUSB_REG_STATUS0, &val);
412 val &= STATUS0_BC_LVL;
415 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES0,
416 SWITCHES0_MEAS_CC1 | SWITCHES0_MEAS_CC2,
419 regmap_read(chip->regmap, FUSB_REG_SWITCHES0, &store);
421 val &= ~(SWITCHES0_MEAS_CC1 | SWITCHES0_MEAS_CC2 |
422 SWITCHES0_PU_EN1 | SWITCHES0_PU_EN2);
423 if (chip->cc_state & 0x01) {
424 val |= SWITCHES0_MEAS_CC1 | SWITCHES0_PU_EN1;
426 val |= SWITCHES0_MEAS_CC2 | SWITCHES0_PU_EN2;
428 regmap_write(chip->regmap, FUSB_REG_SWITCHES0, val);
430 regmap_write(chip->regmap, FUSB_REG_MEASURE, chip->cc_meas_high);
431 usleep_range(250, 300);
433 regmap_read(chip->regmap, FUSB_REG_STATUS0, &val);
434 if (val & STATUS0_COMP) {
439 regmap_write(chip->regmap, FUSB_REG_MEASURE, chip->cc_meas_high);
440 usleep_range(250, 300);
441 regmap_read(chip->regmap, FUSB_REG_STATUS0, &val);
442 if (val & STATUS0_COMP) {
444 if (comp_times == 3) {
445 *CC_MEASURE = TYPEC_CC_VOLT_OPEN;
446 regmap_write(chip->regmap, FUSB_REG_SWITCHES0, store);
451 regmap_write(chip->regmap, FUSB_REG_MEASURE, chip->cc_meas_low);
452 regmap_read(chip->regmap, FUSB_REG_MEASURE, &val);
453 usleep_range(250, 300);
455 regmap_read(chip->regmap, FUSB_REG_STATUS0, &val);
457 if (val & STATUS0_COMP)
458 *CC_MEASURE = TYPEC_CC_VOLT_RD;
460 *CC_MEASURE = TYPEC_CC_VOLT_RA;
461 regmap_write(chip->regmap, FUSB_REG_SWITCHES0, store);
468 static int tcpm_set_cc(struct fusb30x_chip *chip, int mode)
472 val &= ~(SWITCHES0_PU_EN1 | SWITCHES0_PU_EN2 |
473 SWITCHES0_PDWN1 | SWITCHES0_PDWN2);
479 if (chip->togdone_pullup)
480 val |= SWITCHES0_PU_EN2;
482 val |= SWITCHES0_PU_EN1;
485 val |= SWITCHES0_PDWN1 | SWITCHES0_PDWN2;
488 val |= SWITCHES0_PDWN1 | SWITCHES0_PDWN2;
494 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES0, mask, val);
498 static int tcpm_set_rx_enable(struct fusb30x_chip *chip, int enable)
503 if (chip->cc_polarity)
504 val |= SWITCHES0_MEAS_CC2;
506 val |= SWITCHES0_MEAS_CC1;
507 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES0,
508 SWITCHES0_MEAS_CC1 | SWITCHES0_MEAS_CC2,
510 fusb302_flush_rx_fifo(chip);
511 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES1,
512 SWITCHES1_AUTO_CRC, SWITCHES1_AUTO_CRC);
515 * bit of a hack here.
516 * when this function is called to disable rx (enable=0)
517 * using it as an indication of detach (gulp!)
518 * to reset our knowledge of where
519 * the toggle state machine landed.
521 chip->togdone_pullup = 0;
524 tcpm_set_cc(chip, FUSB_MODE_DRP);
525 regmap_update_bits(chip->regmap, FUSB_REG_CONTROL2,
526 CONTROL2_TOG_RD_ONLY,
527 CONTROL2_TOG_RD_ONLY);
529 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES0,
530 SWITCHES0_MEAS_CC1 | SWITCHES0_MEAS_CC2,
532 regmap_update_bits(chip->regmap,
533 FUSB_REG_SWITCHES1, SWITCHES1_AUTO_CRC, 0);
539 static int tcpm_set_msg_header(struct fusb30x_chip *chip)
541 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES1,
542 SWITCHES1_POWERROLE | SWITCHES1_DATAROLE,
543 (chip->notify.power_role << 7) |
544 (chip->notify.data_role << 4));
545 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES1,
546 SWITCHES1_SPECREV, 2 << 5);
550 static int tcpm_set_polarity(struct fusb30x_chip *chip, bool polarity)
554 #ifdef FUSB_VCONN_SUPPORT
555 if (chip->vconn_enabled) {
557 val |= SWITCHES0_VCONN_CC1;
559 val |= SWITCHES0_VCONN_CC2;
564 val |= SWITCHES0_MEAS_CC2;
566 val |= SWITCHES0_MEAS_CC1;
568 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES0,
569 SWITCHES0_VCONN_CC1 | SWITCHES0_VCONN_CC2 |
570 SWITCHES0_MEAS_CC1 | SWITCHES0_MEAS_CC2,
575 val |= SWITCHES1_TXCC2;
577 val |= SWITCHES1_TXCC1;
578 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES1,
579 SWITCHES1_TXCC1 | SWITCHES1_TXCC2,
582 chip->cc_polarity = polarity;
587 static int tcpm_set_vconn(struct fusb30x_chip *chip, int enable)
592 tcpm_set_polarity(chip, chip->cc_polarity);
594 val &= ~(SWITCHES0_VCONN_CC1 | SWITCHES0_VCONN_CC2);
595 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES0,
596 SWITCHES0_VCONN_CC1 | SWITCHES0_VCONN_CC2,
599 chip->vconn_enabled = enable;
603 static void fusb302_pd_reset(struct fusb30x_chip *chip)
605 regmap_write(chip->regmap, FUSB_REG_RESET, RESET_PD_RESET);
606 regmap_reinit_cache(chip->regmap, &fusb302_regmap_config);
609 static void tcpm_select_rp_value(struct fusb30x_chip *chip, u32 rp)
613 regmap_read(chip->regmap, FUSB_REG_CONTROL0, &control0_reg);
615 control0_reg &= ~CONTROL0_HOST_CUR;
617 * according to the host current, the compare value is different
620 /* host pull up current is 80ua , high voltage is 1.596v, low is 0.21v */
622 chip->cc_meas_high = 0x26;
623 chip->cc_meas_low = 0x5;
624 control0_reg |= CONTROL0_HOST_CUR_USB;
626 /* host pull up current is 180ua , high voltage is 1.596v, low is 0.42v */
628 chip->cc_meas_high = 0x26;
629 chip->cc_meas_low = 0xa;
630 control0_reg |= CONTROL0_HOST_CUR_1A5;
632 /* host pull up current is 330ua , high voltage is 2.604v, low is 0.798v*/
634 chip->cc_meas_high = 0x26;
635 chip->cc_meas_low = 0x13;
636 control0_reg |= CONTROL0_HOST_CUR_3A0;
639 chip->cc_meas_high = 0x26;
640 chip->cc_meas_low = 0xa;
641 control0_reg |= CONTROL0_HOST_CUR_1A5;
645 regmap_write(chip->regmap, FUSB_REG_CONTROL0, control0_reg);
648 static void tcpm_init(struct fusb30x_chip *chip)
653 regmap_read(chip->regmap, FUSB_REG_DEVICEID, &tmp);
654 chip->chip_id = (u8)tmp;
655 platform_set_vbus_lvl_enable(chip, 0, 0);
656 chip->notify.is_cc_connected = 0;
659 /* restore default settings */
660 regmap_update_bits(chip->regmap, FUSB_REG_RESET, RESET_SW_RESET,
662 fusb302_pd_reset(chip);
663 /* set auto_retry and number of retries */
664 regmap_update_bits(chip->regmap, FUSB_REG_CONTROL3,
665 CONTROL3_AUTO_RETRY | CONTROL3_N_RETRIES,
666 CONTROL3_AUTO_RETRY | CONTROL3_N_RETRIES),
670 val &= ~(MASK_M_BC_LVL | MASK_M_COLLISION | MASK_M_ALERT |
672 regmap_write(chip->regmap, FUSB_REG_MASK, val);
675 val &= ~(MASKA_M_TOGDONE | MASKA_M_RETRYFAIL | MASKA_M_HARDSENT |
676 MASKA_M_TXSENT | MASKA_M_HARDRST);
677 regmap_write(chip->regmap, FUSB_REG_MASKA, val);
680 val = ~MASKB_M_GCRCSEND;
681 regmap_write(chip->regmap, FUSB_REG_MASKB, val);
684 regmap_update_bits(chip->regmap, FUSB_REG_CONTROL2,
685 CONTROL2_MODE | CONTROL2_TOGGLE,
686 (1 << 1) | CONTROL2_TOGGLE);
688 regmap_update_bits(chip->regmap, FUSB_REG_CONTROL2,
689 CONTROL2_TOG_RD_ONLY,
690 CONTROL2_TOG_RD_ONLY);
693 tcpm_select_rp_value(chip, TYPEC_RP_1A5);
694 /* Interrupts Enable */
695 regmap_update_bits(chip->regmap, FUSB_REG_CONTROL0, CONTROL0_INT_MASK,
698 tcpm_set_polarity(chip, 0);
699 tcpm_set_vconn(chip, 0);
701 regmap_write(chip->regmap, FUSB_REG_POWER, 0xf);
704 static void pd_execute_hard_reset(struct fusb30x_chip *chip)
708 if (chip->notify.power_role)
709 set_state(chip, policy_src_transition_default);
711 set_state(chip, policy_snk_transition_default);
714 static void tcpc_alert(struct fusb30x_chip *chip, int *evt)
716 int interrupt, interrupta, interruptb;
719 regmap_read(chip->regmap, FUSB_REG_INTERRUPT, &interrupt);
720 regmap_read(chip->regmap, FUSB_REG_INTERRUPTA, &interrupta);
721 regmap_read(chip->regmap, FUSB_REG_INTERRUPTB, &interruptb);
723 if (interrupt & INTERRUPT_BC_LVL) {
724 if (chip->notify.is_cc_connected)
728 if (interrupt & INTERRUPT_VBUSOK) {
729 if (chip->notify.is_cc_connected)
733 if (interrupta & INTERRUPTA_TOGDONE) {
735 regmap_read(chip->regmap, FUSB_REG_STATUS1A, &val);
736 chip->cc_state = ((u8)val >> 3) & 0x07;
738 regmap_update_bits(chip->regmap, FUSB_REG_CONTROL2,
742 val &= ~(SWITCHES0_PU_EN1 | SWITCHES0_PU_EN2 |
743 SWITCHES0_PDWN1 | SWITCHES0_PDWN2);
745 if (chip->cc_state & 0x01)
746 val |= SWITCHES0_PU_EN1;
748 val |= SWITCHES0_PU_EN2;
750 regmap_update_bits(chip->regmap, FUSB_REG_SWITCHES0,
751 SWITCHES0_PU_EN1 | SWITCHES0_PU_EN2 |
752 SWITCHES0_PDWN1 | SWITCHES0_PDWN2,
756 if (interrupta & INTERRUPTA_TXSENT) {
758 fusb302_flush_rx_fifo(chip);
759 chip->tx_state = tx_success;
762 if (interruptb & INTERRUPTB_GCRCSENT)
765 if (interrupta & INTERRUPTA_HARDRST) {
766 fusb302_pd_reset(chip);
767 pd_execute_hard_reset(chip);
768 *evt |= EVENT_REC_RESET;
771 if (interrupta & INTERRUPTA_RETRYFAIL) {
773 chip->tx_state = tx_failed;
776 if (interrupta & INTERRUPTA_HARDSENT) {
777 chip->tx_state = tx_success;
778 chip->timer_state = T_DISABLED;
783 static void mux_alert(struct fusb30x_chip *chip, int *evt)
785 if (!chip->timer_mux) {
786 *evt |= EVENT_TIMER_MUX;
787 chip->timer_mux = T_DISABLED;
790 if (!chip->timer_state) {
791 *evt |= EVENT_TIMER_STATE;
792 chip->timer_state = T_DISABLED;
795 if (chip->work_continue) {
796 *evt |= EVENT_WORK_CONTINUE;
797 chip->work_continue = 0;
801 static void set_state_unattached(struct fusb30x_chip *chip)
803 dev_info(chip->dev, "connection has disconnected\n");
805 tcpm_set_rx_enable(chip, 0);
806 chip->conn_state = unattached;
807 tcpm_set_cc(chip, FUSB_MODE_DRP);
809 /* claer notify_info */
810 memset(&chip->notify, 0, sizeof(struct notify_info));
811 platform_fusb_notify(chip);
813 if (chip->gpio_discharge)
814 gpiod_set_value(chip->gpio_discharge, 1);
816 if (chip->gpio_discharge)
817 gpiod_set_value(chip->gpio_discharge, 0);
820 static int tcpm_check_vbus(struct fusb30x_chip *chip)
824 /* Read status register */
825 regmap_read(chip->regmap, FUSB_REG_STATUS0, &val);
827 return (val & STATUS0_VBUSOK) ? 1 : 0;
830 static void set_mesg(struct fusb30x_chip *chip, int cmd, int is_DMT)
833 struct PD_CAP_INFO *pd_cap_info = &chip->pd_cap_info;
835 chip->send_head = ((chip->msg_id & 0x7) << 9) |
836 ((chip->notify.power_role & 0x1) << 8) |
838 ((chip->notify.data_role & 0x1) << 5);
842 case DMT_SOURCECAPABILITIES:
843 chip->send_head |= ((chip->n_caps_used & 0x3) << 12) | (cmd & 0xf);
845 for (i = 0; i < chip->n_caps_used; i++) {
846 chip->send_load[i] = (pd_cap_info->supply_type << 30) |
847 (pd_cap_info->dual_role_power << 29) |
848 (pd_cap_info->usb_suspend_support << 28) |
849 (pd_cap_info->externally_powered << 27) |
850 (pd_cap_info->usb_communications_cap << 26) |
851 (pd_cap_info->data_role_swap << 25) |
852 (pd_cap_info->peak_current << 20) |
853 (chip->source_power_supply[i] << 10) |
854 (chip->source_max_current[i]);
858 chip->send_head |= ((1 << 12) | (cmd & 0xf));
859 /* send request with FVRDO */
860 chip->send_load[0] = (chip->pos_power << 28) |
866 switch (CAP_POWER_TYPE(chip->rec_load[chip->pos_power - 1])) {
869 chip->send_load[0] |= ((CAP_FPDO_VOLTAGE(chip->rec_load[chip->pos_power - 1]) << 10) & 0x3ff);
870 chip->send_load[0] |= (CAP_FPDO_CURRENT(chip->rec_load[chip->pos_power - 1]) & 0x3ff);
874 chip->send_load[0] |= ((CAP_VPDO_VOLTAGE(chip->rec_load[chip->pos_power - 1]) << 10) & 0x3ff);
875 chip->send_load[0] |= (CAP_VPDO_CURRENT(chip->rec_load[chip->pos_power - 1]) & 0x3ff);
878 /* not meet battery caps */
882 case DMT_SINKCAPABILITIES:
884 case DMT_VENDERDEFINED:
890 chip->send_head |= (cmd & 0xf);
894 static void set_vdm_mesg(struct fusb30x_chip *chip, int cmd, int type, int mode)
896 chip->send_head = (chip->msg_id & 0x7) << 9;
897 chip->send_head |= (chip->notify.power_role & 0x1) << 8;
899 chip->send_head = ((chip->msg_id & 0x7) << 9) |
900 ((chip->notify.power_role & 0x1) << 8) |
902 ((chip->notify.data_role & 0x1) << 5) |
903 (DMT_VENDERDEFINED & 0xf);
905 chip->send_load[0] = (1 << 15) |
911 case VDM_DISCOVERY_ID:
912 case VDM_DISCOVERY_SVIDS:
914 chip->send_load[0] |= (0xff00 << 16);
915 chip->send_head |= (1 << 12);
917 case VDM_DISCOVERY_MODES:
918 chip->send_load[0] |=
919 (chip->vdm_svid[chip->val_tmp >> 1] << 16);
920 chip->send_head |= (1 << 12);
923 chip->send_head |= (1 << 12);
924 chip->send_load[0] |= (mode << 8) | (0xff01 << 16);
927 chip->send_head |= (1 << 12);
928 chip->send_load[0] |= (0x0f << 8) | (0xff01 << 16);
930 case VDM_DP_STATUS_UPDATE:
931 chip->send_head |= (2 << 12);
932 chip->send_load[0] |= (1 << 8) | (0xff01 << 16);
933 chip->send_load[1] = 5;
936 chip->send_head |= (2 << 12);
937 chip->send_load[0] |= (1 << 8) | (0xff01 << 16);
938 chip->send_load[1] = (chip->notify.pin_assignment_def << 8) |
946 static enum tx_state policy_send_hardrst(struct fusb30x_chip *chip, int evt)
948 switch (chip->tx_state) {
950 regmap_update_bits(chip->regmap, FUSB_REG_CONTROL3,
951 CONTROL3_SEND_HARDRESET,
952 CONTROL3_SEND_HARDRESET);
953 chip->tx_state = tx_busy;
954 chip->timer_state = T_BMC_TIMEOUT;
955 fusb_timer_start(&chip->timer_state_machine,
959 if (evt & EVENT_TIMER_STATE)
960 chip->tx_state = tx_success;
963 return chip->tx_state;
966 static enum tx_state policy_send_data(struct fusb30x_chip *chip)
972 switch (chip->tx_state) {
974 senddata[pos++] = FUSB_TKN_SYNC1;
975 senddata[pos++] = FUSB_TKN_SYNC1;
976 senddata[pos++] = FUSB_TKN_SYNC1;
977 senddata[pos++] = FUSB_TKN_SYNC2;
979 len = PD_HEADER_CNT(chip->send_head) << 2;
980 senddata[pos++] = FUSB_TKN_PACKSYM | ((len + 2) & 0x1f);
982 senddata[pos++] = chip->send_head & 0xff;
983 senddata[pos++] = (chip->send_head >> 8) & 0xff;
985 memcpy(&senddata[pos], chip->send_load, len);
988 senddata[pos++] = FUSB_TKN_JAMCRC;
989 senddata[pos++] = FUSB_TKN_EOP;
990 senddata[pos++] = FUSB_TKN_TXOFF;
991 senddata[pos++] = FUSB_TKN_TXON;
993 regmap_raw_write(chip->regmap, FUSB_REG_FIFO, senddata, pos);
994 chip->tx_state = tx_busy;
1002 return chip->tx_state;
1005 static void process_vdm_msg(struct fusb30x_chip *chip)
1007 u32 vdm_header = chip->rec_load[0];
1011 /* can't procee unstructed vdm msg */
1012 if (!GET_VDMHEAD_STRUCT_TYPE(vdm_header))
1015 switch (GET_VDMHEAD_CMD_TYPE(vdm_header)) {
1017 switch (GET_VDMHEAD_CMD(vdm_header)) {
1019 dev_info(chip->dev, "attention, dp_status %x\n",
1021 chip->notify.attention = 1;
1022 chip->vdm_state = 6;
1025 dev_warn(chip->dev, "rec unknown init vdm msg\n");
1030 switch (GET_VDMHEAD_CMD(vdm_header)) {
1031 case VDM_DISCOVERY_ID:
1032 chip->vdm_id = chip->rec_load[1];
1034 case VDM_DISCOVERY_SVIDS:
1035 for (i = 0; i < 6; i++) {
1036 tmp = (chip->rec_load[i + 1] >> 16) &
1039 chip->vdm_svid[i * 2] = tmp;
1040 chip->vdm_svid_num++;
1045 tmp = (chip->rec_load[i + 1] & 0x0000ffff);
1047 chip->vdm_svid[i * 2 + 1] = tmp;
1048 chip->vdm_svid_num++;
1054 case VDM_DISCOVERY_MODES:
1055 /* indicate there are some vdo modes */
1056 if (PD_HEADER_CNT(chip->rec_head) > 1) {
1058 * store mode config,
1059 * enter first mode default
1061 if (!((chip->rec_load[1] >> 8) & 0x3f)) {
1065 chip->notify.pin_assignment_support = 0;
1066 chip->notify.pin_assignment_def = 0;
1067 chip->notify.pin_assignment_support =
1068 (chip->rec_load[1] >> 8) & 0x3f;
1069 tmp = chip->notify.pin_assignment_support;
1070 for (i = 0; i < 6; i++) {
1076 chip->notify.pin_assignment_def = 0x20 >> i;
1080 case VDM_ENTER_MODE:
1083 case VDM_DP_STATUS_UPDATE:
1084 dev_dbg(chip->dev, "dp_status 0x%x\n",
1091 "DP config successful, pin_assignment 0x%x\n",
1092 chip->notify.pin_assignment_def);
1093 chip->notify.is_enter_mode = 1;
1100 dev_warn(chip->dev, "REC NACK for 0x%x\n",
1101 GET_VDMHEAD_CMD(vdm_header));
1103 chip->vdm_state = 0xff;
1108 static int vdm_send_discoveryid(struct fusb30x_chip *chip, int evt)
1112 switch (chip->vdm_send_state) {
1114 set_vdm_mesg(chip, VDM_DISCOVERY_ID, VDM_TYPE_INIT, 0);
1117 chip->vdm_send_state++;
1119 tmp = policy_send_data(chip);
1120 if (tmp == tx_success) {
1121 chip->vdm_send_state++;
1122 chip->timer_state = T_SENDER_RESPONSE;
1123 fusb_timer_start(&chip->timer_state_machine,
1125 } else if (tmp == tx_failed) {
1126 dev_warn(chip->dev, "VDM_DISCOVERY_ID send failed\n");
1127 /* disable auto_vdm_machine */
1128 chip->vdm_state = 0xff;
1131 if (chip->vdm_send_state != 2)
1134 if (evt & EVENT_TIMER_STATE) {
1135 dev_warn(chip->dev, "VDM_DISCOVERY_ID time out\n");
1136 chip->vdm_state = 0xff;
1137 chip->work_continue = 1;
1142 chip->vdm_send_state = 0;
1145 return -EINPROGRESS;
1148 static int vdm_send_discoverysvid(struct fusb30x_chip *chip, int evt)
1152 switch (chip->vdm_send_state) {
1154 set_vdm_mesg(chip, VDM_DISCOVERY_SVIDS, VDM_TYPE_INIT, 0);
1155 memset(chip->vdm_svid, 0, 12);
1156 chip->vdm_svid_num = 0;
1158 chip->vdm_send_state++;
1160 tmp = policy_send_data(chip);
1161 if (tmp == tx_success) {
1162 chip->vdm_send_state++;
1163 chip->timer_state = T_SENDER_RESPONSE;
1164 fusb_timer_start(&chip->timer_state_machine,
1166 } else if (tmp == tx_failed) {
1167 dev_warn(chip->dev, "VDM_DISCOVERY_SVIDS send failed\n");
1168 /* disable auto_vdm_machine */
1169 chip->vdm_state = 0xff;
1172 if (chip->vdm_send_state != 2)
1175 if (evt & EVENT_TIMER_STATE) {
1176 dev_warn(chip->dev, "VDM_DISCOVERY_SVIDS time out\n");
1177 chip->vdm_state = 0xff;
1178 chip->work_continue = 1;
1181 if (!chip->vdm_svid_num)
1183 chip->vdm_send_state = 0;
1186 return -EINPROGRESS;
1189 static int vdm_send_discoverymodes(struct fusb30x_chip *chip, int evt)
1193 if ((chip->val_tmp >> 1) != chip->vdm_svid_num) {
1194 switch (chip->vdm_send_state) {
1196 set_vdm_mesg(chip, VDM_DISCOVERY_MODES,
1199 chip->vdm_send_state++;
1201 tmp = policy_send_data(chip);
1202 if (tmp == tx_success) {
1203 chip->vdm_send_state++;
1204 chip->timer_state = T_SENDER_RESPONSE;
1205 fusb_timer_start(&chip->timer_state_machine,
1207 } else if (tmp == tx_failed) {
1209 "VDM_DISCOVERY_MODES send failed\n");
1210 chip->vdm_state = 0xff;
1213 if (chip->vdm_send_state != 2)
1216 if (evt & EVENT_TIMER_STATE) {
1218 "VDM_DISCOVERY_MODES time out\n");
1219 chip->vdm_state = 0xff;
1220 chip->work_continue = 1;
1223 if (!(chip->val_tmp & 1))
1225 chip->val_tmp &= 0xfe;
1227 chip->vdm_send_state = 0;
1228 chip->work_continue = 1;
1236 return -EINPROGRESS;
1239 static int vdm_send_entermode(struct fusb30x_chip *chip, int evt)
1243 switch (chip->vdm_send_state) {
1245 set_vdm_mesg(chip, VDM_ENTER_MODE, VDM_TYPE_INIT, 1);
1247 chip->vdm_send_state++;
1248 chip->notify.is_enter_mode = 0;
1250 tmp = policy_send_data(chip);
1251 if (tmp == tx_success) {
1252 chip->vdm_send_state++;
1253 chip->timer_state = T_SENDER_RESPONSE;
1254 fusb_timer_start(&chip->timer_state_machine,
1256 } else if (tmp == tx_failed) {
1257 dev_warn(chip->dev, "VDM_ENTER_MODE send failed\n");
1258 /* disable auto_vdm_machine */
1259 chip->vdm_state = 0xff;
1262 if (chip->vdm_send_state != 2)
1265 if (evt & EVENT_TIMER_STATE) {
1266 dev_warn(chip->dev, "VDM_ENTER_MODE time out\n");
1267 chip->vdm_state = 0xff;
1268 chip->work_continue = 1;
1274 chip->vdm_send_state = 0;
1277 return -EINPROGRESS;
1280 static int vdm_send_getdpstatus(struct fusb30x_chip *chip, int evt)
1284 switch (chip->vdm_send_state) {
1286 set_vdm_mesg(chip, VDM_DP_STATUS_UPDATE, VDM_TYPE_INIT, 1);
1288 chip->vdm_send_state++;
1290 tmp = policy_send_data(chip);
1291 if (tmp == tx_success) {
1292 chip->vdm_send_state++;
1293 chip->timer_state = T_SENDER_RESPONSE;
1294 fusb_timer_start(&chip->timer_state_machine,
1296 } else if (tmp == tx_failed) {
1298 "VDM_DP_STATUS_UPDATE send failed\n");
1299 /* disable auto_vdm_machine */
1300 chip->vdm_state = 0xff;
1303 if (chip->vdm_send_state != 2)
1306 if (evt & EVENT_TIMER_STATE) {
1307 dev_warn(chip->dev, "VDM_DP_STATUS_UPDATE time out\n");
1308 chip->vdm_state = 0xff;
1309 chip->work_continue = 1;
1315 chip->vdm_send_state = 0;
1318 return -EINPROGRESS;
1321 static int vdm_send_dpconfig(struct fusb30x_chip *chip, int evt)
1325 switch (chip->vdm_send_state) {
1327 set_vdm_mesg(chip, VDM_DP_CONFIG, VDM_TYPE_INIT, 0);
1329 chip->vdm_send_state++;
1331 tmp = policy_send_data(chip);
1332 if (tmp == tx_success) {
1333 chip->vdm_send_state++;
1334 chip->timer_state = T_SENDER_RESPONSE;
1335 fusb_timer_start(&chip->timer_state_machine,
1337 } else if (tmp == tx_failed) {
1338 dev_warn(chip->dev, "vdm_send_dpconfig send failed\n");
1339 /* disable auto_vdm_machine */
1340 chip->vdm_state = 0xff;
1343 if (chip->vdm_send_state != 2)
1346 if (evt & EVENT_TIMER_STATE) {
1347 dev_warn(chip->dev, "vdm_send_dpconfig time out\n");
1348 chip->vdm_state = 0xff;
1349 chip->work_continue = 1;
1355 chip->vdm_send_state = 0;
1358 return -EINPROGRESS;
1361 static void auto_vdm_machine(struct fusb30x_chip *chip, int evt)
1363 switch (chip->vdm_state) {
1365 if (vdm_send_discoveryid(chip, evt))
1370 if (vdm_send_discoverysvid(chip, evt))
1375 if (vdm_send_discoverymodes(chip, evt))
1380 if (vdm_send_entermode(chip, evt))
1385 if (vdm_send_dpconfig(chip, evt))
1387 chip->vdm_state = 6;
1390 if (vdm_send_getdpstatus(chip, evt))
1395 platform_fusb_notify(chip);
1400 static void fusb_state_disabled(struct fusb30x_chip *chip, int evt)
1402 platform_fusb_notify(chip);
1405 static void fusb_state_unattached(struct fusb30x_chip *chip, int evt)
1407 chip->notify.is_cc_connected = 0;
1408 if ((evt & EVENT_CC) && chip->cc_state) {
1409 if (chip->cc_state & 0x04)
1410 set_state(chip, attach_wait_sink);
1412 set_state(chip, attach_wait_source);
1414 tcpm_get_cc(chip, &chip->cc1, &chip->cc2);
1415 chip->debounce_cnt = 0;
1416 chip->timer_mux = 2;
1417 fusb_timer_start(&chip->timer_mux_machine, chip->timer_mux);
1421 static void fusb_state_attach_wait_sink(struct fusb30x_chip *chip, int evt)
1425 if (evt & EVENT_TIMER_MUX) {
1426 tcpm_get_cc(chip, &cc1, &cc2);
1428 if ((chip->cc1 == cc1) && (chip->cc2 == cc2)) {
1429 chip->debounce_cnt++;
1433 chip->debounce_cnt = 0;
1436 if (chip->debounce_cnt > N_DEBOUNCE_CNT) {
1437 if ((chip->cc1 != chip->cc2) &&
1438 ((!chip->cc1) || (!chip->cc2))) {
1439 set_state(chip, attached_sink);
1441 set_state_unattached(chip);
1446 chip->timer_mux = 2;
1447 fusb_timer_start(&chip->timer_mux_machine,
1452 static void fusb_state_attach_wait_source(struct fusb30x_chip *chip, int evt)
1456 if (evt & EVENT_TIMER_MUX) {
1457 tcpm_get_cc(chip, &cc1, &cc2);
1459 if ((chip->cc1 == cc1) && (chip->cc2 == cc2)) {
1460 if (chip->debounce_cnt++ == 0)
1461 platform_set_vbus_lvl_enable(chip, 1, 0);
1465 chip->debounce_cnt = 0;
1468 if (chip->debounce_cnt > N_DEBOUNCE_CNT) {
1469 if (((!chip->cc1) || (!chip->cc2)) &&
1470 ((chip->cc1 == TYPEC_CC_VOLT_RD) ||
1471 (chip->cc2 == TYPEC_CC_VOLT_RD))) {
1472 set_state(chip, attached_source);
1474 set_state_unattached(chip);
1480 chip->timer_mux = 2;
1481 fusb_timer_start(&chip->timer_mux_machine,
1486 static void fusb_state_attached_source(struct fusb30x_chip *chip, int evt)
1488 tcpm_set_polarity(chip, !(chip->cc_state & 0x01));
1489 tcpm_set_vconn(chip, 1);
1491 chip->notify.is_cc_connected = 1;
1492 if (chip->cc_state & 0x01)
1493 chip->cc_polarity = 0;
1495 chip->cc_polarity = 1;
1497 chip->notify.power_role = 1;
1498 chip->notify.data_role = 1;
1499 chip->hardrst_count = 0;
1500 set_state(chip, policy_src_startup);
1501 dev_info(chip->dev, "CC connected in %d as DFP\n", chip->cc_polarity);
1504 static void fusb_state_attached_sink(struct fusb30x_chip *chip, int evt)
1506 chip->notify.is_cc_connected = 1;
1507 if (chip->cc_state & 0x01)
1508 chip->cc_polarity = 0;
1510 chip->cc_polarity = 1;
1512 chip->notify.power_role = 0;
1513 chip->notify.data_role = 0;
1514 chip->hardrst_count = 0;
1515 set_state(chip, policy_snk_startup);
1516 dev_info(chip->dev, "CC connected in %d as UFP\n", chip->cc_polarity);
1519 static void fusb_state_src_startup(struct fusb30x_chip *chip, int evt)
1521 chip->caps_counter = 0;
1522 chip->notify.is_pd_connected = 0;
1524 chip->vdm_state = 0;
1525 chip->vdm_substate = 0;
1526 chip->vdm_send_state = 0;
1529 memset(chip->partner_cap, 0, sizeof(chip->partner_cap));
1531 tcpm_set_msg_header(chip);
1532 tcpm_set_polarity(chip, chip->cc_polarity);
1533 tcpm_set_rx_enable(chip, 1);
1535 set_state(chip, policy_src_send_caps);
1538 static void fusb_state_src_discovery(struct fusb30x_chip *chip, int evt)
1540 switch (chip->sub_state) {
1542 chip->caps_counter++;
1544 if (chip->caps_counter < N_CAPS_COUNT) {
1545 chip->timer_state = T_TYPEC_SEND_SOURCECAP;
1546 fusb_timer_start(&chip->timer_state_machine,
1548 chip->sub_state = 1;
1550 set_state(chip, disabled);
1554 if (evt & EVENT_TIMER_STATE) {
1555 set_state(chip, policy_src_send_caps);
1556 } else if ((evt & EVENT_TIMER_MUX) &&
1557 (chip->hardrst_count > N_HARDRESET_COUNT)) {
1558 if (chip->notify.is_pd_connected)
1559 set_state(chip, error_recovery);
1561 set_state(chip, disabled);
1567 static void fusb_state_src_send_caps(struct fusb30x_chip *chip, int evt)
1571 switch (chip->sub_state) {
1573 set_mesg(chip, DMT_SOURCECAPABILITIES, DATAMESSAGE);
1574 chip->sub_state = 1;
1575 chip->tx_state = tx_idle;
1578 tmp = policy_send_data(chip);
1580 if (tmp == tx_success) {
1581 chip->hardrst_count = 0;
1582 chip->caps_counter = 0;
1583 chip->timer_state = T_SENDER_RESPONSE;
1584 fusb_timer_start(&chip->timer_state_machine,
1586 chip->timer_mux = T_DISABLED;
1588 } else if (tmp == tx_failed) {
1589 set_state(chip, policy_src_discovery);
1593 if (!(evt & FLAG_EVENT))
1596 if (evt & EVENT_RX) {
1597 if ((PD_HEADER_CNT(chip->rec_head) == 1) &&
1598 (PD_HEADER_TYPE(chip->rec_head) == DMT_REQUEST)) {
1599 set_state(chip, policy_src_negotiate_cap);
1601 set_state(chip, policy_src_send_softrst);
1603 } else if (evt & EVENT_TIMER_STATE) {
1604 if (chip->hardrst_count <= N_HARDRESET_COUNT)
1605 set_state(chip, policy_src_send_hardrst);
1607 set_state(chip, disabled);
1608 } else if (evt & EVENT_TIMER_MUX) {
1609 if (chip->notify.is_pd_connected)
1610 set_state(chip, disabled);
1612 set_state(chip, error_recovery);
1618 static void fusb_state_src_negotiate_cap(struct fusb30x_chip *chip, int evt)
1623 tmp = (chip->rec_load[0] >> 28) & 0x07;
1624 if (tmp > chip->n_caps_used)
1625 set_state(chip, policy_src_cap_response);
1627 set_state(chip, policy_src_transition_supply);
1630 static void fusb_state_src_transition_supply(struct fusb30x_chip *chip,
1635 switch (chip->sub_state) {
1637 set_mesg(chip, CMT_ACCEPT, CONTROLMESSAGE);
1638 chip->tx_state = tx_idle;
1642 tmp = policy_send_data(chip);
1643 if (tmp == tx_success) {
1644 chip->timer_state = T_SRC_TRANSITION;
1646 fusb_timer_start(&chip->timer_state_machine,
1648 } else if (tmp == tx_failed) {
1649 set_state(chip, policy_src_send_softrst);
1653 if (evt & EVENT_TIMER_STATE) {
1654 chip->notify.is_pd_connected = 1;
1655 platform_set_vbus_lvl_enable(chip, 1, 0);
1656 set_mesg(chip, CMT_PS_RDY, CONTROLMESSAGE);
1657 chip->tx_state = tx_idle;
1659 chip->work_continue = 1;
1663 tmp = policy_send_data(chip);
1664 if (tmp == tx_success) {
1666 "PD connected as DFP, supporting 5V\n");
1667 set_state(chip, policy_src_ready);
1668 } else if (tmp == tx_failed) {
1669 set_state(chip, policy_src_send_softrst);
1675 static void fusb_state_src_cap_response(struct fusb30x_chip *chip, int evt)
1679 switch (chip->sub_state) {
1681 set_mesg(chip, CMT_REJECT, CONTROLMESSAGE);
1682 chip->tx_state = tx_idle;
1686 tmp = policy_send_data(chip);
1687 if (tmp == tx_success) {
1688 if (chip->notify.is_pd_connected) {
1690 "PD connected as DFP, supporting 5V\n");
1691 set_state(chip, policy_src_ready);
1693 set_state(chip, policy_src_send_hardrst);
1695 } else if (tmp == tx_failed) {
1696 set_state(chip, policy_src_send_softrst);
1702 static void fusb_state_src_transition_default(struct fusb30x_chip *chip,
1705 switch (chip->sub_state) {
1707 chip->notify.is_pd_connected = 0;
1708 platform_set_vbus_lvl_enable(chip, 0, 0);
1709 if (chip->notify.data_role)
1710 regmap_update_bits(chip->regmap,
1713 SWITCHES1_DATAROLE);
1715 regmap_update_bits(chip->regmap,
1720 chip->timer_state = T_SRC_RECOVER;
1721 fusb_timer_start(&chip->timer_state_machine,
1726 if (evt & EVENT_TIMER_STATE) {
1727 platform_set_vbus_lvl_enable(chip, 1, 0);
1728 chip->timer_mux = T_NO_RESPONSE;
1729 fusb_timer_start(&chip->timer_mux_machine,
1731 set_state(chip, policy_src_startup);
1732 dev_dbg(chip->dev, "reset over-> src startup\n");
1738 static void fusb_state_src_ready(struct fusb30x_chip *chip, int evt)
1740 if (evt & EVENT_RX) {
1741 if ((PD_HEADER_CNT(chip->rec_head)) &&
1742 (PD_HEADER_TYPE(chip->rec_head) == DMT_VENDERDEFINED)) {
1743 process_vdm_msg(chip);
1744 chip->work_continue = 1;
1745 chip->timer_state = T_DISABLED;
1749 /* TODO: swap function would be added here later on*/
1751 if (!chip->partner_cap[0])
1752 set_state(chip, policy_src_get_sink_caps);
1754 auto_vdm_machine(chip, evt);
1757 static void fusb_state_src_get_sink_cap(struct fusb30x_chip *chip, int evt)
1761 switch (chip->sub_state) {
1763 set_mesg(chip, CMT_GETSINKCAP, CONTROLMESSAGE);
1764 chip->tx_state = tx_idle;
1768 tmp = policy_send_data(chip);
1769 if (tmp == tx_success) {
1770 chip->timer_state = T_SENDER_RESPONSE;
1772 fusb_timer_start(&chip->timer_state_machine,
1774 } else if (tmp == tx_failed) {
1775 set_state(chip, policy_src_send_softrst);
1778 if (!(evt & FLAG_EVENT))
1781 if (evt & EVENT_RX) {
1782 if ((PD_HEADER_CNT(chip->rec_head)) &&
1783 (PD_HEADER_TYPE(chip->rec_head) ==
1784 DMT_SINKCAPABILITIES)) {
1786 tmp < PD_HEADER_CNT(chip->rec_head);
1788 chip->partner_cap[tmp] =
1789 chip->rec_load[tmp];
1791 set_state(chip, policy_src_ready);
1793 chip->partner_cap[0] = 0xffffffff;
1794 set_state(chip, policy_src_ready);
1796 } else if (evt & EVENT_TIMER_STATE) {
1797 dev_warn(chip->dev, "Get sink cap time out\n");
1798 chip->partner_cap[0] = 0xffffffff;
1799 set_state(chip, policy_src_ready);
1804 static void fusb_state_src_send_hardreset(struct fusb30x_chip *chip, int evt)
1808 switch (chip->sub_state) {
1810 chip->tx_state = tx_idle;
1814 tmp = policy_send_hardrst(chip, evt);
1815 if (tmp == tx_success) {
1816 chip->hardrst_count++;
1817 set_state(chip, policy_src_transition_default);
1818 } else if (tmp == tx_failed) {
1819 /* can't reach here */
1820 set_state(chip, error_recovery);
1826 static void fusb_state_src_send_softreset(struct fusb30x_chip *chip, int evt)
1830 switch (chip->sub_state) {
1832 set_mesg(chip, CMT_SOFTRESET, CONTROLMESSAGE);
1833 chip->tx_state = tx_idle;
1837 tmp = policy_send_data(chip);
1838 if (tmp == tx_success) {
1839 chip->timer_state = T_SENDER_RESPONSE;
1841 fusb_timer_start(&chip->timer_state_machine,
1843 } else if (tmp == tx_failed) {
1844 set_state(chip, policy_src_send_hardrst);
1847 if (!(evt & FLAG_EVENT))
1850 if (evt & EVENT_RX) {
1851 if ((!PD_HEADER_CNT(chip->rec_head)) &&
1852 (PD_HEADER_TYPE(chip->rec_head) == CMT_ACCEPT))
1853 set_state(chip, policy_src_send_caps);
1854 } else if (evt & EVENT_TIMER_STATE) {
1855 set_state(chip, policy_src_send_hardrst);
1861 static void fusb_state_snk_startup(struct fusb30x_chip *chip, int evt)
1863 chip->notify.is_pd_connected = 0;
1865 chip->vdm_state = 0;
1866 chip->vdm_substate = 0;
1867 chip->vdm_send_state = 0;
1869 chip->pos_power = 0;
1871 memset(chip->partner_cap, 0, sizeof(chip->partner_cap));
1873 tcpm_set_msg_header(chip);
1874 tcpm_set_polarity(chip, chip->cc_polarity);
1875 tcpm_set_rx_enable(chip, 1);
1876 set_state(chip, policy_snk_discovery);
1879 static void fusb_state_snk_discovery(struct fusb30x_chip *chip, int evt)
1881 set_state(chip, policy_snk_wait_caps);
1882 chip->timer_state = T_TYPEC_SINK_WAIT_CAP;
1883 fusb_timer_start(&chip->timer_state_machine,
1887 static void fusb_state_snk_wait_caps(struct fusb30x_chip *chip, int evt)
1889 if (evt & EVENT_RX) {
1890 if (PD_HEADER_CNT(chip->rec_head) &&
1891 PD_HEADER_TYPE(chip->rec_head) == DMT_SOURCECAPABILITIES) {
1892 chip->timer_mux = T_DISABLED;
1893 set_state(chip, policy_snk_evaluate_caps);
1895 } else if (evt & EVENT_TIMER_STATE) {
1896 if (chip->hardrst_count <= N_HARDRESET_COUNT)
1897 set_state(chip, policy_snk_send_hardrst);
1899 set_state(chip, disabled);
1900 } else if ((evt & EVENT_TIMER_MUX) &&
1901 (chip->hardrst_count > N_HARDRESET_COUNT)) {
1902 if (chip->notify.is_pd_connected)
1903 set_state(chip, error_recovery);
1905 set_state(chip, disabled);
1909 static void fusb_state_snk_evaluate_caps(struct fusb30x_chip *chip, int evt)
1913 chip->hardrst_count = 0;
1914 chip->pos_power = 0;
1916 for (tmp = 0; tmp < PD_HEADER_CNT(chip->rec_head); tmp++) {
1917 switch (CAP_POWER_TYPE(chip->rec_load[tmp])) {
1920 if (CAP_FPDO_VOLTAGE(chip->rec_load[tmp]) <= 100)
1921 chip->pos_power = tmp + 1;
1925 if (CAP_VPDO_VOLTAGE(chip->rec_load[tmp]) <= 100)
1926 chip->pos_power = tmp + 1;
1929 /* not meet battery caps */
1933 fusb302_set_pos_power_by_charge_ic(chip);
1935 if ((!chip->pos_power) || (chip->pos_power > 7)) {
1936 chip->pos_power = 0;
1937 set_state(chip, policy_snk_wait_caps);
1939 set_state(chip, policy_snk_select_cap);
1943 static void fusb_state_snk_select_cap(struct fusb30x_chip *chip, int evt)
1947 switch (chip->sub_state) {
1949 set_mesg(chip, DMT_REQUEST, DATAMESSAGE);
1950 chip->sub_state = 1;
1951 chip->tx_state = tx_idle;
1954 tmp = policy_send_data(chip);
1956 if (tmp == tx_success) {
1957 chip->timer_state = T_SENDER_RESPONSE;
1958 fusb_timer_start(&chip->timer_state_machine,
1961 } else if (tmp == tx_failed) {
1962 set_state(chip, policy_snk_discovery);
1966 if (!(evt & FLAG_EVENT))
1969 if (evt & EVENT_RX) {
1970 if (!PD_HEADER_CNT(chip->rec_head)) {
1971 switch (PD_HEADER_TYPE(chip->rec_head)) {
1974 policy_snk_transition_sink);
1975 chip->timer_state = T_PS_TRANSITION;
1976 fusb_timer_start(&chip->timer_state_machine,
1981 if (chip->notify.is_pd_connected) {
1983 "PD connected as UFP, fetching 5V\n");
1988 policy_snk_wait_caps);
1990 * make sure don't send
1991 * hard reset to prevent
1994 chip->hardrst_count =
1995 N_HARDRESET_COUNT + 1;
2002 } else if (evt & EVENT_TIMER_STATE) {
2003 set_state(chip, policy_snk_send_hardrst);
2009 static void fusb_state_snk_transition_sink(struct fusb30x_chip *chip, int evt)
2011 if (evt & EVENT_RX) {
2012 if ((!PD_HEADER_CNT(chip->rec_head)) &&
2013 (PD_HEADER_TYPE(chip->rec_head) == CMT_PS_RDY)) {
2014 chip->notify.is_pd_connected = 1;
2016 "PD connected as UFP, fetching 5V\n");
2017 set_state(chip, policy_snk_ready);
2018 } else if ((PD_HEADER_CNT(chip->rec_head)) &&
2019 (PD_HEADER_TYPE(chip->rec_head) ==
2020 DMT_SOURCECAPABILITIES)) {
2021 set_state(chip, policy_snk_evaluate_caps);
2023 } else if (evt & EVENT_TIMER_STATE) {
2024 set_state(chip, policy_snk_send_hardrst);
2028 static void fusb_state_snk_transition_default(struct fusb30x_chip *chip,
2031 switch (chip->sub_state) {
2033 chip->notify.is_pd_connected = 0;
2034 chip->timer_mux = T_NO_RESPONSE;
2035 fusb_timer_start(&chip->timer_mux_machine,
2037 chip->timer_state = T_PS_HARD_RESET_MAX + T_SAFE_0V;
2038 fusb_timer_start(&chip->timer_state_machine,
2040 if (chip->notify.data_role)
2041 tcpm_set_msg_header(chip);
2045 if (!tcpm_check_vbus(chip)) {
2047 chip->timer_state = T_SRC_RECOVER_MAX + T_SRC_TURN_ON;
2048 fusb_timer_start(&chip->timer_state_machine,
2050 } else if (evt & EVENT_TIMER_STATE) {
2051 set_state(chip, policy_snk_startup);
2055 if (tcpm_check_vbus(chip)) {
2056 chip->timer_state = T_DISABLED;
2057 set_state(chip, policy_snk_startup);
2058 } else if (evt & EVENT_TIMER_STATE) {
2059 set_state(chip, policy_snk_startup);
2065 static void fusb_state_snk_ready(struct fusb30x_chip *chip, int evt)
2067 /* TODO: snk_ready_function would be added later on*/
2068 platform_fusb_notify(chip);
2071 static void fusb_state_snk_send_hardreset(struct fusb30x_chip *chip, int evt)
2075 switch (chip->sub_state) {
2077 chip->tx_state = tx_idle;
2080 tmp = policy_send_hardrst(chip, evt);
2081 if (tmp == tx_success) {
2082 chip->hardrst_count++;
2083 set_state(chip, policy_snk_transition_default);
2084 } else if (tmp == tx_failed) {
2085 set_state(chip, error_recovery);
2091 static void fusb_state_snk_send_softreset(struct fusb30x_chip *chip, int evt)
2095 switch (chip->sub_state) {
2097 set_mesg(chip, CMT_SOFTRESET, CONTROLMESSAGE);
2098 chip->tx_state = tx_idle;
2101 tmp = policy_send_data(chip);
2102 if (tmp == tx_success) {
2103 chip->timer_state = T_SENDER_RESPONSE;
2105 fusb_timer_start(&chip->timer_state_machine,
2107 } else if (tmp == tx_failed) {
2108 /* can't reach here */
2109 set_state(chip, policy_snk_send_hardrst);
2112 if (!(evt & FLAG_EVENT))
2115 if (evt & EVENT_RX) {
2116 if ((!PD_HEADER_CNT(chip->rec_head)) &&
2117 (PD_HEADER_TYPE(chip->rec_head) == CMT_ACCEPT))
2118 set_state(chip, policy_snk_wait_caps);
2119 } else if (evt & EVENT_TIMER_STATE) {
2120 set_state(chip, policy_snk_send_hardrst);
2126 static void state_machine_typec(struct fusb30x_chip *chip)
2131 tcpc_alert(chip, &evt);
2132 mux_alert(chip, &evt);
2136 if (chip->notify.is_cc_connected) {
2137 if (evt & EVENT_CC) {
2138 if ((chip->cc_state & 0x04) &&
2139 (chip->conn_state !=
2140 policy_snk_transition_default)) {
2141 if (!tcpm_check_vbus(chip))
2142 set_state_unattached(chip);
2143 } else if (chip->conn_state !=
2144 policy_src_transition_default) {
2145 tcpm_get_cc(chip, &cc1, &cc2);
2146 if (!(chip->cc_state & 0x01))
2148 if (cc1 == TYPEC_CC_VOLT_OPEN)
2149 set_state_unattached(chip);
2154 if (evt & EVENT_RX) {
2155 tcpm_get_message(chip);
2156 if ((!PD_HEADER_CNT(chip->rec_head)) &&
2157 (PD_HEADER_TYPE(chip->rec_head) == CMT_SOFTRESET)) {
2158 if (chip->notify.power_role)
2159 set_state(chip, policy_src_send_softrst);
2161 set_state(chip, policy_snk_send_softrst);
2165 if (evt & EVENT_TX) {
2166 if (chip->tx_state == tx_success)
2169 switch (chip->conn_state) {
2171 fusb_state_disabled(chip, evt);
2173 case error_recovery:
2174 set_state_unattached(chip);
2177 fusb_state_unattached(chip, evt);
2179 case attach_wait_sink:
2180 fusb_state_attach_wait_sink(chip, evt);
2182 case attach_wait_source:
2183 fusb_state_attach_wait_source(chip, evt);
2185 case attached_source:
2186 fusb_state_attached_source(chip, evt);
2189 fusb_state_attached_sink(chip, evt);
2192 /* POWER DELIVERY */
2193 case policy_src_startup:
2194 fusb_state_src_startup(chip, evt);
2196 case policy_src_discovery:
2197 fusb_state_src_discovery(chip, evt);
2199 case policy_src_send_caps:
2200 fusb_state_src_send_caps(chip, evt);
2201 if (chip->conn_state != policy_src_negotiate_cap)
2203 case policy_src_negotiate_cap:
2204 fusb_state_src_negotiate_cap(chip, evt);
2206 case policy_src_transition_supply:
2207 fusb_state_src_transition_supply(chip, evt);
2209 case policy_src_cap_response:
2210 fusb_state_src_cap_response(chip, evt);
2212 case policy_src_transition_default:
2213 fusb_state_src_transition_default(chip, evt);
2215 case policy_src_ready:
2216 fusb_state_src_ready(chip, evt);
2218 case policy_src_get_sink_caps:
2219 fusb_state_src_get_sink_cap(chip, evt);
2221 case policy_src_send_hardrst:
2222 fusb_state_src_send_hardreset(chip, evt);
2224 case policy_src_send_softrst:
2225 fusb_state_src_send_softreset(chip, evt);
2229 case policy_snk_startup:
2230 fusb_state_snk_startup(chip, evt);
2232 case policy_snk_discovery:
2233 fusb_state_snk_discovery(chip, evt);
2235 case policy_snk_wait_caps:
2236 fusb_state_snk_wait_caps(chip, evt);
2238 case policy_snk_evaluate_caps:
2239 fusb_state_snk_evaluate_caps(chip, evt);
2241 case policy_snk_select_cap:
2242 fusb_state_snk_select_cap(chip, evt);
2244 case policy_snk_transition_sink:
2245 fusb_state_snk_transition_sink(chip, evt);
2247 case policy_snk_transition_default:
2248 fusb_state_snk_transition_default(chip, evt);
2250 case policy_snk_ready:
2251 fusb_state_snk_ready(chip, evt);
2253 case policy_snk_send_hardrst:
2254 fusb_state_snk_send_hardreset(chip, evt);
2256 case policy_snk_send_softrst:
2257 fusb_state_snk_send_softreset(chip, evt);
2265 if (chip->work_continue) {
2266 queue_work(chip->fusb30x_wq, &chip->work);
2270 if (!platform_get_device_irq_state(chip))
2271 fusb_irq_enable(chip);
2273 queue_work(chip->fusb30x_wq, &chip->work);
2276 static irqreturn_t cc_interrupt_handler(int irq, void *dev_id)
2278 struct fusb30x_chip *chip = dev_id;
2280 queue_work(chip->fusb30x_wq, &chip->work);
2281 fusb_irq_disable(chip);
2285 static int fusb_initialize_gpio(struct fusb30x_chip *chip)
2287 chip->gpio_int = devm_gpiod_get_optional(chip->dev, "int-n", GPIOD_IN);
2288 if (IS_ERR(chip->gpio_int))
2289 return PTR_ERR(chip->gpio_int);
2291 /* some board support vbus with other ways */
2292 chip->gpio_vbus_5v = devm_gpiod_get_optional(chip->dev, "vbus-5v",
2294 if (IS_ERR(chip->gpio_vbus_5v))
2296 "Could not get named GPIO for VBus5V!\n");
2298 gpiod_set_raw_value(chip->gpio_vbus_5v, 0);
2300 chip->gpio_vbus_other = devm_gpiod_get_optional(chip->dev,
2303 if (IS_ERR(chip->gpio_vbus_other))
2305 "Could not get named GPIO for VBusOther!\n");
2307 gpiod_set_raw_value(chip->gpio_vbus_other, 0);
2309 chip->gpio_discharge = devm_gpiod_get_optional(chip->dev, "discharge",
2311 if (IS_ERR(chip->gpio_discharge)) {
2313 "Could not get named GPIO for discharge!\n");
2314 chip->gpio_discharge = NULL;
2320 static enum hrtimer_restart fusb_timer_handler(struct hrtimer *timer)
2324 for (i = 0; i < fusb30x_port_used; i++) {
2325 if (timer == &fusb30x_port_info[i]->timer_state_machine) {
2326 if (fusb30x_port_info[i]->timer_state != T_DISABLED)
2327 fusb30x_port_info[i]->timer_state = 0;
2331 if (timer == &fusb30x_port_info[i]->timer_mux_machine) {
2332 if (fusb30x_port_info[i]->timer_mux != T_DISABLED)
2333 fusb30x_port_info[i]->timer_mux = 0;
2338 if (i != fusb30x_port_used)
2339 queue_work(fusb30x_port_info[i]->fusb30x_wq,
2340 &fusb30x_port_info[i]->work);
2342 return HRTIMER_NORESTART;
2345 static void fusb_initialize_timer(struct fusb30x_chip *chip)
2347 hrtimer_init(&chip->timer_state_machine, CLOCK_MONOTONIC,
2349 chip->timer_state_machine.function = fusb_timer_handler;
2351 hrtimer_init(&chip->timer_mux_machine, CLOCK_MONOTONIC,
2353 chip->timer_mux_machine.function = fusb_timer_handler;
2355 chip->timer_state = T_DISABLED;
2356 chip->timer_mux = T_DISABLED;
2359 static void fusb302_work_func(struct work_struct *work)
2361 struct fusb30x_chip *chip;
2363 chip = container_of(work, struct fusb30x_chip, work);
2364 state_machine_typec(chip);
2367 static int fusb30x_probe(struct i2c_client *client,
2368 const struct i2c_device_id *id)
2370 struct fusb30x_chip *chip;
2371 struct PD_CAP_INFO *pd_cap_info;
2374 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
2378 if (fusb30x_port_used == 0xff)
2381 chip->port_num = fusb30x_port_used++;
2382 fusb30x_port_info[chip->port_num] = chip;
2384 chip->dev = &client->dev;
2385 chip->regmap = devm_regmap_init_i2c(client, &fusb302_regmap_config);
2386 if (IS_ERR(chip->regmap)) {
2387 dev_err(&client->dev, "Failed to allocate regmap!\n");
2388 return PTR_ERR(chip->regmap);
2391 ret = fusb_initialize_gpio(chip);
2395 fusb_initialize_timer(chip);
2397 chip->fusb30x_wq = create_workqueue("fusb302_wq");
2398 INIT_WORK(&chip->work, fusb302_work_func);
2401 tcpm_set_rx_enable(chip, 0);
2402 chip->conn_state = unattached;
2403 tcpm_set_cc(chip, FUSB_MODE_DRP);
2405 chip->n_caps_used = 1;
2406 chip->source_power_supply[0] = 0x64;
2407 chip->source_max_current[0] = 0x96;
2410 * these two variable should be 1 if support DRP,
2411 * but now we do not support swap,
2412 * it will be blanked in future
2414 pd_cap_info = &chip->pd_cap_info;
2415 pd_cap_info->dual_role_power = 0;
2416 pd_cap_info->data_role_swap = 0;
2418 pd_cap_info->externally_powered = 1;
2419 pd_cap_info->usb_suspend_support = 0;
2420 pd_cap_info->usb_communications_cap = 0;
2421 pd_cap_info->supply_type = 0;
2422 pd_cap_info->peak_current = 0;
2424 chip->extcon = devm_extcon_dev_allocate(&client->dev, fusb302_cable);
2425 if (IS_ERR(chip->extcon)) {
2426 dev_err(&client->dev, "allocat extcon failed\n");
2427 return PTR_ERR(chip->extcon);
2430 ret = devm_extcon_dev_register(&client->dev, chip->extcon);
2432 dev_err(&client->dev, "failed to register extcon: %d\n",
2437 ret = extcon_set_property_capability(chip->extcon, EXTCON_USB,
2438 EXTCON_PROP_USB_TYPEC_POLARITY);
2440 dev_err(&client->dev,
2441 "failed to set USB property capability: %d\n",
2446 ret = extcon_set_property_capability(chip->extcon, EXTCON_USB_HOST,
2447 EXTCON_PROP_USB_TYPEC_POLARITY);
2449 dev_err(&client->dev,
2450 "failed to set USB_HOST property capability: %d\n",
2455 ret = extcon_set_property_capability(chip->extcon, EXTCON_DISP_DP,
2456 EXTCON_PROP_USB_TYPEC_POLARITY);
2458 dev_err(&client->dev,
2459 "failed to set DISP_DP property capability: %d\n",
2464 ret = extcon_set_property_capability(chip->extcon, EXTCON_USB,
2465 EXTCON_PROP_USB_SS);
2467 dev_err(&client->dev,
2468 "failed to set USB USB_SS property capability: %d\n",
2473 ret = extcon_set_property_capability(chip->extcon, EXTCON_USB_HOST,
2474 EXTCON_PROP_USB_SS);
2476 dev_err(&client->dev,
2477 "failed to set USB_HOST USB_SS property capability: %d\n",
2482 ret = extcon_set_property_capability(chip->extcon, EXTCON_DISP_DP,
2483 EXTCON_PROP_USB_SS);
2485 dev_err(&client->dev,
2486 "failed to set DISP_DP USB_SS property capability: %d\n",
2491 ret = extcon_set_property_capability(chip->extcon, EXTCON_CHG_USB_FAST,
2492 EXTCON_PROP_USB_TYPEC_POLARITY);
2494 dev_err(&client->dev,
2495 "failed to set USB_PD property capability: %d\n", ret);
2499 i2c_set_clientdata(client, chip);
2501 spin_lock_init(&chip->irq_lock);
2502 chip->enable_irq = 1;
2504 chip->gpio_int_irq = gpiod_to_irq(chip->gpio_int);
2505 if (chip->gpio_int_irq < 0) {
2506 dev_err(&client->dev,
2507 "Unable to request IRQ for INT_N GPIO! %d\n",
2509 ret = chip->gpio_int_irq;
2513 ret = devm_request_threaded_irq(&client->dev,
2516 cc_interrupt_handler,
2517 IRQF_ONESHOT | IRQF_TRIGGER_LOW,
2521 dev_err(&client->dev, "irq request failed\n");
2525 dev_info(chip->dev, "port %d probe success\n", chip->port_num);
2530 destroy_workqueue(chip->fusb30x_wq);
2534 static int fusb30x_remove(struct i2c_client *client)
2536 struct fusb30x_chip *chip = i2c_get_clientdata(client);
2538 destroy_workqueue(chip->fusb30x_wq);
2542 static void fusb30x_shutdown(struct i2c_client *client)
2544 struct fusb30x_chip *chip = i2c_get_clientdata(client);
2546 if (chip->gpio_vbus_5v)
2547 gpiod_set_value(chip->gpio_vbus_5v, 0);
2548 if (chip->gpio_discharge) {
2549 gpiod_set_value(chip->gpio_discharge, 1);
2551 gpiod_set_value(chip->gpio_discharge, 0);
2555 static const struct of_device_id fusb30x_dt_match[] = {
2556 { .compatible = FUSB30X_I2C_DEVICETREE_NAME },
2559 MODULE_DEVICE_TABLE(of, fusb30x_dt_match);
2561 static const struct i2c_device_id fusb30x_i2c_device_id[] = {
2562 { FUSB30X_I2C_DRIVER_NAME, 0 },
2565 MODULE_DEVICE_TABLE(i2c, fusb30x_i2c_device_id);
2567 static struct i2c_driver fusb30x_driver = {
2569 .name = FUSB30X_I2C_DRIVER_NAME,
2570 .of_match_table = of_match_ptr(fusb30x_dt_match),
2572 .probe = fusb30x_probe,
2573 .remove = fusb30x_remove,
2574 .shutdown = fusb30x_shutdown,
2575 .id_table = fusb30x_i2c_device_id,
2578 module_i2c_driver(fusb30x_driver);
2580 MODULE_LICENSE("GPL");
2581 MODULE_AUTHOR("zain wang <zain.wang@rock-chips.com>");
2582 MODULE_DESCRIPTION("fusb302 typec pd driver");