Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 
0003 /*
0004  * Driver for Analogix ANX7411 USB Type-C and PD controller
0005  *
0006  * Copyright(c) 2022, Analogix Semiconductor. All rights reserved.
0007  *
0008  */
0009 #include <linux/gpio/consumer.h>
0010 #include <linux/i2c.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/iopoll.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/mutex.h>
0016 #include <linux/of_graph.h>
0017 #include <linux/of_platform.h>
0018 #include <linux/pm_runtime.h>
0019 #include <linux/regulator/consumer.h>
0020 #include <linux/slab.h>
0021 #include <linux/types.h>
0022 #include <linux/usb/pd.h>
0023 #include <linux/usb/role.h>
0024 #include <linux/usb/tcpci.h>
0025 #include <linux/usb/typec.h>
0026 #include <linux/usb/typec_dp.h>
0027 #include <linux/usb/typec_mux.h>
0028 #include <linux/workqueue.h>
0029 #include <linux/power_supply.h>
0030 
0031 #define TCPC_ADDRESS1       0x58
0032 #define TCPC_ADDRESS2       0x56
0033 #define TCPC_ADDRESS3       0x54
0034 #define TCPC_ADDRESS4       0x52
0035 #define SPI_ADDRESS1        0x7e
0036 #define SPI_ADDRESS2        0x6e
0037 #define SPI_ADDRESS3        0x64
0038 #define SPI_ADDRESS4        0x62
0039 
0040 struct anx7411_i2c_select {
0041     u8 tcpc_address;
0042     u8 spi_address;
0043 };
0044 
0045 #define VID_ANALOGIX        0x1F29
0046 #define PID_ANALOGIX        0x7411
0047 
0048 /* TCPC register define */
0049 
0050 #define ANALOG_CTRL_10      0xAA
0051 
0052 #define STATUS_LEN      2
0053 #define ALERT_0         0xCB
0054 #define RECEIVED_MSG        BIT(7)
0055 #define SOFTWARE_INT        BIT(6)
0056 #define MSG_LEN         32
0057 #define HEADER_LEN      2
0058 #define MSG_HEADER      0x00
0059 #define MSG_TYPE        0x01
0060 #define MSG_RAWDATA     0x02
0061 #define MSG_LEN_MASK        0x1F
0062 
0063 #define ALERT_1         0xCC
0064 #define INTP_POW_ON     BIT(7)
0065 #define INTP_POW_OFF        BIT(6)
0066 
0067 #define VBUS_THRESHOLD_H    0xDD
0068 #define VBUS_THRESHOLD_L    0xDE
0069 
0070 #define FW_CTRL_0       0xF0
0071 #define UNSTRUCT_VDM_EN     BIT(0)
0072 #define DELAY_200MS     BIT(1)
0073 #define VSAFE0          0
0074 #define VSAFE1          BIT(2)
0075 #define VSAFE2          BIT(3)
0076 #define VSAFE3          (BIT(2) | BIT(3))
0077 #define FRS_EN          BIT(7)
0078 
0079 #define FW_PARAM        0xF1
0080 #define DONGLE_IOP      BIT(0)
0081 
0082 #define FW_CTRL_2       0xF7
0083 #define SINK_CTRL_DIS_FLAG  BIT(5)
0084 
0085 /* SPI register define */
0086 #define OCM_CTRL_0      0x6E
0087 #define OCM_RESET       BIT(6)
0088 
0089 #define MAX_VOLTAGE     0xAC
0090 #define MAX_POWER       0xAD
0091 #define MIN_POWER       0xAE
0092 
0093 #define REQUEST_VOLTAGE     0xAF
0094 #define VOLTAGE_UNIT        100 /* mV per unit */
0095 
0096 #define REQUEST_CURRENT     0xB1
0097 #define CURRENT_UNIT        50 /* mA per unit */
0098 
0099 #define CMD_SEND_BUF        0xC0
0100 #define CMD_RECV_BUF        0xE0
0101 
0102 #define REQ_VOL_20V_IN_100MV    0xC8
0103 #define REQ_CUR_2_25A_IN_50MA   0x2D
0104 #define REQ_CUR_3_25A_IN_50MA   0x41
0105 
0106 #define DEF_5V          5000
0107 #define DEF_1_5A        1500
0108 
0109 #define LOBYTE(w)       ((u8)((w) & 0xFF))
0110 #define HIBYTE(w)       ((u8)(((u16)(w) >> 8) & 0xFF))
0111 
0112 enum anx7411_typec_message_type {
0113     TYPE_SRC_CAP = 0x00,
0114     TYPE_SNK_CAP = 0x01,
0115     TYPE_SNK_IDENTITY = 0x02,
0116     TYPE_SVID = 0x03,
0117     TYPE_SET_SNK_DP_CAP = 0x08,
0118     TYPE_PSWAP_REQ = 0x10,
0119     TYPE_DSWAP_REQ = 0x11,
0120     TYPE_VDM = 0x14,
0121     TYPE_OBJ_REQ = 0x16,
0122     TYPE_DP_ALT_ENTER = 0x19,
0123     TYPE_DP_DISCOVER_MODES_INFO = 0x27,
0124     TYPE_GET_DP_CONFIG = 0x29,
0125     TYPE_DP_CONFIGURE = 0x2A,
0126     TYPE_GET_DP_DISCOVER_MODES_INFO = 0x2E,
0127     TYPE_GET_DP_ALT_ENTER = 0x2F,
0128 };
0129 
0130 #define FW_CTRL_1       0xB2
0131 #define AUTO_PD_EN      BIT(1)
0132 #define TRYSRC_EN       BIT(2)
0133 #define TRYSNK_EN       BIT(3)
0134 #define FORCE_SEND_RDO      BIT(6)
0135 
0136 #define FW_VER          0xB4
0137 #define FW_SUBVER       0xB5
0138 
0139 #define INT_MASK        0xB6
0140 #define INT_STS         0xB7
0141 #define OCM_BOOT_UP     BIT(0)
0142 #define OC_OV_EVENT     BIT(1)
0143 #define VCONN_CHANGE        BIT(2)
0144 #define VBUS_CHANGE     BIT(3)
0145 #define CC_STATUS_CHANGE    BIT(4)
0146 #define DATA_ROLE_CHANGE    BIT(5)
0147 #define PR_CONSUMER_GOT_POWER   BIT(6)
0148 #define HPD_STATUS_CHANGE   BIT(7)
0149 
0150 #define SYSTEM_STSTUS       0xB8
0151 /* 0: SINK off; 1: SINK on */
0152 #define SINK_STATUS     BIT(1)
0153 /* 0: VCONN off; 1: VCONN on*/
0154 #define VCONN_STATUS        BIT(2)
0155 /* 0: vbus off; 1: vbus on*/
0156 #define VBUS_STATUS     BIT(3)
0157 /* 1: host; 0:device*/
0158 #define DATA_ROLE       BIT(5)
0159 /* 0: Chunking; 1: Unchunked*/
0160 #define SUPPORT_UNCHUNKING  BIT(6)
0161 /* 0: HPD low; 1: HPD high*/
0162 #define HPD_STATUS      BIT(7)
0163 
0164 #define DATA_DFP        1
0165 #define DATA_UFP        2
0166 #define POWER_SOURCE        1
0167 #define POWER_SINK      2
0168 
0169 #define CC_STATUS       0xB9
0170 #define CC1_RD          BIT(0)
0171 #define CC2_RD          BIT(4)
0172 #define CC1_RA          BIT(1)
0173 #define CC2_RA          BIT(5)
0174 #define CC1_RD          BIT(0)
0175 #define CC1_RP(cc)      (((cc) >> 2) & 0x03)
0176 #define CC2_RP(cc)      (((cc) >> 6) & 0x03)
0177 
0178 #define PD_REV_INIT     0xBA
0179 
0180 #define PD_EXT_MSG_CTRL     0xBB
0181 #define SRC_CAP_EXT_REPLY   BIT(0)
0182 #define MANUFACTURER_INFO_REPLY BIT(1)
0183 #define BATTERY_STS_REPLY   BIT(2)
0184 #define BATTERY_CAP_REPLY   BIT(3)
0185 #define ALERT_REPLY     BIT(4)
0186 #define STATUS_REPLY        BIT(5)
0187 #define PPS_STATUS_REPLY    BIT(6)
0188 #define SNK_CAP_EXT_REPLY   BIT(7)
0189 
0190 #define NO_CONNECT      0x00
0191 #define USB3_1_CONNECTED    0x01
0192 #define DP_ALT_4LANES       0x02
0193 #define USB3_1_DP_2LANES    0x03
0194 #define CC1_CONNECTED       0x01
0195 #define CC2_CONNECTED       0x02
0196 #define SELECT_PIN_ASSIGMENT_C  0x04
0197 #define SELECT_PIN_ASSIGMENT_D  0x08
0198 #define SELECT_PIN_ASSIGMENT_E  0x10
0199 #define SELECT_PIN_ASSIGMENT_U  0x00
0200 #define REDRIVER_ADDRESS    0x20
0201 #define REDRIVER_OFFSET     0x00
0202 
0203 #define DP_SVID         0xFF01
0204 #define VDM_ACK         0x40
0205 #define VDM_CMD_RES     0x00
0206 #define VDM_CMD_DIS_ID      0x01
0207 #define VDM_CMD_DIS_SVID    0x02
0208 #define VDM_CMD_DIS_MOD     0x03
0209 #define VDM_CMD_ENTER_MODE  0x04
0210 #define VDM_CMD_EXIT_MODE   0x05
0211 #define VDM_CMD_ATTENTION   0x06
0212 #define VDM_CMD_GET_STS     0x10
0213 #define VDM_CMD_AND_ACK_MASK    0x5F
0214 
0215 #define MAX_ALTMODE     2
0216 
0217 #define HAS_SOURCE_CAP      BIT(0)
0218 #define HAS_SINK_CAP        BIT(1)
0219 #define HAS_SINK_WATT       BIT(2)
0220 
0221 enum anx7411_psy_state {
0222     /* copy from drivers/usb/typec/tcpm */
0223     ANX7411_PSY_OFFLINE = 0,
0224     ANX7411_PSY_FIXED_ONLINE,
0225 
0226     /* private */
0227     /* PD keep in, but disconnct power to bq25700,
0228      * this state can be active when higher capacity adapter plug in,
0229      * and change to ONLINE state when higher capacity adapter plug out
0230      */
0231     ANX7411_PSY_HANG = 0xff,
0232 };
0233 
0234 struct typec_params {
0235     int request_current; /* ma */
0236     int request_voltage; /* mv */
0237     int cc_connect;
0238     int cc_orientation_valid;
0239     int cc_status;
0240     int data_role;
0241     int power_role;
0242     int vconn_role;
0243     int dp_altmode_enter;
0244     int cust_altmode_enter;
0245     struct usb_role_switch *role_sw;
0246     struct typec_port *port;
0247     struct typec_partner *partner;
0248     struct typec_mux_dev *typec_mux;
0249     struct typec_switch_dev *typec_switch;
0250     struct typec_altmode *amode[MAX_ALTMODE];
0251     struct typec_altmode *port_amode[MAX_ALTMODE];
0252     struct typec_displayport_data data;
0253     int pin_assignment;
0254     struct typec_capability caps;
0255     u32 src_pdo[PDO_MAX_OBJECTS];
0256     u32 sink_pdo[PDO_MAX_OBJECTS];
0257     u8 caps_flags;
0258     u8 src_pdo_nr;
0259     u8 sink_pdo_nr;
0260     u8 sink_watt;
0261     u8 sink_voltage;
0262 };
0263 
0264 #define MAX_BUF_LEN 30
0265 struct fw_msg {
0266     u8 msg_len;
0267     u8 msg_type;
0268     u8 buf[MAX_BUF_LEN];
0269 } __packed;
0270 
0271 struct anx7411_data {
0272     int fw_version;
0273     int fw_subversion;
0274     struct i2c_client *tcpc_client;
0275     struct i2c_client *spi_client;
0276     struct fw_msg send_msg;
0277     struct fw_msg recv_msg;
0278     struct gpio_desc *intp_gpiod;
0279     struct fwnode_handle *connector_fwnode;
0280     struct typec_params typec;
0281     int intp_irq;
0282     struct work_struct work;
0283     struct workqueue_struct *workqueue;
0284     /* Lock for interrupt work queue */
0285     struct mutex lock;
0286 
0287     enum anx7411_psy_state psy_online;
0288     enum power_supply_usb_type usb_type;
0289     struct power_supply *psy;
0290     struct power_supply_desc psy_desc;
0291     struct device *dev;
0292 };
0293 
0294 static u8 snk_identity[] = {
0295     LOBYTE(VID_ANALOGIX), HIBYTE(VID_ANALOGIX), 0x00, 0x82, /* snk_id_hdr */
0296     0x00, 0x00, 0x00, 0x00,                                 /* snk_cert */
0297     0x00, 0x00, LOBYTE(PID_ANALOGIX), HIBYTE(PID_ANALOGIX), /* 5snk_ama */
0298 };
0299 
0300 static u8 dp_caps[4] = {0xC6, 0x00, 0x00, 0x00};
0301 
0302 static int anx7411_reg_read(struct i2c_client *client,
0303                 u8 reg_addr)
0304 {
0305     return i2c_smbus_read_byte_data(client, reg_addr);
0306 }
0307 
0308 static int anx7411_reg_block_read(struct i2c_client *client,
0309                   u8 reg_addr, u8 len, u8 *buf)
0310 {
0311     return i2c_smbus_read_i2c_block_data(client, reg_addr, len, buf);
0312 }
0313 
0314 static int anx7411_reg_write(struct i2c_client *client,
0315                  u8 reg_addr, u8 reg_val)
0316 {
0317     return i2c_smbus_write_byte_data(client, reg_addr, reg_val);
0318 }
0319 
0320 static int anx7411_reg_block_write(struct i2c_client *client,
0321                    u8 reg_addr, u8 len, u8 *buf)
0322 {
0323     return i2c_smbus_write_i2c_block_data(client, reg_addr, len, buf);
0324 }
0325 
0326 static struct anx7411_i2c_select anx7411_i2c_addr[] = {
0327     {TCPC_ADDRESS1, SPI_ADDRESS1},
0328     {TCPC_ADDRESS2, SPI_ADDRESS2},
0329     {TCPC_ADDRESS3, SPI_ADDRESS3},
0330     {TCPC_ADDRESS4, SPI_ADDRESS4},
0331 };
0332 
0333 static int anx7411_detect_power_mode(struct anx7411_data *ctx)
0334 {
0335     int ret;
0336     int mode;
0337 
0338     ret = anx7411_reg_read(ctx->spi_client, REQUEST_CURRENT);
0339     if (ret < 0)
0340         return ret;
0341 
0342     ctx->typec.request_current = ret * CURRENT_UNIT; /* 50ma per unit */
0343 
0344     ret = anx7411_reg_read(ctx->spi_client, REQUEST_VOLTAGE);
0345     if (ret < 0)
0346         return ret;
0347 
0348     ctx->typec.request_voltage = ret * VOLTAGE_UNIT; /* 100mv per unit */
0349 
0350     if (ctx->psy_online == ANX7411_PSY_OFFLINE) {
0351         ctx->psy_online = ANX7411_PSY_FIXED_ONLINE;
0352         ctx->usb_type = POWER_SUPPLY_USB_TYPE_PD;
0353         power_supply_changed(ctx->psy);
0354     }
0355 
0356     if (!ctx->typec.cc_orientation_valid)
0357         return 0;
0358 
0359     if (ctx->typec.cc_connect == CC1_CONNECTED)
0360         mode = CC1_RP(ctx->typec.cc_status);
0361     else
0362         mode = CC2_RP(ctx->typec.cc_status);
0363     if (mode) {
0364         typec_set_pwr_opmode(ctx->typec.port, mode - 1);
0365         return 0;
0366     }
0367 
0368     typec_set_pwr_opmode(ctx->typec.port, TYPEC_PWR_MODE_PD);
0369 
0370     return 0;
0371 }
0372 
0373 static int anx7411_register_partner(struct anx7411_data *ctx,
0374                     int pd, int accessory)
0375 {
0376     struct typec_partner_desc desc;
0377     struct typec_partner *partner;
0378 
0379     if (ctx->typec.partner)
0380         return 0;
0381 
0382     desc.usb_pd = pd;
0383     desc.accessory = accessory;
0384     desc.identity = NULL;
0385     partner = typec_register_partner(ctx->typec.port, &desc);
0386     if (IS_ERR(partner))
0387         return PTR_ERR(partner);
0388 
0389     ctx->typec.partner = partner;
0390 
0391     return 0;
0392 }
0393 
0394 static int anx7411_detect_cc_orientation(struct anx7411_data *ctx)
0395 {
0396     struct device *dev = &ctx->spi_client->dev;
0397     int ret;
0398     int cc1_rd, cc2_rd;
0399     int cc1_ra, cc2_ra;
0400     int cc1_rp, cc2_rp;
0401 
0402     ret = anx7411_reg_read(ctx->spi_client, CC_STATUS);
0403     if (ret < 0)
0404         return ret;
0405 
0406     ctx->typec.cc_status = ret;
0407 
0408     cc1_rd = ret & CC1_RD ? 1 : 0;
0409     cc2_rd = ret & CC2_RD ? 1 : 0;
0410     cc1_ra = ret & CC1_RA ? 1 : 0;
0411     cc2_ra = ret & CC2_RA ? 1 : 0;
0412     cc1_rp = CC1_RP(ret);
0413     cc2_rp = CC2_RP(ret);
0414 
0415     /* Debug cable, nothing to do */
0416     if (cc1_rd && cc2_rd) {
0417         ctx->typec.cc_orientation_valid = 0;
0418         return anx7411_register_partner(ctx, 0, TYPEC_ACCESSORY_DEBUG);
0419     }
0420 
0421     if (cc1_ra && cc2_ra) {
0422         ctx->typec.cc_orientation_valid = 0;
0423         return anx7411_register_partner(ctx, 0, TYPEC_ACCESSORY_AUDIO);
0424     }
0425 
0426     ctx->typec.cc_orientation_valid = 1;
0427 
0428     ret = anx7411_register_partner(ctx, 1, TYPEC_ACCESSORY_NONE);
0429     if (ret) {
0430         dev_err(dev, "register partner\n");
0431         return ret;
0432     }
0433 
0434     if (cc1_rd || cc1_rp) {
0435         typec_set_orientation(ctx->typec.port, TYPEC_ORIENTATION_NORMAL);
0436         ctx->typec.cc_connect = CC1_CONNECTED;
0437     }
0438 
0439     if (cc2_rd || cc2_rp) {
0440         typec_set_orientation(ctx->typec.port, TYPEC_ORIENTATION_REVERSE);
0441         ctx->typec.cc_connect = CC2_CONNECTED;
0442     }
0443 
0444     return 0;
0445 }
0446 
0447 static int anx7411_set_mux(struct anx7411_data *ctx, int pin_assignment)
0448 {
0449     int mode = TYPEC_STATE_SAFE;
0450 
0451     switch (pin_assignment) {
0452     case SELECT_PIN_ASSIGMENT_U:
0453         /* default 4 line USB 3.1 */
0454         mode = TYPEC_STATE_MODAL;
0455         break;
0456     case SELECT_PIN_ASSIGMENT_C:
0457     case SELECT_PIN_ASSIGMENT_E:
0458         /* 4 line DP */
0459         mode = TYPEC_STATE_SAFE;
0460         break;
0461     case SELECT_PIN_ASSIGMENT_D:
0462         /* 2 line DP, 2 line USB */
0463         mode = TYPEC_MODE_USB3;
0464         break;
0465     default:
0466         mode = TYPEC_STATE_SAFE;
0467         break;
0468     }
0469 
0470     ctx->typec.pin_assignment = pin_assignment;
0471 
0472     return typec_set_mode(ctx->typec.port, mode);
0473 }
0474 
0475 static int anx7411_set_usb_role(struct anx7411_data *ctx, enum usb_role role)
0476 {
0477     if (!ctx->typec.role_sw)
0478         return 0;
0479 
0480     return usb_role_switch_set_role(ctx->typec.role_sw, role);
0481 }
0482 
0483 static int anx7411_data_role_detect(struct anx7411_data *ctx)
0484 {
0485     int ret;
0486 
0487     ret = anx7411_reg_read(ctx->spi_client, SYSTEM_STSTUS);
0488     if (ret < 0)
0489         return ret;
0490 
0491     ctx->typec.data_role = (ret & DATA_ROLE) ? TYPEC_HOST : TYPEC_DEVICE;
0492     ctx->typec.vconn_role = (ret & VCONN_STATUS) ? TYPEC_SOURCE : TYPEC_SINK;
0493 
0494     typec_set_data_role(ctx->typec.port, ctx->typec.data_role);
0495 
0496     typec_set_vconn_role(ctx->typec.port, ctx->typec.vconn_role);
0497 
0498     if (ctx->typec.data_role == TYPEC_HOST)
0499         return anx7411_set_usb_role(ctx, USB_ROLE_HOST);
0500 
0501     return anx7411_set_usb_role(ctx, USB_ROLE_DEVICE);
0502 }
0503 
0504 static int anx7411_power_role_detect(struct anx7411_data *ctx)
0505 {
0506     int ret;
0507 
0508     ret = anx7411_reg_read(ctx->spi_client, SYSTEM_STSTUS);
0509     if (ret < 0)
0510         return ret;
0511 
0512     ctx->typec.power_role = (ret & SINK_STATUS) ? TYPEC_SINK : TYPEC_SOURCE;
0513 
0514     if (ctx->typec.power_role == TYPEC_SOURCE) {
0515         ctx->typec.request_current = DEF_1_5A;
0516         ctx->typec.request_voltage = DEF_5V;
0517     }
0518 
0519     typec_set_pwr_role(ctx->typec.port, ctx->typec.power_role);
0520 
0521     return 0;
0522 }
0523 
0524 static int anx7411_cc_status_detect(struct anx7411_data *ctx)
0525 {
0526     anx7411_detect_cc_orientation(ctx);
0527     anx7411_detect_power_mode(ctx);
0528 
0529     return 0;
0530 }
0531 
0532 static void anx7411_partner_unregister_altmode(struct anx7411_data *ctx)
0533 {
0534     int i;
0535 
0536     ctx->typec.dp_altmode_enter = 0;
0537     ctx->typec.cust_altmode_enter = 0;
0538 
0539     for (i = 0; i < MAX_ALTMODE; i++)
0540         if (ctx->typec.amode[i]) {
0541             typec_unregister_altmode(ctx->typec.amode[i]);
0542             ctx->typec.amode[i] = NULL;
0543         }
0544 
0545     ctx->typec.pin_assignment = 0;
0546 }
0547 
0548 static int anx7411_typec_register_altmode(struct anx7411_data *ctx,
0549                       int svid, int vdo)
0550 {
0551     struct device *dev = &ctx->spi_client->dev;
0552     struct typec_altmode_desc desc;
0553     int err;
0554     int i;
0555 
0556     desc.svid = svid;
0557     desc.vdo = vdo;
0558 
0559     for (i = 0; i < MAX_ALTMODE; i++)
0560         if (!ctx->typec.amode[i])
0561             break;
0562 
0563     desc.mode = i + 1; /* start with 1 */
0564 
0565     if (i >= MAX_ALTMODE) {
0566         dev_err(dev, "no altmode space for registering\n");
0567         return -ENOMEM;
0568     }
0569 
0570     ctx->typec.amode[i] = typec_partner_register_altmode(ctx->typec.partner,
0571                                  &desc);
0572     if (IS_ERR(ctx->typec.amode[i])) {
0573         dev_err(dev, "failed to register altmode\n");
0574         err = PTR_ERR(ctx->typec.amode[i]);
0575         ctx->typec.amode[i] = NULL;
0576         return err;
0577     }
0578 
0579     return 0;
0580 }
0581 
0582 static void anx7411_unregister_partner(struct anx7411_data *ctx)
0583 {
0584     if (ctx->typec.partner) {
0585         typec_unregister_partner(ctx->typec.partner);
0586         ctx->typec.partner = NULL;
0587     }
0588 }
0589 
0590 static int anx7411_update_altmode(struct anx7411_data *ctx, int svid)
0591 {
0592     int i;
0593 
0594     if (svid == DP_SVID)
0595         ctx->typec.dp_altmode_enter = 1;
0596     else
0597         ctx->typec.cust_altmode_enter = 1;
0598 
0599     for (i = 0; i < MAX_ALTMODE; i++) {
0600         if (!ctx->typec.amode[i])
0601             continue;
0602 
0603         if (ctx->typec.amode[i]->svid == svid) {
0604             typec_altmode_update_active(ctx->typec.amode[i], true);
0605             typec_altmode_notify(ctx->typec.amode[i],
0606                          ctx->typec.pin_assignment,
0607                          &ctx->typec.data);
0608             break;
0609         }
0610     }
0611 
0612     return 0;
0613 }
0614 
0615 static int anx7411_register_altmode(struct anx7411_data *ctx,
0616                     bool dp_altmode, u8 *buf)
0617 {
0618     int ret;
0619     int svid;
0620     int mid;
0621 
0622     if (!ctx->typec.partner)
0623         return 0;
0624 
0625     svid = DP_SVID;
0626     if (dp_altmode) {
0627         mid = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
0628 
0629         return anx7411_typec_register_altmode(ctx, svid, mid);
0630     }
0631 
0632     svid = (buf[3] << 8) | buf[2];
0633     if ((buf[0] & VDM_CMD_AND_ACK_MASK) != (VDM_ACK | VDM_CMD_ENTER_MODE))
0634         return anx7411_update_altmode(ctx, svid);
0635 
0636     if ((buf[0] & VDM_CMD_AND_ACK_MASK) != (VDM_ACK | VDM_CMD_DIS_MOD))
0637         return 0;
0638 
0639     mid = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24);
0640 
0641     ret = anx7411_typec_register_altmode(ctx, svid, mid);
0642     if (ctx->typec.cust_altmode_enter)
0643         ret |= anx7411_update_altmode(ctx, svid);
0644 
0645     return ret;
0646 }
0647 
0648 static int anx7411_parse_cmd(struct anx7411_data *ctx, u8 type, u8 *buf, u8 len)
0649 {
0650     struct device *dev = &ctx->spi_client->dev;
0651     u8 cur_50ma, vol_100mv;
0652 
0653     switch (type) {
0654     case TYPE_SRC_CAP:
0655         cur_50ma = anx7411_reg_read(ctx->spi_client, REQUEST_CURRENT);
0656         vol_100mv = anx7411_reg_read(ctx->spi_client, REQUEST_VOLTAGE);
0657 
0658         ctx->typec.request_voltage = vol_100mv * VOLTAGE_UNIT;
0659         ctx->typec.request_current = cur_50ma * CURRENT_UNIT;
0660 
0661         ctx->psy_online = ANX7411_PSY_FIXED_ONLINE;
0662         ctx->usb_type = POWER_SUPPLY_USB_TYPE_PD;
0663         power_supply_changed(ctx->psy);
0664         break;
0665     case TYPE_SNK_CAP:
0666         break;
0667     case TYPE_SVID:
0668         break;
0669     case TYPE_SNK_IDENTITY:
0670         break;
0671     case TYPE_GET_DP_ALT_ENTER:
0672         /* DP alt mode enter success */
0673         if (buf[0])
0674             anx7411_update_altmode(ctx, DP_SVID);
0675         break;
0676     case TYPE_DP_ALT_ENTER:
0677         /* Update DP altmode */
0678         anx7411_update_altmode(ctx, DP_SVID);
0679         break;
0680     case TYPE_OBJ_REQ:
0681         anx7411_detect_power_mode(ctx);
0682         break;
0683     case TYPE_DP_CONFIGURE:
0684         anx7411_set_mux(ctx, buf[1]);
0685         break;
0686     case TYPE_DP_DISCOVER_MODES_INFO:
0687         /* Make sure discover modes valid */
0688         if (buf[0] | buf[1])
0689             /* Register DP Altmode */
0690             anx7411_register_altmode(ctx, 1, buf);
0691         break;
0692     case TYPE_VDM:
0693         /* Register other altmode */
0694         anx7411_register_altmode(ctx, 0, buf);
0695         break;
0696     default:
0697         dev_err(dev, "ignore message(0x%.02x).\n", type);
0698         break;
0699     }
0700 
0701     return 0;
0702 }
0703 
0704 static u8 checksum(struct device *dev, u8 *buf, u8 len)
0705 {
0706     u8 ret = 0;
0707     u8 i;
0708 
0709     for (i = 0; i < len; i++)
0710         ret += buf[i];
0711 
0712     return ret;
0713 }
0714 
0715 static int anx7411_read_msg_ctrl_status(struct i2c_client *client)
0716 {
0717     return anx7411_reg_read(client, CMD_SEND_BUF);
0718 }
0719 
0720 static int anx7411_wait_msg_empty(struct i2c_client *client)
0721 {
0722     int val;
0723 
0724     return readx_poll_timeout(anx7411_read_msg_ctrl_status,
0725                   client, val, (val < 0) || (val == 0),
0726                   2000, 2000 * 150);
0727 }
0728 
0729 static int anx7411_send_msg(struct anx7411_data *ctx, u8 type, u8 *buf, u8 size)
0730 {
0731     struct device *dev = &ctx->spi_client->dev;
0732     struct fw_msg *msg = &ctx->send_msg;
0733     u8 crc;
0734     int ret;
0735 
0736     size = min_t(u8, size, (u8)MAX_BUF_LEN);
0737     memcpy(msg->buf, buf, size);
0738     msg->msg_type = type;
0739     /* msg len equals buffer length + msg_type */
0740     msg->msg_len = size + 1;
0741 
0742     /* Do CRC check for all buffer data and msg_len and msg_type */
0743     crc = checksum(dev, (u8 *)msg, size + HEADER_LEN);
0744     msg->buf[size] = 0 - crc;
0745 
0746     ret = anx7411_wait_msg_empty(ctx->spi_client);
0747     if (ret)
0748         return ret;
0749 
0750     ret = anx7411_reg_block_write(ctx->spi_client,
0751                       CMD_SEND_BUF + 1, size + HEADER_LEN,
0752                       &msg->msg_type);
0753     ret |= anx7411_reg_write(ctx->spi_client, CMD_SEND_BUF,
0754                  msg->msg_len);
0755     return ret;
0756 }
0757 
0758 static int anx7411_process_cmd(struct anx7411_data *ctx)
0759 {
0760     struct device *dev = &ctx->spi_client->dev;
0761     struct fw_msg *msg = &ctx->recv_msg;
0762     u8 len;
0763     u8 crc;
0764     int ret;
0765 
0766     /* Read message from firmware */
0767     ret = anx7411_reg_block_read(ctx->spi_client, CMD_RECV_BUF,
0768                      MSG_LEN, (u8 *)msg);
0769     if (ret < 0)
0770         return 0;
0771 
0772     if (!msg->msg_len)
0773         return 0;
0774 
0775     ret = anx7411_reg_write(ctx->spi_client, CMD_RECV_BUF, 0);
0776     if (ret)
0777         return ret;
0778 
0779     len = msg->msg_len & MSG_LEN_MASK;
0780     crc = checksum(dev, (u8 *)msg, len + HEADER_LEN);
0781     if (crc) {
0782         dev_err(dev, "message error crc(0x%.02x)\n", crc);
0783         return -ERANGE;
0784     }
0785 
0786     return anx7411_parse_cmd(ctx, msg->msg_type, msg->buf, len - 1);
0787 }
0788 
0789 static void anx7411_translate_payload(struct device *dev, __le32 *payload,
0790                       u32 *pdo, int nr, const char *type)
0791 {
0792     int i;
0793 
0794     if (nr > PDO_MAX_OBJECTS) {
0795         dev_err(dev, "nr(%d) exceed PDO_MAX_OBJECTS(%d)\n",
0796             nr, PDO_MAX_OBJECTS);
0797 
0798         return;
0799     }
0800 
0801     for (i = 0; i < nr; i++)
0802         payload[i] = cpu_to_le32(pdo[i]);
0803 }
0804 
0805 static int anx7411_config(struct anx7411_data *ctx)
0806 {
0807     struct device *dev = &ctx->spi_client->dev;
0808     struct typec_params *typecp = &ctx->typec;
0809     __le32 payload[PDO_MAX_OBJECTS];
0810     int ret;
0811 
0812     /* Config PD FW work under PD 2.0 */
0813     ret = anx7411_reg_write(ctx->spi_client, PD_REV_INIT, PD_REV20);
0814     ret |= anx7411_reg_write(ctx->tcpc_client, FW_CTRL_0,
0815                  UNSTRUCT_VDM_EN | DELAY_200MS |
0816                  VSAFE1 | FRS_EN);
0817     ret |= anx7411_reg_write(ctx->spi_client, FW_CTRL_1,
0818                  AUTO_PD_EN | FORCE_SEND_RDO);
0819 
0820     /* Set VBUS current threshold */
0821     ret |= anx7411_reg_write(ctx->tcpc_client, VBUS_THRESHOLD_H, 0xff);
0822     ret |= anx7411_reg_write(ctx->tcpc_client, VBUS_THRESHOLD_L, 0x03);
0823 
0824     /* Fix dongle compatible issue */
0825     ret |= anx7411_reg_write(ctx->tcpc_client, FW_PARAM,
0826                  anx7411_reg_read(ctx->tcpc_client, FW_PARAM) |
0827                  DONGLE_IOP);
0828     ret |= anx7411_reg_write(ctx->spi_client, INT_MASK, 0);
0829 
0830     ret |= anx7411_reg_write(ctx->spi_client, PD_EXT_MSG_CTRL, 0xFF);
0831     if (ret)
0832         return ret;
0833 
0834     if (typecp->caps_flags & HAS_SOURCE_CAP) {
0835         anx7411_translate_payload(dev, payload, typecp->src_pdo,
0836                       typecp->src_pdo_nr, "source");
0837         anx7411_send_msg(ctx, TYPE_SRC_CAP, (u8 *)&payload,
0838                  typecp->src_pdo_nr * 4);
0839         anx7411_send_msg(ctx, TYPE_SNK_IDENTITY, snk_identity,
0840                  sizeof(snk_identity));
0841         anx7411_send_msg(ctx, TYPE_SET_SNK_DP_CAP, dp_caps,
0842                  sizeof(dp_caps));
0843     }
0844 
0845     if (typecp->caps_flags & HAS_SINK_CAP) {
0846         anx7411_translate_payload(dev, payload, typecp->sink_pdo,
0847                       typecp->sink_pdo_nr, "sink");
0848         anx7411_send_msg(ctx, TYPE_SNK_CAP, (u8 *)&payload,
0849                  typecp->sink_pdo_nr * 4);
0850     }
0851 
0852     if (typecp->caps_flags & HAS_SINK_WATT) {
0853         if (typecp->sink_watt) {
0854             ret |= anx7411_reg_write(ctx->spi_client, MAX_POWER,
0855                          typecp->sink_watt);
0856             /* Set min power to 1W */
0857             ret |= anx7411_reg_write(ctx->spi_client, MIN_POWER, 2);
0858         }
0859 
0860         if (typecp->sink_voltage)
0861             ret |= anx7411_reg_write(ctx->spi_client, MAX_VOLTAGE,
0862                       typecp->sink_voltage);
0863         if (ret)
0864             return ret;
0865     }
0866 
0867     if (!typecp->caps_flags)
0868         usleep_range(5000, 6000);
0869 
0870     ctx->fw_version = anx7411_reg_read(ctx->spi_client, FW_VER);
0871     ctx->fw_subversion = anx7411_reg_read(ctx->spi_client, FW_SUBVER);
0872 
0873     return 0;
0874 }
0875 
0876 static void anx7411_chip_standby(struct anx7411_data *ctx)
0877 {
0878     int ret;
0879     u8 cc1, cc2;
0880     struct device *dev = &ctx->spi_client->dev;
0881 
0882     ret = anx7411_reg_write(ctx->spi_client, OCM_CTRL_0,
0883                 anx7411_reg_read(ctx->spi_client, OCM_CTRL_0) |
0884                 OCM_RESET);
0885     ret |= anx7411_reg_write(ctx->tcpc_client, ANALOG_CTRL_10, 0x80);
0886     /* Set TCPC to RD and DRP enable */
0887     cc1 = TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT;
0888     cc2 = TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT;
0889     ret |= anx7411_reg_write(ctx->tcpc_client, TCPC_ROLE_CTRL,
0890                  TCPC_ROLE_CTRL_DRP | cc1 | cc2);
0891 
0892     /* Send DRP toggle command */
0893     ret |= anx7411_reg_write(ctx->tcpc_client, TCPC_COMMAND,
0894                  TCPC_CMD_LOOK4CONNECTION);
0895 
0896     /* Send TCPC enter standby command */
0897     ret |= anx7411_reg_write(ctx->tcpc_client,
0898                  TCPC_COMMAND, TCPC_CMD_I2C_IDLE);
0899     if (ret)
0900         dev_err(dev, "Chip standby failed\n");
0901 }
0902 
0903 static void anx7411_work_func(struct work_struct *work)
0904 {
0905     int ret;
0906     u8 buf[STATUS_LEN];
0907     u8 int_change; /* Interrupt change */
0908     u8 int_status; /* Firmware status update */
0909     u8 alert0, alert1; /* Interrupt alert source */
0910     struct anx7411_data *ctx = container_of(work, struct anx7411_data, work);
0911     struct device *dev = &ctx->spi_client->dev;
0912 
0913     mutex_lock(&ctx->lock);
0914 
0915     /* Read interrupt change status */
0916     ret = anx7411_reg_block_read(ctx->spi_client, INT_STS, STATUS_LEN, buf);
0917     if (ret < 0) {
0918         /* Power standby mode, just return */
0919         goto unlock;
0920     }
0921     int_change = buf[0];
0922     int_status = buf[1];
0923 
0924     /* Read alert register */
0925     ret = anx7411_reg_block_read(ctx->tcpc_client, ALERT_0, STATUS_LEN, buf);
0926     if (ret < 0)
0927         goto unlock;
0928 
0929     alert0 = buf[0];
0930     alert1 = buf[1];
0931 
0932     /* Clear interrupt and alert status */
0933     ret = anx7411_reg_write(ctx->spi_client, INT_STS, 0);
0934     ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_0, alert0);
0935     ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_1, alert1);
0936     if (ret)
0937         goto unlock;
0938 
0939     if (alert1 & INTP_POW_OFF) {
0940         anx7411_partner_unregister_altmode(ctx);
0941         if (anx7411_set_usb_role(ctx, USB_ROLE_NONE))
0942             dev_err(dev, "Set usb role\n");
0943         anx7411_unregister_partner(ctx);
0944         ctx->psy_online = ANX7411_PSY_OFFLINE;
0945         ctx->usb_type = POWER_SUPPLY_USB_TYPE_C;
0946         ctx->typec.request_voltage = 0;
0947         ctx->typec.request_current = 0;
0948         power_supply_changed(ctx->psy);
0949         anx7411_chip_standby(ctx);
0950         goto unlock;
0951     }
0952 
0953     if ((alert0 & SOFTWARE_INT) && (int_change & OCM_BOOT_UP)) {
0954         if (anx7411_config(ctx))
0955             dev_err(dev, "Config failed\n");
0956         if (anx7411_data_role_detect(ctx))
0957             dev_err(dev, "set PD data role\n");
0958         if (anx7411_power_role_detect(ctx))
0959             dev_err(dev, "set PD power role\n");
0960         anx7411_set_mux(ctx, SELECT_PIN_ASSIGMENT_C);
0961     }
0962 
0963     if (alert0 & RECEIVED_MSG)
0964         anx7411_process_cmd(ctx);
0965 
0966     ret = (int_status & DATA_ROLE) ? TYPEC_HOST : TYPEC_DEVICE;
0967     if (ctx->typec.data_role != ret)
0968         if (anx7411_data_role_detect(ctx))
0969             dev_err(dev, "set PD data role\n");
0970 
0971     ret = (int_status & SINK_STATUS) ? TYPEC_SINK : TYPEC_SOURCE;
0972     if (ctx->typec.power_role != ret)
0973         if (anx7411_power_role_detect(ctx))
0974             dev_err(dev, "set PD power role\n");
0975 
0976     if ((alert0 & SOFTWARE_INT) && (int_change & CC_STATUS_CHANGE))
0977         anx7411_cc_status_detect(ctx);
0978 
0979 unlock:
0980     mutex_unlock(&ctx->lock);
0981 }
0982 
0983 static irqreturn_t anx7411_intr_isr(int irq, void *data)
0984 {
0985     struct anx7411_data *ctx = (struct anx7411_data *)data;
0986 
0987     queue_work(ctx->workqueue, &ctx->work);
0988 
0989     return IRQ_HANDLED;
0990 }
0991 
0992 static int anx7411_register_i2c_dummy_clients(struct anx7411_data *ctx,
0993                           struct i2c_client *client)
0994 {
0995     int i;
0996     u8 spi_addr;
0997 
0998     for (i = 0; i < ARRAY_SIZE(anx7411_i2c_addr); i++) {
0999         if (client->addr == (anx7411_i2c_addr[i].tcpc_address >> 1)) {
1000             spi_addr = anx7411_i2c_addr[i].spi_address >> 1;
1001             ctx->spi_client = i2c_new_dummy_device(client->adapter,
1002                                    spi_addr);
1003             if (!IS_ERR(ctx->spi_client))
1004                 return 0;
1005         }
1006     }
1007 
1008     dev_err(&client->dev, "unable to get SPI slave\n");
1009     return -ENOMEM;
1010 }
1011 
1012 static void anx7411_port_unregister_altmodes(struct typec_altmode **adev)
1013 {
1014     int i;
1015 
1016     for (i = 0; i < MAX_ALTMODE; i++)
1017         if (adev[i]) {
1018             typec_unregister_altmode(adev[i]);
1019             adev[i] = NULL;
1020         }
1021 }
1022 
1023 static int anx7411_usb_mux_set(struct typec_mux_dev *mux,
1024                    struct typec_mux_state *state)
1025 {
1026     struct anx7411_data *ctx = typec_mux_get_drvdata(mux);
1027     struct device *dev = &ctx->spi_client->dev;
1028     int has_dp;
1029 
1030     has_dp = (state->alt && state->alt->svid == USB_TYPEC_DP_SID &&
1031           state->alt->mode == USB_TYPEC_DP_MODE);
1032     if (!has_dp)
1033         dev_err(dev, "dp altmode not register\n");
1034 
1035     return 0;
1036 }
1037 
1038 static int anx7411_usb_set_orientation(struct typec_switch_dev *sw,
1039                        enum typec_orientation orientation)
1040 {
1041     /* No need set */
1042 
1043     return 0;
1044 }
1045 
1046 static int anx7411_register_switch(struct anx7411_data *ctx,
1047                    struct device *dev,
1048                    struct fwnode_handle *fwnode)
1049 {
1050     struct typec_switch_desc sw_desc = { };
1051 
1052     sw_desc.fwnode = fwnode;
1053     sw_desc.drvdata = ctx;
1054     sw_desc.name = fwnode_get_name(fwnode);
1055     sw_desc.set = anx7411_usb_set_orientation;
1056 
1057     ctx->typec.typec_switch = typec_switch_register(dev, &sw_desc);
1058     if (IS_ERR(ctx->typec.typec_switch)) {
1059         dev_err(dev, "switch register failed\n");
1060         return PTR_ERR(ctx->typec.typec_switch);
1061     }
1062 
1063     return 0;
1064 }
1065 
1066 static int anx7411_register_mux(struct anx7411_data *ctx,
1067                 struct device *dev,
1068                 struct fwnode_handle *fwnode)
1069 {
1070     struct typec_mux_desc mux_desc = { };
1071 
1072     mux_desc.fwnode = fwnode;
1073     mux_desc.drvdata = ctx;
1074     mux_desc.name = fwnode_get_name(fwnode);
1075     mux_desc.set = anx7411_usb_mux_set;
1076 
1077     ctx->typec.typec_mux = typec_mux_register(dev, &mux_desc);
1078     if (IS_ERR(ctx->typec.typec_mux)) {
1079         dev_err(dev, "mux register failed\n");
1080         return PTR_ERR(ctx->typec.typec_mux);
1081     }
1082 
1083     return 0;
1084 }
1085 
1086 static void anx7411_unregister_mux(struct anx7411_data *ctx)
1087 {
1088     if (ctx->typec.typec_mux) {
1089         typec_mux_unregister(ctx->typec.typec_mux);
1090         ctx->typec.typec_mux = NULL;
1091     }
1092 }
1093 
1094 static void anx7411_unregister_switch(struct anx7411_data *ctx)
1095 {
1096     if (ctx->typec.typec_switch) {
1097         typec_switch_unregister(ctx->typec.typec_switch);
1098         ctx->typec.typec_switch = NULL;
1099     }
1100 }
1101 
1102 static int anx7411_typec_switch_probe(struct anx7411_data *ctx,
1103                       struct device *dev)
1104 {
1105     int ret;
1106     struct device_node *node;
1107 
1108     node = of_find_node_by_name(dev->of_node, "orientation_switch");
1109     if (!node)
1110         return 0;
1111 
1112     ret = anx7411_register_switch(ctx, dev, &node->fwnode);
1113     if (ret) {
1114         dev_err(dev, "failed register switch");
1115         return ret;
1116     }
1117 
1118     node = of_find_node_by_name(dev->of_node, "mode_switch");
1119     if (!node) {
1120         dev_err(dev, "no typec mux exist");
1121         ret = -ENODEV;
1122         goto unregister_switch;
1123     }
1124 
1125     ret = anx7411_register_mux(ctx, dev, &node->fwnode);
1126     if (ret) {
1127         dev_err(dev, "failed register mode switch");
1128         ret = -ENODEV;
1129         goto unregister_switch;
1130     }
1131 
1132     return 0;
1133 
1134 unregister_switch:
1135     anx7411_unregister_switch(ctx);
1136 
1137     return ret;
1138 }
1139 
1140 static int anx7411_typec_port_probe(struct anx7411_data *ctx,
1141                     struct device *dev)
1142 {
1143     struct typec_capability *cap = &ctx->typec.caps;
1144     struct typec_params *typecp = &ctx->typec;
1145     struct fwnode_handle *fwnode;
1146     const char *buf;
1147     int ret, i;
1148 
1149     fwnode = device_get_named_child_node(dev, "connector");
1150     if (!fwnode)
1151         return -EINVAL;
1152 
1153     ret = fwnode_property_read_string(fwnode, "power-role", &buf);
1154     if (ret) {
1155         dev_err(dev, "power-role not found: %d\n", ret);
1156         return ret;
1157     }
1158 
1159     ret = typec_find_port_power_role(buf);
1160     if (ret < 0)
1161         return ret;
1162     cap->type = ret;
1163 
1164     ret = fwnode_property_read_string(fwnode, "data-role", &buf);
1165     if (ret) {
1166         dev_err(dev, "data-role not found: %d\n", ret);
1167         return ret;
1168     }
1169 
1170     ret = typec_find_port_data_role(buf);
1171     if (ret < 0)
1172         return ret;
1173     cap->data = ret;
1174 
1175     ret = fwnode_property_read_string(fwnode, "try-power-role", &buf);
1176     if (ret) {
1177         dev_err(dev, "try-power-role not found: %d\n", ret);
1178         return ret;
1179     }
1180 
1181     ret = typec_find_power_role(buf);
1182     if (ret < 0)
1183         return ret;
1184     cap->prefer_role = ret;
1185 
1186     /* Get source pdos */
1187     ret = fwnode_property_count_u32(fwnode, "source-pdos");
1188     if (ret > 0) {
1189         typecp->src_pdo_nr = min_t(u8, ret, PDO_MAX_OBJECTS);
1190         ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
1191                              typecp->src_pdo,
1192                              typecp->src_pdo_nr);
1193         if (ret < 0) {
1194             dev_err(dev, "source cap validate failed: %d\n", ret);
1195             return -EINVAL;
1196         }
1197 
1198         typecp->caps_flags |= HAS_SOURCE_CAP;
1199     }
1200 
1201     ret = fwnode_property_count_u32(fwnode, "sink-pdos");
1202     if (ret > 0) {
1203         typecp->sink_pdo_nr = min_t(u8, ret, PDO_MAX_OBJECTS);
1204         ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
1205                              typecp->sink_pdo,
1206                              typecp->sink_pdo_nr);
1207         if (ret < 0) {
1208             dev_err(dev, "sink cap validate failed: %d\n", ret);
1209             return -EINVAL;
1210         }
1211 
1212         for (i = 0; i < typecp->sink_pdo_nr; i++) {
1213             ret = 0;
1214             switch (pdo_type(typecp->sink_pdo[i])) {
1215             case PDO_TYPE_FIXED:
1216                 ret = pdo_fixed_voltage(typecp->sink_pdo[i]);
1217                 break;
1218             case PDO_TYPE_BATT:
1219             case PDO_TYPE_VAR:
1220                 ret = pdo_max_voltage(typecp->sink_pdo[i]);
1221                 break;
1222             case PDO_TYPE_APDO:
1223             default:
1224                 ret = 0;
1225                 break;
1226             }
1227 
1228             /* 100mv per unit */
1229             typecp->sink_voltage = max(5000, ret) / 100;
1230         }
1231 
1232         typecp->caps_flags |= HAS_SINK_CAP;
1233     }
1234 
1235     if (!fwnode_property_read_u32(fwnode, "op-sink-microwatt", &ret)) {
1236         typecp->sink_watt = ret / 500000; /* 500mw per unit */
1237         typecp->caps_flags |= HAS_SINK_WATT;
1238     }
1239 
1240     cap->fwnode = fwnode;
1241 
1242     ctx->typec.role_sw = usb_role_switch_get(dev);
1243     if (IS_ERR(ctx->typec.role_sw)) {
1244         dev_err(dev, "USB role switch not found.\n");
1245         ctx->typec.role_sw = NULL;
1246     }
1247 
1248     ctx->typec.port = typec_register_port(dev, cap);
1249     if (IS_ERR(ctx->typec.port)) {
1250         ret = PTR_ERR(ctx->typec.port);
1251         ctx->typec.port = NULL;
1252         dev_err(dev, "Failed to register type c port %d\n", ret);
1253         return ret;
1254     }
1255 
1256     typec_port_register_altmodes(ctx->typec.port, NULL, ctx,
1257                      ctx->typec.port_amode,
1258                      MAX_ALTMODE);
1259     return 0;
1260 }
1261 
1262 static int anx7411_typec_check_connection(struct anx7411_data *ctx)
1263 {
1264     int ret;
1265 
1266     ret = anx7411_reg_read(ctx->spi_client, FW_VER);
1267     if (ret < 0)
1268         return 0; /* No device attached in typec port */
1269 
1270     /* Clear interrupt and alert status */
1271     ret = anx7411_reg_write(ctx->spi_client, INT_STS, 0);
1272     ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_0, 0xFF);
1273     ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_1, 0xFF);
1274     if (ret)
1275         return ret;
1276 
1277     ret = anx7411_cc_status_detect(ctx);
1278     ret |= anx7411_power_role_detect(ctx);
1279     ret |= anx7411_data_role_detect(ctx);
1280     ret |= anx7411_set_mux(ctx, SELECT_PIN_ASSIGMENT_C);
1281     if (ret)
1282         return ret;
1283 
1284     ret = anx7411_send_msg(ctx, TYPE_GET_DP_ALT_ENTER, NULL, 0);
1285     ret |= anx7411_send_msg(ctx, TYPE_GET_DP_DISCOVER_MODES_INFO, NULL, 0);
1286 
1287     return ret;
1288 }
1289 
1290 static int __maybe_unused anx7411_runtime_pm_suspend(struct device *dev)
1291 {
1292     struct anx7411_data *ctx = dev_get_drvdata(dev);
1293 
1294     mutex_lock(&ctx->lock);
1295 
1296     anx7411_partner_unregister_altmode(ctx);
1297 
1298     if (ctx->typec.partner)
1299         anx7411_unregister_partner(ctx);
1300 
1301     mutex_unlock(&ctx->lock);
1302 
1303     return 0;
1304 }
1305 
1306 static int __maybe_unused anx7411_runtime_pm_resume(struct device *dev)
1307 {
1308     struct anx7411_data *ctx = dev_get_drvdata(dev);
1309 
1310     mutex_lock(&ctx->lock);
1311     /* Detect PD connection */
1312     if (anx7411_typec_check_connection(ctx))
1313         dev_err(dev, "check connection");
1314 
1315     mutex_unlock(&ctx->lock);
1316 
1317     return 0;
1318 }
1319 
1320 static const struct dev_pm_ops anx7411_pm_ops = {
1321     SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1322                 pm_runtime_force_resume)
1323     SET_RUNTIME_PM_OPS(anx7411_runtime_pm_suspend,
1324                anx7411_runtime_pm_resume, NULL)
1325 };
1326 
1327 static void anx7411_get_gpio_irq(struct anx7411_data *ctx)
1328 {
1329     struct device *dev = &ctx->tcpc_client->dev;
1330 
1331     ctx->intp_gpiod = devm_gpiod_get_optional(dev, "interrupt", GPIOD_IN);
1332     if (IS_ERR_OR_NULL(ctx->intp_gpiod)) {
1333         dev_err(dev, "no interrupt gpio property\n");
1334         return;
1335     }
1336 
1337     ctx->intp_irq = gpiod_to_irq(ctx->intp_gpiod);
1338     if (ctx->intp_irq < 0)
1339         dev_err(dev, "failed to get GPIO IRQ\n");
1340 }
1341 
1342 static enum power_supply_usb_type anx7411_psy_usb_types[] = {
1343     POWER_SUPPLY_USB_TYPE_C,
1344     POWER_SUPPLY_USB_TYPE_PD,
1345     POWER_SUPPLY_USB_TYPE_PD_PPS,
1346 };
1347 
1348 static enum power_supply_property anx7411_psy_props[] = {
1349     POWER_SUPPLY_PROP_USB_TYPE,
1350     POWER_SUPPLY_PROP_ONLINE,
1351     POWER_SUPPLY_PROP_VOLTAGE_MIN,
1352     POWER_SUPPLY_PROP_VOLTAGE_MAX,
1353     POWER_SUPPLY_PROP_VOLTAGE_NOW,
1354     POWER_SUPPLY_PROP_CURRENT_MAX,
1355     POWER_SUPPLY_PROP_CURRENT_NOW,
1356 };
1357 
1358 static int anx7411_psy_set_prop(struct power_supply *psy,
1359                 enum power_supply_property psp,
1360                 const union power_supply_propval *val)
1361 {
1362     struct anx7411_data *ctx = power_supply_get_drvdata(psy);
1363     int ret = 0;
1364 
1365     if (psp == POWER_SUPPLY_PROP_ONLINE)
1366         ctx->psy_online = val->intval;
1367     else
1368         ret = -EINVAL;
1369 
1370     power_supply_changed(ctx->psy);
1371     return ret;
1372 }
1373 
1374 static int anx7411_psy_prop_writeable(struct power_supply *psy,
1375                       enum power_supply_property psp)
1376 {
1377     return psp == POWER_SUPPLY_PROP_ONLINE;
1378 }
1379 
1380 static int anx7411_psy_get_prop(struct power_supply *psy,
1381                 enum power_supply_property psp,
1382                 union power_supply_propval *val)
1383 {
1384     struct anx7411_data *ctx = power_supply_get_drvdata(psy);
1385     int ret = 0;
1386 
1387     switch (psp) {
1388     case POWER_SUPPLY_PROP_USB_TYPE:
1389         val->intval = ctx->usb_type;
1390         break;
1391     case POWER_SUPPLY_PROP_ONLINE:
1392         val->intval = ctx->psy_online;
1393         break;
1394     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1395     case POWER_SUPPLY_PROP_VOLTAGE_MIN:
1396     case POWER_SUPPLY_PROP_VOLTAGE_MAX:
1397         val->intval = (ctx->psy_online) ?
1398             ctx->typec.request_voltage * 1000 : 0;
1399         break;
1400     case POWER_SUPPLY_PROP_CURRENT_NOW:
1401     case POWER_SUPPLY_PROP_CURRENT_MAX:
1402         val->intval = (ctx->psy_online) ?
1403             ctx->typec.request_current * 1000 : 0;
1404         break;
1405     default:
1406         ret = -EINVAL;
1407         break;
1408     }
1409     return ret;
1410 }
1411 
1412 static int anx7411_psy_register(struct anx7411_data *ctx)
1413 {
1414     struct power_supply_desc *psy_desc = &ctx->psy_desc;
1415     struct power_supply_config psy_cfg = {};
1416     char *psy_name;
1417 
1418     psy_name = devm_kasprintf(ctx->dev, GFP_KERNEL, "anx7411-source-psy-%s",
1419                   dev_name(ctx->dev));
1420     if (!psy_name)
1421         return -ENOMEM;
1422 
1423     psy_desc->name = psy_name;
1424     psy_desc->type = POWER_SUPPLY_TYPE_USB;
1425     psy_desc->usb_types = anx7411_psy_usb_types;
1426     psy_desc->num_usb_types = ARRAY_SIZE(anx7411_psy_usb_types);
1427     psy_desc->properties = anx7411_psy_props;
1428     psy_desc->num_properties = ARRAY_SIZE(anx7411_psy_props);
1429 
1430     psy_desc->get_property = anx7411_psy_get_prop;
1431     psy_desc->set_property = anx7411_psy_set_prop;
1432     psy_desc->property_is_writeable = anx7411_psy_prop_writeable;
1433 
1434     ctx->usb_type = POWER_SUPPLY_USB_TYPE_C;
1435     ctx->psy = devm_power_supply_register(ctx->dev, psy_desc, &psy_cfg);
1436 
1437     if (IS_ERR(ctx->psy))
1438         dev_warn(ctx->dev, "unable to register psy\n");
1439 
1440     return PTR_ERR_OR_ZERO(ctx->psy);
1441 }
1442 
1443 static int anx7411_i2c_probe(struct i2c_client *client,
1444                  const struct i2c_device_id *id)
1445 {
1446     struct anx7411_data *plat;
1447     struct device *dev = &client->dev;
1448     int ret;
1449 
1450     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
1451         return -ENODEV;
1452 
1453     plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
1454     if (!plat)
1455         return -ENOMEM;
1456 
1457     plat->tcpc_client = client;
1458     i2c_set_clientdata(client, plat);
1459 
1460     mutex_init(&plat->lock);
1461 
1462     ret = anx7411_register_i2c_dummy_clients(plat, client);
1463     if (ret) {
1464         dev_err(dev, "fail to reserve I2C bus\n");
1465         return ret;
1466     }
1467 
1468     ret = anx7411_typec_switch_probe(plat, dev);
1469     if (ret) {
1470         dev_err(dev, "fail to probe typec switch\n");
1471         goto free_i2c_dummy;
1472     }
1473 
1474     ret = anx7411_typec_port_probe(plat, dev);
1475     if (ret) {
1476         dev_err(dev, "fail to probe typec property.\n");
1477         ret = -ENODEV;
1478         goto free_typec_switch;
1479     }
1480 
1481     plat->intp_irq = client->irq;
1482     if (!client->irq)
1483         anx7411_get_gpio_irq(plat);
1484 
1485     if (!plat->intp_irq) {
1486         dev_err(dev, "fail to get interrupt IRQ\n");
1487         ret = -EINVAL;
1488         goto free_typec_port;
1489     }
1490 
1491     plat->dev = dev;
1492     plat->psy_online = ANX7411_PSY_OFFLINE;
1493     ret = anx7411_psy_register(plat);
1494     if (ret) {
1495         dev_err(dev, "register psy\n");
1496         goto free_typec_port;
1497     }
1498 
1499     INIT_WORK(&plat->work, anx7411_work_func);
1500     plat->workqueue = alloc_workqueue("anx7411_work",
1501                       WQ_FREEZABLE |
1502                       WQ_MEM_RECLAIM,
1503                       1);
1504     if (!plat->workqueue) {
1505         dev_err(dev, "fail to create work queue\n");
1506         ret = -ENOMEM;
1507         goto free_typec_port;
1508     }
1509 
1510     ret = devm_request_threaded_irq(dev, plat->intp_irq,
1511                     NULL, anx7411_intr_isr,
1512                     IRQF_TRIGGER_FALLING |
1513                     IRQF_ONESHOT,
1514                     "anx7411-intp", plat);
1515     if (ret) {
1516         dev_err(dev, "fail to request irq\n");
1517         goto free_wq;
1518     }
1519 
1520     if (anx7411_typec_check_connection(plat))
1521         dev_err(dev, "check status\n");
1522 
1523     pm_runtime_enable(dev);
1524 
1525     return 0;
1526 
1527 free_wq:
1528     destroy_workqueue(plat->workqueue);
1529 
1530 free_typec_port:
1531     typec_unregister_port(plat->typec.port);
1532     anx7411_port_unregister_altmodes(plat->typec.port_amode);
1533 
1534 free_typec_switch:
1535     anx7411_unregister_switch(plat);
1536     anx7411_unregister_mux(plat);
1537 
1538 free_i2c_dummy:
1539     i2c_unregister_device(plat->spi_client);
1540 
1541     return ret;
1542 }
1543 
1544 static int anx7411_i2c_remove(struct i2c_client *client)
1545 {
1546     struct anx7411_data *plat = i2c_get_clientdata(client);
1547 
1548     anx7411_partner_unregister_altmode(plat);
1549     anx7411_unregister_partner(plat);
1550 
1551     if (plat->workqueue)
1552         destroy_workqueue(plat->workqueue);
1553 
1554     if (plat->spi_client)
1555         i2c_unregister_device(plat->spi_client);
1556 
1557     if (plat->typec.role_sw)
1558         usb_role_switch_put(plat->typec.role_sw);
1559 
1560     anx7411_unregister_mux(plat);
1561 
1562     anx7411_unregister_switch(plat);
1563 
1564     if (plat->typec.port)
1565         typec_unregister_port(plat->typec.port);
1566 
1567     anx7411_port_unregister_altmodes(plat->typec.port_amode);
1568 
1569     return 0;
1570 }
1571 
1572 static const struct i2c_device_id anx7411_id[] = {
1573     {"anx7411", 0},
1574     {}
1575 };
1576 
1577 MODULE_DEVICE_TABLE(i2c, anx7411_id);
1578 
1579 static const struct of_device_id anx_match_table[] = {
1580     {.compatible = "analogix,anx7411",},
1581     {},
1582 };
1583 
1584 static struct i2c_driver anx7411_driver = {
1585     .driver = {
1586         .name = "anx7411",
1587         .of_match_table = anx_match_table,
1588         .pm = &anx7411_pm_ops,
1589     },
1590     .probe = anx7411_i2c_probe,
1591     .remove = anx7411_i2c_remove,
1592 
1593     .id_table = anx7411_id,
1594 };
1595 
1596 module_i2c_driver(anx7411_driver);
1597 
1598 MODULE_DESCRIPTION("Anx7411 USB Type-C PD driver");
1599 MODULE_AUTHOR("Xin Ji <xji@analogixsemi.com>");
1600 MODULE_LICENSE("GPL");
1601 MODULE_VERSION("0.1.5");