Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: typec: anx7411: Add Analogix PD ANX7411 support

Add driver for analogix ANX7411 USB Type-C DRP port controller.

Acked-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Xin Ji <xji@analogixsemi.com>
Link: https://lore.kernel.org/r/20220714081350.36447-2-xji@analogixsemi.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Xin Ji and committed by
Greg Kroah-Hartman
fe6d8a9c 4af37191

+1608
+11
drivers/usb/typec/Kconfig
··· 52 52 53 53 source "drivers/usb/typec/tipd/Kconfig" 54 54 55 + config TYPEC_ANX7411 56 + tristate "Analogix ANX7411 Type-C DRP Port controller driver" 57 + depends on I2C 58 + depends on USB_ROLE_SWITCH 59 + help 60 + Say Y or M here if your system has Analogix ANX7411 Type-C DRP Port 61 + controller driver. 62 + 63 + If you choose to build this driver as a dynamically linked module, the 64 + module will be called anx7411.ko. 65 + 55 66 config TYPEC_RT1719 56 67 tristate "Richtek RT1719 Sink Only Type-C controller driver" 57 68 depends on USB_ROLE_SWITCH || !USB_ROLE_SWITCH
+1
drivers/usb/typec/Makefile
··· 6 6 obj-$(CONFIG_TYPEC_TCPM) += tcpm/ 7 7 obj-$(CONFIG_TYPEC_UCSI) += ucsi/ 8 8 obj-$(CONFIG_TYPEC_TPS6598X) += tipd/ 9 + obj-$(CONFIG_TYPEC_ANX7411) += anx7411.o 9 10 obj-$(CONFIG_TYPEC_HD3SS3220) += hd3ss3220.o 10 11 obj-$(CONFIG_TYPEC_QCOM_PMIC) += qcom-pmic-typec.o 11 12 obj-$(CONFIG_TYPEC_STUSB160X) += stusb160x.o
+1596
drivers/usb/typec/anx7411.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + 3 + /* 4 + * Driver for Analogix ANX7411 USB Type-C and PD controller 5 + * 6 + * Copyright(c) 2022, Analogix Semiconductor. All rights reserved. 7 + * 8 + */ 9 + #include <linux/gpio/consumer.h> 10 + #include <linux/i2c.h> 11 + #include <linux/interrupt.h> 12 + #include <linux/iopoll.h> 13 + #include <linux/kernel.h> 14 + #include <linux/module.h> 15 + #include <linux/mutex.h> 16 + #include <linux/of_graph.h> 17 + #include <linux/of_platform.h> 18 + #include <linux/pm_runtime.h> 19 + #include <linux/regulator/consumer.h> 20 + #include <linux/slab.h> 21 + #include <linux/types.h> 22 + #include <linux/usb/pd.h> 23 + #include <linux/usb/role.h> 24 + #include <linux/usb/tcpci.h> 25 + #include <linux/usb/typec.h> 26 + #include <linux/usb/typec_dp.h> 27 + #include <linux/usb/typec_mux.h> 28 + #include <linux/workqueue.h> 29 + #include <linux/power_supply.h> 30 + 31 + #define TCPC_ADDRESS1 0x58 32 + #define TCPC_ADDRESS2 0x56 33 + #define TCPC_ADDRESS3 0x54 34 + #define TCPC_ADDRESS4 0x52 35 + #define SPI_ADDRESS1 0x7e 36 + #define SPI_ADDRESS2 0x6e 37 + #define SPI_ADDRESS3 0x64 38 + #define SPI_ADDRESS4 0x62 39 + 40 + struct anx7411_i2c_select { 41 + u8 tcpc_address; 42 + u8 spi_address; 43 + }; 44 + 45 + #define VID_ANALOGIX 0x1F29 46 + #define PID_ANALOGIX 0x7411 47 + 48 + /* TCPC register define */ 49 + 50 + #define ANALOG_CTRL_10 0xAA 51 + 52 + #define STATUS_LEN 2 53 + #define ALERT_0 0xCB 54 + #define RECEIVED_MSG BIT(7) 55 + #define SOFTWARE_INT BIT(6) 56 + #define MSG_LEN 32 57 + #define HEADER_LEN 2 58 + #define MSG_HEADER 0x00 59 + #define MSG_TYPE 0x01 60 + #define MSG_RAWDATA 0x02 61 + #define MSG_LEN_MASK 0x1F 62 + 63 + #define ALERT_1 0xCC 64 + #define INTP_POW_ON BIT(7) 65 + #define INTP_POW_OFF BIT(6) 66 + 67 + #define VBUS_THRESHOLD_H 0xDD 68 + #define VBUS_THRESHOLD_L 0xDE 69 + 70 + #define FW_CTRL_0 0xF0 71 + #define UNSTRUCT_VDM_EN BIT(0) 72 + #define DELAY_200MS BIT(1) 73 + #define VSAFE0 0 74 + #define VSAFE1 BIT(2) 75 + #define VSAFE2 BIT(3) 76 + #define VSAFE3 (BIT(2) | BIT(3)) 77 + #define FRS_EN BIT(7) 78 + 79 + #define FW_PARAM 0xF1 80 + #define DONGLE_IOP BIT(0) 81 + 82 + #define FW_CTRL_2 0xF7 83 + #define SINK_CTRL_DIS_FLAG BIT(5) 84 + 85 + /* SPI register define */ 86 + #define OCM_CTRL_0 0x6E 87 + #define OCM_RESET BIT(6) 88 + 89 + #define MAX_VOLTAGE 0xAC 90 + #define MAX_POWER 0xAD 91 + #define MIN_POWER 0xAE 92 + 93 + #define REQUEST_VOLTAGE 0xAF 94 + #define VOLTAGE_UNIT 100 /* mV per unit */ 95 + 96 + #define REQUEST_CURRENT 0xB1 97 + #define CURRENT_UNIT 50 /* mA per unit */ 98 + 99 + #define CMD_SEND_BUF 0xC0 100 + #define CMD_RECV_BUF 0xE0 101 + 102 + #define REQ_VOL_20V_IN_100MV 0xC8 103 + #define REQ_CUR_2_25A_IN_50MA 0x2D 104 + #define REQ_CUR_3_25A_IN_50MA 0x41 105 + 106 + #define DEF_5V 5000 107 + #define DEF_1_5A 1500 108 + 109 + #define LOBYTE(w) ((u8)((w) & 0xFF)) 110 + #define HIBYTE(w) ((u8)(((u16)(w) >> 8) & 0xFF)) 111 + 112 + enum anx7411_typec_message_type { 113 + TYPE_SRC_CAP = 0x00, 114 + TYPE_SNK_CAP = 0x01, 115 + TYPE_SNK_IDENTITY = 0x02, 116 + TYPE_SVID = 0x03, 117 + TYPE_SET_SNK_DP_CAP = 0x08, 118 + TYPE_PSWAP_REQ = 0x10, 119 + TYPE_DSWAP_REQ = 0x11, 120 + TYPE_VDM = 0x14, 121 + TYPE_OBJ_REQ = 0x16, 122 + TYPE_DP_ALT_ENTER = 0x19, 123 + TYPE_DP_DISCOVER_MODES_INFO = 0x27, 124 + TYPE_GET_DP_CONFIG = 0x29, 125 + TYPE_DP_CONFIGURE = 0x2A, 126 + TYPE_GET_DP_DISCOVER_MODES_INFO = 0x2E, 127 + TYPE_GET_DP_ALT_ENTER = 0x2F, 128 + }; 129 + 130 + #define FW_CTRL_1 0xB2 131 + #define AUTO_PD_EN BIT(1) 132 + #define TRYSRC_EN BIT(2) 133 + #define TRYSNK_EN BIT(3) 134 + #define FORCE_SEND_RDO BIT(6) 135 + 136 + #define FW_VER 0xB4 137 + #define FW_SUBVER 0xB5 138 + 139 + #define INT_MASK 0xB6 140 + #define INT_STS 0xB7 141 + #define OCM_BOOT_UP BIT(0) 142 + #define OC_OV_EVENT BIT(1) 143 + #define VCONN_CHANGE BIT(2) 144 + #define VBUS_CHANGE BIT(3) 145 + #define CC_STATUS_CHANGE BIT(4) 146 + #define DATA_ROLE_CHANGE BIT(5) 147 + #define PR_CONSUMER_GOT_POWER BIT(6) 148 + #define HPD_STATUS_CHANGE BIT(7) 149 + 150 + #define SYSTEM_STSTUS 0xB8 151 + /* 0: SINK off; 1: SINK on */ 152 + #define SINK_STATUS BIT(1) 153 + /* 0: VCONN off; 1: VCONN on*/ 154 + #define VCONN_STATUS BIT(2) 155 + /* 0: vbus off; 1: vbus on*/ 156 + #define VBUS_STATUS BIT(3) 157 + /* 1: host; 0:device*/ 158 + #define DATA_ROLE BIT(5) 159 + /* 0: Chunking; 1: Unchunked*/ 160 + #define SUPPORT_UNCHUNKING BIT(6) 161 + /* 0: HPD low; 1: HPD high*/ 162 + #define HPD_STATUS BIT(7) 163 + 164 + #define DATA_DFP 1 165 + #define DATA_UFP 2 166 + #define POWER_SOURCE 1 167 + #define POWER_SINK 2 168 + 169 + #define CC_STATUS 0xB9 170 + #define CC1_RD BIT(0) 171 + #define CC2_RD BIT(4) 172 + #define CC1_RA BIT(1) 173 + #define CC2_RA BIT(5) 174 + #define CC1_RD BIT(0) 175 + #define CC1_RP(cc) (((cc) >> 2) & 0x03) 176 + #define CC2_RP(cc) (((cc) >> 6) & 0x03) 177 + 178 + #define PD_REV_INIT 0xBA 179 + 180 + #define PD_EXT_MSG_CTRL 0xBB 181 + #define SRC_CAP_EXT_REPLY BIT(0) 182 + #define MANUFACTURER_INFO_REPLY BIT(1) 183 + #define BATTERY_STS_REPLY BIT(2) 184 + #define BATTERY_CAP_REPLY BIT(3) 185 + #define ALERT_REPLY BIT(4) 186 + #define STATUS_REPLY BIT(5) 187 + #define PPS_STATUS_REPLY BIT(6) 188 + #define SNK_CAP_EXT_REPLY BIT(7) 189 + 190 + #define NO_CONNECT 0x00 191 + #define USB3_1_CONNECTED 0x01 192 + #define DP_ALT_4LANES 0x02 193 + #define USB3_1_DP_2LANES 0x03 194 + #define CC1_CONNECTED 0x01 195 + #define CC2_CONNECTED 0x02 196 + #define SELECT_PIN_ASSIGMENT_C 0x04 197 + #define SELECT_PIN_ASSIGMENT_D 0x08 198 + #define SELECT_PIN_ASSIGMENT_E 0x10 199 + #define SELECT_PIN_ASSIGMENT_U 0x00 200 + #define REDRIVER_ADDRESS 0x20 201 + #define REDRIVER_OFFSET 0x00 202 + 203 + #define DP_SVID 0xFF01 204 + #define VDM_ACK 0x40 205 + #define VDM_CMD_RES 0x00 206 + #define VDM_CMD_DIS_ID 0x01 207 + #define VDM_CMD_DIS_SVID 0x02 208 + #define VDM_CMD_DIS_MOD 0x03 209 + #define VDM_CMD_ENTER_MODE 0x04 210 + #define VDM_CMD_EXIT_MODE 0x05 211 + #define VDM_CMD_ATTENTION 0x06 212 + #define VDM_CMD_GET_STS 0x10 213 + #define VDM_CMD_AND_ACK_MASK 0x5F 214 + 215 + #define MAX_ALTMODE 2 216 + 217 + #define HAS_SOURCE_CAP BIT(0) 218 + #define HAS_SINK_CAP BIT(1) 219 + #define HAS_SINK_WATT BIT(2) 220 + 221 + enum anx7411_psy_state { 222 + /* copy from drivers/usb/typec/tcpm */ 223 + ANX7411_PSY_OFFLINE = 0, 224 + ANX7411_PSY_FIXED_ONLINE, 225 + 226 + /* private */ 227 + /* PD keep in, but disconnct power to bq25700, 228 + * this state can be active when higher capacity adapter plug in, 229 + * and change to ONLINE state when higher capacity adapter plug out 230 + */ 231 + ANX7411_PSY_HANG = 0xff, 232 + }; 233 + 234 + struct typec_params { 235 + int request_current; /* ma */ 236 + int request_voltage; /* mv */ 237 + int cc_connect; 238 + int cc_orientation_valid; 239 + int cc_status; 240 + int data_role; 241 + int power_role; 242 + int vconn_role; 243 + int dp_altmode_enter; 244 + int cust_altmode_enter; 245 + struct usb_role_switch *role_sw; 246 + struct typec_port *port; 247 + struct typec_partner *partner; 248 + struct typec_mux_dev *typec_mux; 249 + struct typec_switch_dev *typec_switch; 250 + struct typec_altmode *amode[MAX_ALTMODE]; 251 + struct typec_altmode *port_amode[MAX_ALTMODE]; 252 + struct typec_displayport_data data; 253 + int pin_assignment; 254 + struct typec_capability caps; 255 + u32 src_pdo[PDO_MAX_OBJECTS]; 256 + u32 sink_pdo[PDO_MAX_OBJECTS]; 257 + u8 caps_flags; 258 + u8 src_pdo_nr; 259 + u8 sink_pdo_nr; 260 + u8 sink_watt; 261 + u8 sink_voltage; 262 + }; 263 + 264 + #define MAX_BUF_LEN 30 265 + struct fw_msg { 266 + u8 msg_len; 267 + u8 msg_type; 268 + u8 buf[MAX_BUF_LEN]; 269 + } __packed; 270 + 271 + struct anx7411_data { 272 + int fw_version; 273 + int fw_subversion; 274 + struct i2c_client *tcpc_client; 275 + struct i2c_client *spi_client; 276 + struct fw_msg send_msg; 277 + struct fw_msg recv_msg; 278 + struct gpio_desc *intp_gpiod; 279 + struct fwnode_handle *connector_fwnode; 280 + struct typec_params typec; 281 + int intp_irq; 282 + struct work_struct work; 283 + struct workqueue_struct *workqueue; 284 + /* Lock for interrupt work queue */ 285 + struct mutex lock; 286 + 287 + enum anx7411_psy_state psy_online; 288 + enum power_supply_usb_type usb_type; 289 + struct power_supply *psy; 290 + struct power_supply_desc psy_desc; 291 + struct device *dev; 292 + }; 293 + 294 + static u8 snk_identity[] = { 295 + LOBYTE(VID_ANALOGIX), HIBYTE(VID_ANALOGIX), 0x00, 0x82, /* snk_id_hdr */ 296 + 0x00, 0x00, 0x00, 0x00, /* snk_cert */ 297 + 0x00, 0x00, LOBYTE(PID_ANALOGIX), HIBYTE(PID_ANALOGIX), /* 5snk_ama */ 298 + }; 299 + 300 + static u8 dp_caps[4] = {0xC6, 0x00, 0x00, 0x00}; 301 + 302 + static int anx7411_reg_read(struct i2c_client *client, 303 + u8 reg_addr) 304 + { 305 + return i2c_smbus_read_byte_data(client, reg_addr); 306 + } 307 + 308 + static int anx7411_reg_block_read(struct i2c_client *client, 309 + u8 reg_addr, u8 len, u8 *buf) 310 + { 311 + return i2c_smbus_read_i2c_block_data(client, reg_addr, len, buf); 312 + } 313 + 314 + static int anx7411_reg_write(struct i2c_client *client, 315 + u8 reg_addr, u8 reg_val) 316 + { 317 + return i2c_smbus_write_byte_data(client, reg_addr, reg_val); 318 + } 319 + 320 + static int anx7411_reg_block_write(struct i2c_client *client, 321 + u8 reg_addr, u8 len, u8 *buf) 322 + { 323 + return i2c_smbus_write_i2c_block_data(client, reg_addr, len, buf); 324 + } 325 + 326 + static struct anx7411_i2c_select anx7411_i2c_addr[] = { 327 + {TCPC_ADDRESS1, SPI_ADDRESS1}, 328 + {TCPC_ADDRESS2, SPI_ADDRESS2}, 329 + {TCPC_ADDRESS3, SPI_ADDRESS3}, 330 + {TCPC_ADDRESS4, SPI_ADDRESS4}, 331 + }; 332 + 333 + static int anx7411_detect_power_mode(struct anx7411_data *ctx) 334 + { 335 + int ret; 336 + int mode; 337 + 338 + ret = anx7411_reg_read(ctx->spi_client, REQUEST_CURRENT); 339 + if (ret < 0) 340 + return ret; 341 + 342 + ctx->typec.request_current = ret * CURRENT_UNIT; /* 50ma per unit */ 343 + 344 + ret = anx7411_reg_read(ctx->spi_client, REQUEST_VOLTAGE); 345 + if (ret < 0) 346 + return ret; 347 + 348 + ctx->typec.request_voltage = ret * VOLTAGE_UNIT; /* 100mv per unit */ 349 + 350 + if (ctx->psy_online == ANX7411_PSY_OFFLINE) { 351 + ctx->psy_online = ANX7411_PSY_FIXED_ONLINE; 352 + ctx->usb_type = POWER_SUPPLY_USB_TYPE_PD; 353 + power_supply_changed(ctx->psy); 354 + } 355 + 356 + if (!ctx->typec.cc_orientation_valid) 357 + return 0; 358 + 359 + if (ctx->typec.cc_connect == CC1_CONNECTED) 360 + mode = CC1_RP(ctx->typec.cc_status); 361 + else 362 + mode = CC2_RP(ctx->typec.cc_status); 363 + if (mode) { 364 + typec_set_pwr_opmode(ctx->typec.port, mode - 1); 365 + return 0; 366 + } 367 + 368 + typec_set_pwr_opmode(ctx->typec.port, TYPEC_PWR_MODE_PD); 369 + 370 + return 0; 371 + } 372 + 373 + static int anx7411_register_partner(struct anx7411_data *ctx, 374 + int pd, int accessory) 375 + { 376 + struct typec_partner_desc desc; 377 + 378 + if (ctx->typec.partner) 379 + return 0; 380 + 381 + desc.usb_pd = pd; 382 + desc.accessory = accessory; 383 + desc.identity = NULL; 384 + ctx->typec.partner = typec_register_partner(ctx->typec.port, &desc); 385 + if (IS_ERR(ctx->typec.partner)) { 386 + ctx->typec.partner = NULL; 387 + return PTR_ERR(ctx->typec.partner); 388 + } 389 + 390 + return 0; 391 + } 392 + 393 + static int anx7411_detect_cc_orientation(struct anx7411_data *ctx) 394 + { 395 + struct device *dev = &ctx->spi_client->dev; 396 + int ret; 397 + int cc1_rd, cc2_rd; 398 + int cc1_ra, cc2_ra; 399 + int cc1_rp, cc2_rp; 400 + 401 + ret = anx7411_reg_read(ctx->spi_client, CC_STATUS); 402 + if (ret < 0) 403 + return ret; 404 + 405 + ctx->typec.cc_status = ret; 406 + 407 + cc1_rd = ret & CC1_RD ? 1 : 0; 408 + cc2_rd = ret & CC2_RD ? 1 : 0; 409 + cc1_ra = ret & CC1_RA ? 1 : 0; 410 + cc2_ra = ret & CC2_RA ? 1 : 0; 411 + cc1_rp = CC1_RP(ret); 412 + cc2_rp = CC2_RP(ret); 413 + 414 + /* Debug cable, nothing to do */ 415 + if (cc1_rd && cc2_rd) { 416 + ctx->typec.cc_orientation_valid = 0; 417 + return anx7411_register_partner(ctx, 0, TYPEC_ACCESSORY_DEBUG); 418 + } 419 + 420 + if (cc1_ra && cc2_ra) { 421 + ctx->typec.cc_orientation_valid = 0; 422 + return anx7411_register_partner(ctx, 0, TYPEC_ACCESSORY_AUDIO); 423 + } 424 + 425 + ctx->typec.cc_orientation_valid = 1; 426 + 427 + ret = anx7411_register_partner(ctx, 1, TYPEC_ACCESSORY_NONE); 428 + if (ret) { 429 + dev_err(dev, "register partner\n"); 430 + return ret; 431 + } 432 + 433 + if (cc1_rd || cc1_rp) { 434 + typec_set_orientation(ctx->typec.port, TYPEC_ORIENTATION_NORMAL); 435 + ctx->typec.cc_connect = CC1_CONNECTED; 436 + } 437 + 438 + if (cc2_rd || cc2_rp) { 439 + typec_set_orientation(ctx->typec.port, TYPEC_ORIENTATION_REVERSE); 440 + ctx->typec.cc_connect = CC2_CONNECTED; 441 + } 442 + 443 + return 0; 444 + } 445 + 446 + static int anx7411_set_mux(struct anx7411_data *ctx, int pin_assignment) 447 + { 448 + int mode = TYPEC_STATE_SAFE; 449 + 450 + switch (pin_assignment) { 451 + case SELECT_PIN_ASSIGMENT_U: 452 + /* default 4 line USB 3.1 */ 453 + mode = TYPEC_STATE_MODAL; 454 + break; 455 + case SELECT_PIN_ASSIGMENT_C: 456 + case SELECT_PIN_ASSIGMENT_E: 457 + /* 4 line DP */ 458 + mode = TYPEC_STATE_SAFE; 459 + break; 460 + case SELECT_PIN_ASSIGMENT_D: 461 + /* 2 line DP, 2 line USB */ 462 + mode = TYPEC_MODE_USB3; 463 + break; 464 + default: 465 + mode = TYPEC_STATE_SAFE; 466 + break; 467 + } 468 + 469 + ctx->typec.pin_assignment = pin_assignment; 470 + 471 + return typec_set_mode(ctx->typec.port, mode); 472 + } 473 + 474 + static int anx7411_set_usb_role(struct anx7411_data *ctx, enum usb_role role) 475 + { 476 + if (!ctx->typec.role_sw) 477 + return 0; 478 + 479 + return usb_role_switch_set_role(ctx->typec.role_sw, role); 480 + } 481 + 482 + static int anx7411_data_role_detect(struct anx7411_data *ctx) 483 + { 484 + int ret; 485 + 486 + ret = anx7411_reg_read(ctx->spi_client, SYSTEM_STSTUS); 487 + if (ret < 0) 488 + return ret; 489 + 490 + ctx->typec.data_role = (ret & DATA_ROLE) ? TYPEC_HOST : TYPEC_DEVICE; 491 + ctx->typec.vconn_role = (ret & VCONN_STATUS) ? TYPEC_SOURCE : TYPEC_SINK; 492 + 493 + typec_set_data_role(ctx->typec.port, ctx->typec.data_role); 494 + 495 + typec_set_vconn_role(ctx->typec.port, ctx->typec.vconn_role); 496 + 497 + if (ctx->typec.data_role == TYPEC_HOST) 498 + return anx7411_set_usb_role(ctx, USB_ROLE_HOST); 499 + 500 + return anx7411_set_usb_role(ctx, USB_ROLE_DEVICE); 501 + } 502 + 503 + static int anx7411_power_role_detect(struct anx7411_data *ctx) 504 + { 505 + int ret; 506 + 507 + ret = anx7411_reg_read(ctx->spi_client, SYSTEM_STSTUS); 508 + if (ret < 0) 509 + return ret; 510 + 511 + ctx->typec.power_role = (ret & SINK_STATUS) ? TYPEC_SINK : TYPEC_SOURCE; 512 + 513 + if (ctx->typec.power_role == TYPEC_SOURCE) { 514 + ctx->typec.request_current = DEF_1_5A; 515 + ctx->typec.request_voltage = DEF_5V; 516 + } 517 + 518 + typec_set_pwr_role(ctx->typec.port, ctx->typec.power_role); 519 + 520 + return 0; 521 + } 522 + 523 + static int anx7411_cc_status_detect(struct anx7411_data *ctx) 524 + { 525 + anx7411_detect_cc_orientation(ctx); 526 + anx7411_detect_power_mode(ctx); 527 + 528 + return 0; 529 + } 530 + 531 + static void anx7411_partner_unregister_altmode(struct anx7411_data *ctx) 532 + { 533 + int i; 534 + 535 + ctx->typec.dp_altmode_enter = 0; 536 + ctx->typec.cust_altmode_enter = 0; 537 + 538 + for (i = 0; i < MAX_ALTMODE; i++) 539 + if (ctx->typec.amode[i]) { 540 + typec_unregister_altmode(ctx->typec.amode[i]); 541 + ctx->typec.amode[i] = NULL; 542 + } 543 + 544 + ctx->typec.pin_assignment = 0; 545 + } 546 + 547 + static int anx7411_typec_register_altmode(struct anx7411_data *ctx, 548 + int svid, int vdo) 549 + { 550 + struct device *dev = &ctx->spi_client->dev; 551 + struct typec_altmode_desc desc; 552 + int i; 553 + 554 + desc.svid = svid; 555 + desc.vdo = vdo; 556 + 557 + for (i = 0; i < MAX_ALTMODE; i++) 558 + if (!ctx->typec.amode[i]) 559 + break; 560 + 561 + desc.mode = i + 1; /* start with 1 */ 562 + 563 + if (i >= MAX_ALTMODE) { 564 + dev_err(dev, "no altmode space for registering\n"); 565 + return -ENOMEM; 566 + } 567 + 568 + ctx->typec.amode[i] = typec_partner_register_altmode(ctx->typec.partner, 569 + &desc); 570 + if (IS_ERR(ctx->typec.amode[i])) { 571 + dev_err(dev, "failed to register altmode\n"); 572 + ctx->typec.amode[i] = NULL; 573 + return PTR_ERR(ctx->typec.amode); 574 + } 575 + 576 + return 0; 577 + } 578 + 579 + static void anx7411_unregister_partner(struct anx7411_data *ctx) 580 + { 581 + if (ctx->typec.partner) { 582 + typec_unregister_partner(ctx->typec.partner); 583 + ctx->typec.partner = NULL; 584 + } 585 + } 586 + 587 + static int anx7411_update_altmode(struct anx7411_data *ctx, int svid) 588 + { 589 + int i; 590 + 591 + if (svid == DP_SVID) 592 + ctx->typec.dp_altmode_enter = 1; 593 + else 594 + ctx->typec.cust_altmode_enter = 1; 595 + 596 + for (i = 0; i < MAX_ALTMODE; i++) { 597 + if (!ctx->typec.amode[i]) 598 + continue; 599 + 600 + if (ctx->typec.amode[i]->svid == svid) { 601 + typec_altmode_update_active(ctx->typec.amode[i], true); 602 + typec_altmode_notify(ctx->typec.amode[i], 603 + ctx->typec.pin_assignment, 604 + &ctx->typec.data); 605 + break; 606 + } 607 + } 608 + 609 + return 0; 610 + } 611 + 612 + static int anx7411_register_altmode(struct anx7411_data *ctx, 613 + bool dp_altmode, u8 *buf) 614 + { 615 + int ret; 616 + int svid; 617 + int mid; 618 + 619 + if (!ctx->typec.partner) 620 + return 0; 621 + 622 + svid = DP_SVID; 623 + if (dp_altmode) { 624 + mid = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); 625 + 626 + return anx7411_typec_register_altmode(ctx, svid, mid); 627 + } 628 + 629 + svid = (buf[3] << 8) | buf[2]; 630 + if ((buf[0] & VDM_CMD_AND_ACK_MASK) != (VDM_ACK | VDM_CMD_ENTER_MODE)) 631 + return anx7411_update_altmode(ctx, svid); 632 + 633 + if ((buf[0] & VDM_CMD_AND_ACK_MASK) != (VDM_ACK | VDM_CMD_DIS_MOD)) 634 + return 0; 635 + 636 + mid = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24); 637 + 638 + ret = anx7411_typec_register_altmode(ctx, svid, mid); 639 + if (ctx->typec.cust_altmode_enter) 640 + ret |= anx7411_update_altmode(ctx, svid); 641 + 642 + return ret; 643 + } 644 + 645 + static int anx7411_parse_cmd(struct anx7411_data *ctx, u8 type, u8 *buf, u8 len) 646 + { 647 + struct device *dev = &ctx->spi_client->dev; 648 + u8 cur_50ma, vol_100mv; 649 + 650 + switch (type) { 651 + case TYPE_SRC_CAP: 652 + cur_50ma = anx7411_reg_read(ctx->spi_client, REQUEST_CURRENT); 653 + vol_100mv = anx7411_reg_read(ctx->spi_client, REQUEST_VOLTAGE); 654 + 655 + ctx->typec.request_voltage = vol_100mv * VOLTAGE_UNIT; 656 + ctx->typec.request_current = cur_50ma * CURRENT_UNIT; 657 + 658 + ctx->psy_online = ANX7411_PSY_FIXED_ONLINE; 659 + ctx->usb_type = POWER_SUPPLY_USB_TYPE_PD; 660 + power_supply_changed(ctx->psy); 661 + break; 662 + case TYPE_SNK_CAP: 663 + break; 664 + case TYPE_SVID: 665 + break; 666 + case TYPE_SNK_IDENTITY: 667 + break; 668 + case TYPE_GET_DP_ALT_ENTER: 669 + /* DP alt mode enter success */ 670 + if (buf[0]) 671 + anx7411_update_altmode(ctx, DP_SVID); 672 + break; 673 + case TYPE_DP_ALT_ENTER: 674 + /* Update DP altmode */ 675 + anx7411_update_altmode(ctx, DP_SVID); 676 + break; 677 + case TYPE_OBJ_REQ: 678 + anx7411_detect_power_mode(ctx); 679 + break; 680 + case TYPE_DP_CONFIGURE: 681 + anx7411_set_mux(ctx, buf[1]); 682 + break; 683 + case TYPE_DP_DISCOVER_MODES_INFO: 684 + /* Make sure discover modes valid */ 685 + if (buf[0] | buf[1]) 686 + /* Register DP Altmode */ 687 + anx7411_register_altmode(ctx, 1, buf); 688 + break; 689 + case TYPE_VDM: 690 + /* Register other altmode */ 691 + anx7411_register_altmode(ctx, 0, buf); 692 + break; 693 + default: 694 + dev_err(dev, "ignore message(0x%.02x).\n", type); 695 + break; 696 + } 697 + 698 + return 0; 699 + } 700 + 701 + static u8 checksum(struct device *dev, u8 *buf, u8 len) 702 + { 703 + u8 ret = 0; 704 + u8 i; 705 + 706 + for (i = 0; i < len; i++) 707 + ret += buf[i]; 708 + 709 + return ret; 710 + } 711 + 712 + static int anx7411_read_msg_ctrl_status(struct i2c_client *client) 713 + { 714 + return anx7411_reg_read(client, CMD_SEND_BUF); 715 + } 716 + 717 + static int anx7411_wait_msg_empty(struct i2c_client *client) 718 + { 719 + int val; 720 + 721 + return readx_poll_timeout(anx7411_read_msg_ctrl_status, 722 + client, val, (val < 0) || (val == 0), 723 + 2000, 2000 * 150); 724 + } 725 + 726 + static int anx7411_send_msg(struct anx7411_data *ctx, u8 type, u8 *buf, u8 size) 727 + { 728 + struct device *dev = &ctx->spi_client->dev; 729 + struct fw_msg *msg = &ctx->send_msg; 730 + u8 crc; 731 + int ret; 732 + 733 + size = min_t(u8, size, (u8)MAX_BUF_LEN); 734 + memcpy(msg->buf, buf, size); 735 + msg->msg_type = type; 736 + /* msg len equals buffer length + msg_type */ 737 + msg->msg_len = size + 1; 738 + 739 + /* Do CRC check for all buffer data and msg_len and msg_type */ 740 + crc = checksum(dev, (u8 *)msg, size + HEADER_LEN); 741 + msg->buf[size] = 0 - crc; 742 + 743 + ret = anx7411_wait_msg_empty(ctx->spi_client); 744 + if (ret) 745 + return ret; 746 + 747 + ret = anx7411_reg_block_write(ctx->spi_client, 748 + CMD_SEND_BUF + 1, size + HEADER_LEN, 749 + &msg->msg_type); 750 + ret |= anx7411_reg_write(ctx->spi_client, CMD_SEND_BUF, 751 + msg->msg_len); 752 + return ret; 753 + } 754 + 755 + static int anx7411_process_cmd(struct anx7411_data *ctx) 756 + { 757 + struct device *dev = &ctx->spi_client->dev; 758 + struct fw_msg *msg = &ctx->recv_msg; 759 + u8 len; 760 + u8 crc; 761 + int ret; 762 + 763 + /* Read message from firmware */ 764 + ret = anx7411_reg_block_read(ctx->spi_client, CMD_RECV_BUF, 765 + MSG_LEN, (u8 *)msg); 766 + if (ret < 0) 767 + return 0; 768 + 769 + if (!msg->msg_len) 770 + return 0; 771 + 772 + ret = anx7411_reg_write(ctx->spi_client, CMD_RECV_BUF, 0); 773 + if (ret) 774 + return ret; 775 + 776 + len = msg->msg_len & MSG_LEN_MASK; 777 + crc = checksum(dev, (u8 *)msg, len + HEADER_LEN); 778 + if (crc) { 779 + dev_err(dev, "message error crc(0x%.02x)\n", crc); 780 + return -ERANGE; 781 + } 782 + 783 + return anx7411_parse_cmd(ctx, msg->msg_type, msg->buf, len - 1); 784 + } 785 + 786 + static void anx7411_translate_payload(struct device *dev, __le32 *payload, 787 + u32 *pdo, int nr, const char *type) 788 + { 789 + int i; 790 + 791 + if (nr > PDO_MAX_OBJECTS) { 792 + dev_err(dev, "nr(%d) exceed PDO_MAX_OBJECTS(%d)\n", 793 + nr, PDO_MAX_OBJECTS); 794 + 795 + return; 796 + } 797 + 798 + for (i = 0; i < nr; i++) 799 + payload[i] = cpu_to_le32(pdo[i]); 800 + } 801 + 802 + static int anx7411_config(struct anx7411_data *ctx) 803 + { 804 + struct device *dev = &ctx->spi_client->dev; 805 + struct typec_params *typecp = &ctx->typec; 806 + __le32 payload[PDO_MAX_OBJECTS]; 807 + int ret; 808 + 809 + /* Config PD FW work under PD 2.0 */ 810 + ret = anx7411_reg_write(ctx->spi_client, PD_REV_INIT, PD_REV20); 811 + ret |= anx7411_reg_write(ctx->tcpc_client, FW_CTRL_0, 812 + UNSTRUCT_VDM_EN | DELAY_200MS | 813 + VSAFE1 | FRS_EN); 814 + ret |= anx7411_reg_write(ctx->spi_client, FW_CTRL_1, 815 + AUTO_PD_EN | FORCE_SEND_RDO); 816 + 817 + /* Set VBUS current threshold */ 818 + ret |= anx7411_reg_write(ctx->tcpc_client, VBUS_THRESHOLD_H, 0xff); 819 + ret |= anx7411_reg_write(ctx->tcpc_client, VBUS_THRESHOLD_L, 0x03); 820 + 821 + /* Fix dongle compatible issue */ 822 + ret |= anx7411_reg_write(ctx->tcpc_client, FW_PARAM, 823 + anx7411_reg_read(ctx->tcpc_client, FW_PARAM) | 824 + DONGLE_IOP); 825 + ret |= anx7411_reg_write(ctx->spi_client, INT_MASK, 0); 826 + 827 + ret |= anx7411_reg_write(ctx->spi_client, PD_EXT_MSG_CTRL, 0xFF); 828 + if (ret) 829 + return ret; 830 + 831 + if (typecp->caps_flags & HAS_SOURCE_CAP) { 832 + anx7411_translate_payload(dev, payload, typecp->src_pdo, 833 + typecp->src_pdo_nr, "source"); 834 + anx7411_send_msg(ctx, TYPE_SRC_CAP, (u8 *)&payload, 835 + typecp->src_pdo_nr * 4); 836 + anx7411_send_msg(ctx, TYPE_SNK_IDENTITY, snk_identity, 837 + sizeof(snk_identity)); 838 + anx7411_send_msg(ctx, TYPE_SET_SNK_DP_CAP, dp_caps, 839 + sizeof(dp_caps)); 840 + } 841 + 842 + if (typecp->caps_flags & HAS_SINK_CAP) { 843 + anx7411_translate_payload(dev, payload, typecp->sink_pdo, 844 + typecp->sink_pdo_nr, "sink"); 845 + anx7411_send_msg(ctx, TYPE_SNK_CAP, (u8 *)&payload, 846 + typecp->sink_pdo_nr * 4); 847 + } 848 + 849 + if (typecp->caps_flags & HAS_SINK_WATT) { 850 + if (typecp->sink_watt) { 851 + ret |= anx7411_reg_write(ctx->spi_client, MAX_POWER, 852 + typecp->sink_watt); 853 + /* Set min power to 1W */ 854 + ret |= anx7411_reg_write(ctx->spi_client, MIN_POWER, 2); 855 + } 856 + 857 + if (typecp->sink_voltage) 858 + ret |= anx7411_reg_write(ctx->spi_client, MAX_VOLTAGE, 859 + typecp->sink_voltage); 860 + if (ret) 861 + return ret; 862 + } 863 + 864 + if (!typecp->caps_flags) 865 + usleep_range(5000, 6000); 866 + 867 + ctx->fw_version = anx7411_reg_read(ctx->spi_client, FW_VER); 868 + ctx->fw_subversion = anx7411_reg_read(ctx->spi_client, FW_SUBVER); 869 + 870 + return 0; 871 + } 872 + 873 + static void anx7411_chip_standby(struct anx7411_data *ctx) 874 + { 875 + int ret; 876 + u8 cc1, cc2; 877 + struct device *dev = &ctx->spi_client->dev; 878 + 879 + ret = anx7411_reg_write(ctx->spi_client, OCM_CTRL_0, 880 + anx7411_reg_read(ctx->spi_client, OCM_CTRL_0) | 881 + OCM_RESET); 882 + ret |= anx7411_reg_write(ctx->tcpc_client, ANALOG_CTRL_10, 0x80); 883 + /* Set TCPC to RD and DRP enable */ 884 + cc1 = TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT; 885 + cc2 = TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT; 886 + ret |= anx7411_reg_write(ctx->tcpc_client, TCPC_ROLE_CTRL, 887 + TCPC_ROLE_CTRL_DRP | cc1 | cc2); 888 + 889 + /* Send DRP toggle command */ 890 + ret |= anx7411_reg_write(ctx->tcpc_client, TCPC_COMMAND, 891 + TCPC_CMD_LOOK4CONNECTION); 892 + 893 + /* Send TCPC enter standby command */ 894 + ret |= anx7411_reg_write(ctx->tcpc_client, 895 + TCPC_COMMAND, TCPC_CMD_I2C_IDLE); 896 + if (ret) 897 + dev_err(dev, "Chip standby failed\n"); 898 + } 899 + 900 + static void anx7411_work_func(struct work_struct *work) 901 + { 902 + int ret; 903 + u8 buf[STATUS_LEN]; 904 + u8 int_change; /* Interrupt change */ 905 + u8 int_status; /* Firmware status update */ 906 + u8 alert0, alert1; /* Interrupt alert source */ 907 + struct anx7411_data *ctx = container_of(work, struct anx7411_data, work); 908 + struct device *dev = &ctx->spi_client->dev; 909 + 910 + mutex_lock(&ctx->lock); 911 + 912 + /* Read interrupt change status */ 913 + ret = anx7411_reg_block_read(ctx->spi_client, INT_STS, STATUS_LEN, buf); 914 + if (ret < 0) { 915 + /* Power standby mode, just return */ 916 + goto unlock; 917 + } 918 + int_change = buf[0]; 919 + int_status = buf[1]; 920 + 921 + /* Read alert register */ 922 + ret = anx7411_reg_block_read(ctx->tcpc_client, ALERT_0, STATUS_LEN, buf); 923 + if (ret < 0) 924 + goto unlock; 925 + 926 + alert0 = buf[0]; 927 + alert1 = buf[1]; 928 + 929 + /* Clear interrupt and alert status */ 930 + ret = anx7411_reg_write(ctx->spi_client, INT_STS, 0); 931 + ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_0, alert0); 932 + ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_1, alert1); 933 + if (ret) 934 + goto unlock; 935 + 936 + if (alert1 & INTP_POW_OFF) { 937 + anx7411_partner_unregister_altmode(ctx); 938 + if (anx7411_set_usb_role(ctx, USB_ROLE_NONE)) 939 + dev_err(dev, "Set usb role\n"); 940 + anx7411_unregister_partner(ctx); 941 + ctx->psy_online = ANX7411_PSY_OFFLINE; 942 + ctx->usb_type = POWER_SUPPLY_USB_TYPE_C; 943 + ctx->typec.request_voltage = 0; 944 + ctx->typec.request_current = 0; 945 + power_supply_changed(ctx->psy); 946 + anx7411_chip_standby(ctx); 947 + goto unlock; 948 + } 949 + 950 + if ((alert0 & SOFTWARE_INT) && (int_change & OCM_BOOT_UP)) { 951 + if (anx7411_config(ctx)) 952 + dev_err(dev, "Config failed\n"); 953 + if (anx7411_data_role_detect(ctx)) 954 + dev_err(dev, "set PD data role\n"); 955 + if (anx7411_power_role_detect(ctx)) 956 + dev_err(dev, "set PD power role\n"); 957 + anx7411_set_mux(ctx, SELECT_PIN_ASSIGMENT_C); 958 + } 959 + 960 + if (alert0 & RECEIVED_MSG) 961 + anx7411_process_cmd(ctx); 962 + 963 + ret = (int_status & DATA_ROLE) ? TYPEC_HOST : TYPEC_DEVICE; 964 + if (ctx->typec.data_role != ret) 965 + if (anx7411_data_role_detect(ctx)) 966 + dev_err(dev, "set PD data role\n"); 967 + 968 + ret = (int_status & SINK_STATUS) ? TYPEC_SINK : TYPEC_SOURCE; 969 + if (ctx->typec.power_role != ret) 970 + if (anx7411_power_role_detect(ctx)) 971 + dev_err(dev, "set PD power role\n"); 972 + 973 + if ((alert0 & SOFTWARE_INT) && (int_change & CC_STATUS_CHANGE)) 974 + anx7411_cc_status_detect(ctx); 975 + 976 + unlock: 977 + mutex_unlock(&ctx->lock); 978 + } 979 + 980 + static irqreturn_t anx7411_intr_isr(int irq, void *data) 981 + { 982 + struct anx7411_data *ctx = (struct anx7411_data *)data; 983 + 984 + queue_work(ctx->workqueue, &ctx->work); 985 + 986 + return IRQ_HANDLED; 987 + } 988 + 989 + static int anx7411_register_i2c_dummy_clients(struct anx7411_data *ctx, 990 + struct i2c_client *client) 991 + { 992 + int i; 993 + u8 spi_addr; 994 + 995 + for (i = 0; i < sizeof(anx7411_i2c_addr); i++) { 996 + if (client->addr == (anx7411_i2c_addr[i].tcpc_address >> 1)) { 997 + spi_addr = anx7411_i2c_addr[i].spi_address >> 1; 998 + ctx->spi_client = i2c_new_dummy_device(client->adapter, 999 + spi_addr); 1000 + if (ctx->spi_client) 1001 + return 0; 1002 + } 1003 + } 1004 + 1005 + dev_err(&client->dev, "unable to get SPI slave\n"); 1006 + return -ENOMEM; 1007 + } 1008 + 1009 + static void anx7411_port_unregister_altmodes(struct typec_altmode **adev) 1010 + { 1011 + int i; 1012 + 1013 + for (i = 0; i < MAX_ALTMODE; i++) 1014 + if (adev[i]) { 1015 + typec_unregister_altmode(adev[i]); 1016 + adev[i] = NULL; 1017 + } 1018 + } 1019 + 1020 + static int anx7411_usb_mux_set(struct typec_mux_dev *mux, 1021 + struct typec_mux_state *state) 1022 + { 1023 + struct anx7411_data *ctx = typec_mux_get_drvdata(mux); 1024 + struct device *dev = &ctx->spi_client->dev; 1025 + int has_dp; 1026 + 1027 + has_dp = (state->alt && state->alt->svid == USB_TYPEC_DP_SID && 1028 + state->alt->mode == USB_TYPEC_DP_MODE); 1029 + if (!has_dp) 1030 + dev_err(dev, "dp altmode not register\n"); 1031 + 1032 + return 0; 1033 + } 1034 + 1035 + static int anx7411_usb_set_orientation(struct typec_switch_dev *sw, 1036 + enum typec_orientation orientation) 1037 + { 1038 + /* No need set */ 1039 + 1040 + return 0; 1041 + } 1042 + 1043 + static int anx7411_register_switch(struct anx7411_data *ctx, 1044 + struct device *dev, 1045 + struct fwnode_handle *fwnode) 1046 + { 1047 + struct typec_switch_desc sw_desc = { }; 1048 + 1049 + sw_desc.fwnode = fwnode; 1050 + sw_desc.drvdata = ctx; 1051 + sw_desc.name = fwnode_get_name(fwnode); 1052 + sw_desc.set = anx7411_usb_set_orientation; 1053 + 1054 + ctx->typec.typec_switch = typec_switch_register(dev, &sw_desc); 1055 + if (IS_ERR(ctx->typec.typec_switch)) { 1056 + dev_err(dev, "switch register failed\n"); 1057 + return PTR_ERR(ctx->typec.typec_switch); 1058 + } 1059 + 1060 + return 0; 1061 + } 1062 + 1063 + static int anx7411_register_mux(struct anx7411_data *ctx, 1064 + struct device *dev, 1065 + struct fwnode_handle *fwnode) 1066 + { 1067 + struct typec_mux_desc mux_desc = { }; 1068 + 1069 + mux_desc.fwnode = fwnode; 1070 + mux_desc.drvdata = ctx; 1071 + mux_desc.name = fwnode_get_name(fwnode); 1072 + mux_desc.set = anx7411_usb_mux_set; 1073 + 1074 + ctx->typec.typec_mux = typec_mux_register(dev, &mux_desc); 1075 + if (IS_ERR(ctx->typec.typec_mux)) { 1076 + dev_err(dev, "mux register failed\n"); 1077 + return PTR_ERR(ctx->typec.typec_mux); 1078 + } 1079 + 1080 + return 0; 1081 + } 1082 + 1083 + static void anx7411_unregister_mux(struct anx7411_data *ctx) 1084 + { 1085 + if (ctx->typec.typec_mux) { 1086 + typec_mux_unregister(ctx->typec.typec_mux); 1087 + ctx->typec.typec_mux = NULL; 1088 + } 1089 + } 1090 + 1091 + static void anx7411_unregister_switch(struct anx7411_data *ctx) 1092 + { 1093 + if (ctx->typec.typec_switch) { 1094 + typec_switch_unregister(ctx->typec.typec_switch); 1095 + ctx->typec.typec_switch = NULL; 1096 + } 1097 + } 1098 + 1099 + static int anx7411_typec_switch_probe(struct anx7411_data *ctx, 1100 + struct device *dev) 1101 + { 1102 + int ret; 1103 + struct device_node *node; 1104 + 1105 + node = of_find_node_by_name(dev->of_node, "orientation_switch"); 1106 + if (!node) 1107 + return 0; 1108 + 1109 + ret = anx7411_register_switch(ctx, dev, &node->fwnode); 1110 + if (ret) { 1111 + dev_err(dev, "failed register switch"); 1112 + return ret; 1113 + } 1114 + 1115 + node = of_find_node_by_name(dev->of_node, "mode_switch"); 1116 + if (!node) { 1117 + dev_err(dev, "no typec mux exist"); 1118 + ret = -ENODEV; 1119 + goto unregister_switch; 1120 + } 1121 + 1122 + ret = anx7411_register_mux(ctx, dev, &node->fwnode); 1123 + if (ret) { 1124 + dev_err(dev, "failed register mode switch"); 1125 + ret = -ENODEV; 1126 + goto unregister_switch; 1127 + } 1128 + 1129 + return 0; 1130 + 1131 + unregister_switch: 1132 + anx7411_unregister_switch(ctx); 1133 + 1134 + return ret; 1135 + } 1136 + 1137 + static int anx7411_typec_port_probe(struct anx7411_data *ctx, 1138 + struct device *dev) 1139 + { 1140 + struct typec_capability *cap = &ctx->typec.caps; 1141 + struct typec_params *typecp = &ctx->typec; 1142 + struct fwnode_handle *fwnode; 1143 + const char *buf; 1144 + int ret, i; 1145 + 1146 + fwnode = device_get_named_child_node(dev, "connector"); 1147 + if (!fwnode) 1148 + return -EINVAL; 1149 + 1150 + ret = fwnode_property_read_string(fwnode, "power-role", &buf); 1151 + if (ret) { 1152 + dev_err(dev, "power-role not found: %d\n", ret); 1153 + return ret; 1154 + } 1155 + 1156 + ret = typec_find_port_power_role(buf); 1157 + if (ret < 0) 1158 + return ret; 1159 + cap->type = ret; 1160 + 1161 + ret = fwnode_property_read_string(fwnode, "data-role", &buf); 1162 + if (ret) { 1163 + dev_err(dev, "data-role not found: %d\n", ret); 1164 + return ret; 1165 + } 1166 + 1167 + ret = typec_find_port_data_role(buf); 1168 + if (ret < 0) 1169 + return ret; 1170 + cap->data = ret; 1171 + 1172 + ret = fwnode_property_read_string(fwnode, "try-power-role", &buf); 1173 + if (ret) { 1174 + dev_err(dev, "try-power-role not found: %d\n", ret); 1175 + return ret; 1176 + } 1177 + 1178 + ret = typec_find_power_role(buf); 1179 + if (ret < 0) 1180 + return ret; 1181 + cap->prefer_role = ret; 1182 + 1183 + /* Get source pdos */ 1184 + ret = fwnode_property_count_u32(fwnode, "source-pdos"); 1185 + if (ret > 0) { 1186 + typecp->src_pdo_nr = min_t(u8, ret, PDO_MAX_OBJECTS); 1187 + ret = fwnode_property_read_u32_array(fwnode, "source-pdos", 1188 + typecp->src_pdo, 1189 + typecp->src_pdo_nr); 1190 + if (ret < 0) { 1191 + dev_err(dev, "source cap validate failed: %d\n", ret); 1192 + return -EINVAL; 1193 + } 1194 + 1195 + typecp->caps_flags |= HAS_SOURCE_CAP; 1196 + } 1197 + 1198 + ret = fwnode_property_count_u32(fwnode, "sink-pdos"); 1199 + if (ret > 0) { 1200 + typecp->sink_pdo_nr = min_t(u8, ret, PDO_MAX_OBJECTS); 1201 + ret = fwnode_property_read_u32_array(fwnode, "sink-pdos", 1202 + typecp->sink_pdo, 1203 + typecp->sink_pdo_nr); 1204 + if (ret < 0) { 1205 + dev_err(dev, "sink cap validate failed: %d\n", ret); 1206 + return -EINVAL; 1207 + } 1208 + 1209 + for (i = 0; i < typecp->sink_pdo_nr; i++) { 1210 + ret = 0; 1211 + switch (pdo_type(typecp->sink_pdo[i])) { 1212 + case PDO_TYPE_FIXED: 1213 + ret = pdo_fixed_voltage(typecp->sink_pdo[i]); 1214 + break; 1215 + case PDO_TYPE_BATT: 1216 + case PDO_TYPE_VAR: 1217 + ret = pdo_max_voltage(typecp->sink_pdo[i]); 1218 + break; 1219 + case PDO_TYPE_APDO: 1220 + default: 1221 + ret = 0; 1222 + break; 1223 + } 1224 + 1225 + /* 100mv per unit */ 1226 + typecp->sink_voltage = max(5000, ret) / 100; 1227 + } 1228 + 1229 + typecp->caps_flags |= HAS_SINK_CAP; 1230 + } 1231 + 1232 + if (!fwnode_property_read_u32(fwnode, "op-sink-microwatt", &ret)) { 1233 + typecp->sink_watt = ret / 500000; /* 500mw per unit */ 1234 + typecp->caps_flags |= HAS_SINK_WATT; 1235 + } 1236 + 1237 + cap->fwnode = fwnode; 1238 + 1239 + ctx->typec.role_sw = usb_role_switch_get(dev); 1240 + if (IS_ERR(ctx->typec.role_sw)) { 1241 + dev_err(dev, "USB role switch not found.\n"); 1242 + ctx->typec.role_sw = NULL; 1243 + } 1244 + 1245 + ctx->typec.port = typec_register_port(dev, cap); 1246 + if (IS_ERR(ctx->typec.port)) { 1247 + ret = PTR_ERR(ctx->typec.port); 1248 + ctx->typec.port = NULL; 1249 + dev_err(dev, "Failed to register type c port %d\n", ret); 1250 + return ret; 1251 + } 1252 + 1253 + typec_port_register_altmodes(ctx->typec.port, NULL, ctx, 1254 + ctx->typec.port_amode, 1255 + MAX_ALTMODE); 1256 + return 0; 1257 + } 1258 + 1259 + static int anx7411_typec_check_connection(struct anx7411_data *ctx) 1260 + { 1261 + int ret; 1262 + 1263 + ret = anx7411_reg_read(ctx->spi_client, FW_VER); 1264 + if (ret < 0) 1265 + return 0; /* No device attached in typec port */ 1266 + 1267 + /* Clear interrupt and alert status */ 1268 + ret = anx7411_reg_write(ctx->spi_client, INT_STS, 0); 1269 + ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_0, 0xFF); 1270 + ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_1, 0xFF); 1271 + if (ret) 1272 + return ret; 1273 + 1274 + ret = anx7411_cc_status_detect(ctx); 1275 + ret |= anx7411_power_role_detect(ctx); 1276 + ret |= anx7411_data_role_detect(ctx); 1277 + ret |= anx7411_set_mux(ctx, SELECT_PIN_ASSIGMENT_C); 1278 + if (ret) 1279 + return ret; 1280 + 1281 + ret = anx7411_send_msg(ctx, TYPE_GET_DP_ALT_ENTER, NULL, 0); 1282 + ret |= anx7411_send_msg(ctx, TYPE_GET_DP_DISCOVER_MODES_INFO, NULL, 0); 1283 + 1284 + return ret; 1285 + } 1286 + 1287 + static int __maybe_unused anx7411_runtime_pm_suspend(struct device *dev) 1288 + { 1289 + struct anx7411_data *ctx = dev_get_drvdata(dev); 1290 + 1291 + mutex_lock(&ctx->lock); 1292 + 1293 + anx7411_partner_unregister_altmode(ctx); 1294 + 1295 + if (ctx->typec.partner) 1296 + anx7411_unregister_partner(ctx); 1297 + 1298 + mutex_unlock(&ctx->lock); 1299 + 1300 + return 0; 1301 + } 1302 + 1303 + static int __maybe_unused anx7411_runtime_pm_resume(struct device *dev) 1304 + { 1305 + struct anx7411_data *ctx = dev_get_drvdata(dev); 1306 + 1307 + mutex_lock(&ctx->lock); 1308 + /* Detect PD connection */ 1309 + if (anx7411_typec_check_connection(ctx)) 1310 + dev_err(dev, "check connection"); 1311 + 1312 + mutex_unlock(&ctx->lock); 1313 + 1314 + return 0; 1315 + } 1316 + 1317 + static const struct dev_pm_ops anx7411_pm_ops = { 1318 + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1319 + pm_runtime_force_resume) 1320 + SET_RUNTIME_PM_OPS(anx7411_runtime_pm_suspend, 1321 + anx7411_runtime_pm_resume, NULL) 1322 + }; 1323 + 1324 + static void anx7411_get_gpio_irq(struct anx7411_data *ctx) 1325 + { 1326 + struct device *dev = &ctx->tcpc_client->dev; 1327 + 1328 + ctx->intp_gpiod = devm_gpiod_get_optional(dev, "interrupt", GPIOD_IN); 1329 + if (!ctx->intp_gpiod) { 1330 + dev_err(dev, "no interrupt gpio property\n"); 1331 + return; 1332 + } 1333 + 1334 + ctx->intp_irq = gpiod_to_irq(ctx->intp_gpiod); 1335 + if (!ctx->intp_irq) 1336 + dev_err(dev, "failed to get GPIO IRQ\n"); 1337 + } 1338 + 1339 + static enum power_supply_usb_type anx7411_psy_usb_types[] = { 1340 + POWER_SUPPLY_USB_TYPE_C, 1341 + POWER_SUPPLY_USB_TYPE_PD, 1342 + POWER_SUPPLY_USB_TYPE_PD_PPS, 1343 + }; 1344 + 1345 + static enum power_supply_property anx7411_psy_props[] = { 1346 + POWER_SUPPLY_PROP_USB_TYPE, 1347 + POWER_SUPPLY_PROP_ONLINE, 1348 + POWER_SUPPLY_PROP_VOLTAGE_MIN, 1349 + POWER_SUPPLY_PROP_VOLTAGE_MAX, 1350 + POWER_SUPPLY_PROP_VOLTAGE_NOW, 1351 + POWER_SUPPLY_PROP_CURRENT_MAX, 1352 + POWER_SUPPLY_PROP_CURRENT_NOW, 1353 + }; 1354 + 1355 + static int anx7411_psy_set_prop(struct power_supply *psy, 1356 + enum power_supply_property psp, 1357 + const union power_supply_propval *val) 1358 + { 1359 + struct anx7411_data *ctx = power_supply_get_drvdata(psy); 1360 + int ret = 0; 1361 + 1362 + if (psp == POWER_SUPPLY_PROP_ONLINE) 1363 + ctx->psy_online = val->intval; 1364 + else 1365 + ret = -EINVAL; 1366 + 1367 + power_supply_changed(ctx->psy); 1368 + return ret; 1369 + } 1370 + 1371 + static int anx7411_psy_prop_writeable(struct power_supply *psy, 1372 + enum power_supply_property psp) 1373 + { 1374 + return psp == POWER_SUPPLY_PROP_ONLINE; 1375 + } 1376 + 1377 + static int anx7411_psy_get_prop(struct power_supply *psy, 1378 + enum power_supply_property psp, 1379 + union power_supply_propval *val) 1380 + { 1381 + struct anx7411_data *ctx = power_supply_get_drvdata(psy); 1382 + int ret = 0; 1383 + 1384 + switch (psp) { 1385 + case POWER_SUPPLY_PROP_USB_TYPE: 1386 + val->intval = ctx->usb_type; 1387 + break; 1388 + case POWER_SUPPLY_PROP_ONLINE: 1389 + val->intval = ctx->psy_online; 1390 + break; 1391 + case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1392 + case POWER_SUPPLY_PROP_VOLTAGE_MIN: 1393 + case POWER_SUPPLY_PROP_VOLTAGE_MAX: 1394 + val->intval = (ctx->psy_online) ? 1395 + ctx->typec.request_voltage * 1000 : 0; 1396 + break; 1397 + case POWER_SUPPLY_PROP_CURRENT_NOW: 1398 + case POWER_SUPPLY_PROP_CURRENT_MAX: 1399 + val->intval = (ctx->psy_online) ? 1400 + ctx->typec.request_current * 1000 : 0; 1401 + break; 1402 + default: 1403 + ret = -EINVAL; 1404 + break; 1405 + } 1406 + return ret; 1407 + } 1408 + 1409 + static int anx7411_psy_register(struct anx7411_data *ctx) 1410 + { 1411 + struct power_supply_desc *psy_desc = &ctx->psy_desc; 1412 + struct power_supply_config psy_cfg = {}; 1413 + char *psy_name; 1414 + 1415 + psy_name = devm_kasprintf(ctx->dev, GFP_KERNEL, "anx7411-source-psy-%s", 1416 + dev_name(ctx->dev)); 1417 + if (!psy_name) 1418 + return -ENOMEM; 1419 + 1420 + psy_desc->name = psy_name; 1421 + psy_desc->type = POWER_SUPPLY_TYPE_USB; 1422 + psy_desc->usb_types = anx7411_psy_usb_types; 1423 + psy_desc->num_usb_types = ARRAY_SIZE(anx7411_psy_usb_types); 1424 + psy_desc->properties = anx7411_psy_props, 1425 + psy_desc->num_properties = ARRAY_SIZE(anx7411_psy_props), 1426 + 1427 + psy_desc->get_property = anx7411_psy_get_prop, 1428 + psy_desc->set_property = anx7411_psy_set_prop, 1429 + psy_desc->property_is_writeable = anx7411_psy_prop_writeable, 1430 + 1431 + ctx->usb_type = POWER_SUPPLY_USB_TYPE_C; 1432 + ctx->psy = devm_power_supply_register(ctx->dev, psy_desc, &psy_cfg); 1433 + 1434 + if (IS_ERR(ctx->psy)) 1435 + dev_warn(ctx->dev, "unable to register psy\n"); 1436 + 1437 + return PTR_ERR_OR_ZERO(ctx->psy); 1438 + } 1439 + 1440 + static int anx7411_i2c_probe(struct i2c_client *client, 1441 + const struct i2c_device_id *id) 1442 + { 1443 + struct anx7411_data *plat; 1444 + struct device *dev = &client->dev; 1445 + int ret; 1446 + 1447 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) 1448 + return -ENODEV; 1449 + 1450 + plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL); 1451 + if (!plat) 1452 + return -ENOMEM; 1453 + 1454 + plat->tcpc_client = client; 1455 + i2c_set_clientdata(client, plat); 1456 + 1457 + mutex_init(&plat->lock); 1458 + 1459 + ret = anx7411_register_i2c_dummy_clients(plat, client); 1460 + if (ret) { 1461 + dev_err(dev, "fail to reserve I2C bus\n"); 1462 + return ret; 1463 + } 1464 + 1465 + ret = anx7411_typec_switch_probe(plat, dev); 1466 + if (ret) { 1467 + dev_err(dev, "fail to probe typec switch\n"); 1468 + goto free_i2c_dummy; 1469 + } 1470 + 1471 + ret = anx7411_typec_port_probe(plat, dev); 1472 + if (ret) { 1473 + dev_err(dev, "fail to probe typec property.\n"); 1474 + ret = -ENODEV; 1475 + goto free_typec_switch; 1476 + } 1477 + 1478 + plat->intp_irq = client->irq; 1479 + if (!client->irq) 1480 + anx7411_get_gpio_irq(plat); 1481 + 1482 + if (!plat->intp_irq) { 1483 + dev_err(dev, "fail to get interrupt IRQ\n"); 1484 + goto free_typec_port; 1485 + } 1486 + 1487 + plat->dev = dev; 1488 + plat->psy_online = ANX7411_PSY_OFFLINE; 1489 + if (anx7411_psy_register(plat)) { 1490 + dev_err(dev, "register psy\n"); 1491 + goto free_typec_port; 1492 + } 1493 + 1494 + INIT_WORK(&plat->work, anx7411_work_func); 1495 + plat->workqueue = alloc_workqueue("anx7411_work", 1496 + WQ_FREEZABLE | 1497 + WQ_MEM_RECLAIM, 1498 + 1); 1499 + if (!plat->workqueue) { 1500 + dev_err(dev, "fail to create work queue\n"); 1501 + ret = -ENOMEM; 1502 + goto free_typec_port; 1503 + } 1504 + 1505 + ret = devm_request_threaded_irq(dev, plat->intp_irq, 1506 + NULL, anx7411_intr_isr, 1507 + IRQF_TRIGGER_FALLING | 1508 + IRQF_ONESHOT, 1509 + "anx7411-intp", plat); 1510 + if (ret) { 1511 + dev_err(dev, "fail to request irq\n"); 1512 + goto free_wq; 1513 + } 1514 + 1515 + if (anx7411_typec_check_connection(plat)) 1516 + dev_err(dev, "check status\n"); 1517 + 1518 + pm_runtime_enable(dev); 1519 + 1520 + return 0; 1521 + 1522 + free_wq: 1523 + destroy_workqueue(plat->workqueue); 1524 + 1525 + free_typec_port: 1526 + typec_unregister_port(plat->typec.port); 1527 + anx7411_port_unregister_altmodes(plat->typec.port_amode); 1528 + 1529 + free_typec_switch: 1530 + anx7411_unregister_switch(plat); 1531 + anx7411_unregister_mux(plat); 1532 + 1533 + free_i2c_dummy: 1534 + i2c_unregister_device(plat->spi_client); 1535 + 1536 + return ret; 1537 + } 1538 + 1539 + static int anx7411_i2c_remove(struct i2c_client *client) 1540 + { 1541 + struct anx7411_data *plat = i2c_get_clientdata(client); 1542 + 1543 + anx7411_partner_unregister_altmode(plat); 1544 + anx7411_unregister_partner(plat); 1545 + 1546 + if (plat->workqueue) 1547 + destroy_workqueue(plat->workqueue); 1548 + 1549 + if (plat->spi_client) 1550 + i2c_unregister_device(plat->spi_client); 1551 + 1552 + if (plat->typec.role_sw) 1553 + usb_role_switch_put(plat->typec.role_sw); 1554 + 1555 + anx7411_unregister_mux(plat); 1556 + 1557 + anx7411_unregister_switch(plat); 1558 + 1559 + if (plat->typec.port) 1560 + typec_unregister_port(plat->typec.port); 1561 + 1562 + anx7411_port_unregister_altmodes(plat->typec.port_amode); 1563 + 1564 + return 0; 1565 + } 1566 + 1567 + static const struct i2c_device_id anx7411_id[] = { 1568 + {"anx7411", 0}, 1569 + {} 1570 + }; 1571 + 1572 + MODULE_DEVICE_TABLE(i2c, anx7411_id); 1573 + 1574 + static const struct of_device_id anx_match_table[] = { 1575 + {.compatible = "analogix,anx7411",}, 1576 + {}, 1577 + }; 1578 + 1579 + static struct i2c_driver anx7411_driver = { 1580 + .driver = { 1581 + .name = "anx7411", 1582 + .of_match_table = anx_match_table, 1583 + .pm = &anx7411_pm_ops, 1584 + }, 1585 + .probe = anx7411_i2c_probe, 1586 + .remove = anx7411_i2c_remove, 1587 + 1588 + .id_table = anx7411_id, 1589 + }; 1590 + 1591 + module_i2c_driver(anx7411_driver); 1592 + 1593 + MODULE_DESCRIPTION("Anx7411 USB Type-C PD driver"); 1594 + MODULE_AUTHOR("Xin Ji <xji@analogixsemi.com>"); 1595 + MODULE_LICENSE("GPL"); 1596 + MODULE_VERSION("0.1.5");