Linux Audio

Check our new training course

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