1 // SPDX-License-Identifier: GPL-2.0-only
4 * Driver for Analogix ANX7411 USB Type-C and PD controller
6 * Copyright(c) 2022, Analogix Semiconductor. All rights reserved.
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>
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
40 struct anx7411_i2c_select {
45 #define VID_ANALOGIX 0x1F29
46 #define PID_ANALOGIX 0x7411
48 /* TCPC register define */
50 #define ANALOG_CTRL_10 0xAA
54 #define RECEIVED_MSG BIT(7)
55 #define SOFTWARE_INT BIT(6)
58 #define MSG_HEADER 0x00
60 #define MSG_RAWDATA 0x02
61 #define MSG_LEN_MASK 0x1F
64 #define INTP_POW_ON BIT(7)
65 #define INTP_POW_OFF BIT(6)
67 #define VBUS_THRESHOLD_H 0xDD
68 #define VBUS_THRESHOLD_L 0xDE
70 #define FW_CTRL_0 0xF0
71 #define UNSTRUCT_VDM_EN BIT(0)
72 #define DELAY_200MS BIT(1)
76 #define VSAFE3 (BIT(2) | BIT(3))
80 #define DONGLE_IOP BIT(0)
82 #define FW_CTRL_2 0xF7
83 #define SINK_CTRL_DIS_FLAG BIT(5)
85 /* SPI register define */
86 #define OCM_CTRL_0 0x6E
87 #define OCM_RESET BIT(6)
89 #define MAX_VOLTAGE 0xAC
90 #define MAX_POWER 0xAD
91 #define MIN_POWER 0xAE
93 #define REQUEST_VOLTAGE 0xAF
94 #define VOLTAGE_UNIT 100 /* mV per unit */
96 #define REQUEST_CURRENT 0xB1
97 #define CURRENT_UNIT 50 /* mA per unit */
99 #define CMD_SEND_BUF 0xC0
100 #define CMD_RECV_BUF 0xE0
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
107 #define DEF_1_5A 1500
109 #define LOBYTE(w) ((u8)((w) & 0xFF))
110 #define HIBYTE(w) ((u8)(((u16)(w) >> 8) & 0xFF))
112 enum anx7411_typec_message_type {
115 TYPE_SNK_IDENTITY = 0x02,
117 TYPE_SET_SNK_DP_CAP = 0x08,
118 TYPE_PSWAP_REQ = 0x10,
119 TYPE_DSWAP_REQ = 0x11,
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,
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)
137 #define FW_SUBVER 0xB5
139 #define INT_MASK 0xB6
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)
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)
166 #define POWER_SOURCE 1
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)
178 #define PD_REV_INIT 0xBA
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)
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
203 #define DP_SVID 0xFF01
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
215 #define MAX_ALTMODE 2
217 #define HAS_SOURCE_CAP BIT(0)
218 #define HAS_SINK_CAP BIT(1)
219 #define HAS_SINK_WATT BIT(2)
221 enum anx7411_psy_state {
222 /* copy from drivers/usb/typec/tcpm */
223 ANX7411_PSY_OFFLINE = 0,
224 ANX7411_PSY_FIXED_ONLINE,
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
231 ANX7411_PSY_HANG = 0xff,
234 struct typec_params {
235 int request_current; /* ma */
236 int request_voltage; /* mv */
238 int cc_orientation_valid;
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;
254 struct typec_capability caps;
255 u32 src_pdo[PDO_MAX_OBJECTS];
256 u32 sink_pdo[PDO_MAX_OBJECTS];
264 #define MAX_BUF_LEN 30
271 struct anx7411_data {
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;
282 struct work_struct work;
283 struct workqueue_struct *workqueue;
284 /* Lock for interrupt work queue */
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;
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 */
300 static u8 dp_caps[4] = {0xC6, 0x00, 0x00, 0x00};
302 static int anx7411_reg_read(struct i2c_client *client,
305 return i2c_smbus_read_byte_data(client, reg_addr);
308 static int anx7411_reg_block_read(struct i2c_client *client,
309 u8 reg_addr, u8 len, u8 *buf)
311 return i2c_smbus_read_i2c_block_data(client, reg_addr, len, buf);
314 static int anx7411_reg_write(struct i2c_client *client,
315 u8 reg_addr, u8 reg_val)
317 return i2c_smbus_write_byte_data(client, reg_addr, reg_val);
320 static int anx7411_reg_block_write(struct i2c_client *client,
321 u8 reg_addr, u8 len, u8 *buf)
323 return i2c_smbus_write_i2c_block_data(client, reg_addr, len, buf);
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},
333 static int anx7411_detect_power_mode(struct anx7411_data *ctx)
338 ret = anx7411_reg_read(ctx->spi_client, REQUEST_CURRENT);
342 ctx->typec.request_current = ret * CURRENT_UNIT; /* 50ma per unit */
344 ret = anx7411_reg_read(ctx->spi_client, REQUEST_VOLTAGE);
348 ctx->typec.request_voltage = ret * VOLTAGE_UNIT; /* 100mv per unit */
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);
356 if (!ctx->typec.cc_orientation_valid)
359 if (ctx->typec.cc_connect == CC1_CONNECTED)
360 mode = CC1_RP(ctx->typec.cc_status);
362 mode = CC2_RP(ctx->typec.cc_status);
364 typec_set_pwr_opmode(ctx->typec.port, mode - 1);
368 typec_set_pwr_opmode(ctx->typec.port, TYPEC_PWR_MODE_PD);
373 static int anx7411_register_partner(struct anx7411_data *ctx,
374 int pd, int accessory)
376 struct typec_partner_desc desc;
377 struct typec_partner *partner;
379 if (ctx->typec.partner)
383 desc.accessory = accessory;
384 desc.identity = NULL;
385 partner = typec_register_partner(ctx->typec.port, &desc);
387 return PTR_ERR(partner);
389 ctx->typec.partner = partner;
394 static int anx7411_detect_cc_orientation(struct anx7411_data *ctx)
396 struct device *dev = &ctx->spi_client->dev;
402 ret = anx7411_reg_read(ctx->spi_client, CC_STATUS);
406 ctx->typec.cc_status = ret;
408 cc1_rd = ret & CC1_RD ? 1 : 0;
409 cc2_rd = ret & CC2_RD ? 1 : 0;
410 cc1_ra = ret & CC1_RA ? 1 : 0;
411 cc2_ra = ret & CC2_RA ? 1 : 0;
412 cc1_rp = CC1_RP(ret);
413 cc2_rp = CC2_RP(ret);
415 /* Debug cable, nothing to do */
416 if (cc1_rd && cc2_rd) {
417 ctx->typec.cc_orientation_valid = 0;
418 return anx7411_register_partner(ctx, 0, TYPEC_ACCESSORY_DEBUG);
421 if (cc1_ra && cc2_ra) {
422 ctx->typec.cc_orientation_valid = 0;
423 return anx7411_register_partner(ctx, 0, TYPEC_ACCESSORY_AUDIO);
426 ctx->typec.cc_orientation_valid = 1;
428 ret = anx7411_register_partner(ctx, 1, TYPEC_ACCESSORY_NONE);
430 dev_err(dev, "register partner\n");
434 if (cc1_rd || cc1_rp) {
435 typec_set_orientation(ctx->typec.port, TYPEC_ORIENTATION_NORMAL);
436 ctx->typec.cc_connect = CC1_CONNECTED;
439 if (cc2_rd || cc2_rp) {
440 typec_set_orientation(ctx->typec.port, TYPEC_ORIENTATION_REVERSE);
441 ctx->typec.cc_connect = CC2_CONNECTED;
447 static int anx7411_set_mux(struct anx7411_data *ctx, int pin_assignment)
449 int mode = TYPEC_STATE_SAFE;
451 switch (pin_assignment) {
452 case SELECT_PIN_ASSIGMENT_U:
453 /* default 4 line USB 3.1 */
454 mode = TYPEC_STATE_MODAL;
456 case SELECT_PIN_ASSIGMENT_C:
457 case SELECT_PIN_ASSIGMENT_E:
459 mode = TYPEC_STATE_SAFE;
461 case SELECT_PIN_ASSIGMENT_D:
462 /* 2 line DP, 2 line USB */
463 mode = TYPEC_MODE_USB3;
466 mode = TYPEC_STATE_SAFE;
470 ctx->typec.pin_assignment = pin_assignment;
472 return typec_set_mode(ctx->typec.port, mode);
475 static int anx7411_set_usb_role(struct anx7411_data *ctx, enum usb_role role)
477 if (!ctx->typec.role_sw)
480 return usb_role_switch_set_role(ctx->typec.role_sw, role);
483 static int anx7411_data_role_detect(struct anx7411_data *ctx)
487 ret = anx7411_reg_read(ctx->spi_client, SYSTEM_STSTUS);
491 ctx->typec.data_role = (ret & DATA_ROLE) ? TYPEC_HOST : TYPEC_DEVICE;
492 ctx->typec.vconn_role = (ret & VCONN_STATUS) ? TYPEC_SOURCE : TYPEC_SINK;
494 typec_set_data_role(ctx->typec.port, ctx->typec.data_role);
496 typec_set_vconn_role(ctx->typec.port, ctx->typec.vconn_role);
498 if (ctx->typec.data_role == TYPEC_HOST)
499 return anx7411_set_usb_role(ctx, USB_ROLE_HOST);
501 return anx7411_set_usb_role(ctx, USB_ROLE_DEVICE);
504 static int anx7411_power_role_detect(struct anx7411_data *ctx)
508 ret = anx7411_reg_read(ctx->spi_client, SYSTEM_STSTUS);
512 ctx->typec.power_role = (ret & SINK_STATUS) ? TYPEC_SINK : TYPEC_SOURCE;
514 if (ctx->typec.power_role == TYPEC_SOURCE) {
515 ctx->typec.request_current = DEF_1_5A;
516 ctx->typec.request_voltage = DEF_5V;
519 typec_set_pwr_role(ctx->typec.port, ctx->typec.power_role);
524 static int anx7411_cc_status_detect(struct anx7411_data *ctx)
526 anx7411_detect_cc_orientation(ctx);
527 anx7411_detect_power_mode(ctx);
532 static void anx7411_partner_unregister_altmode(struct anx7411_data *ctx)
536 ctx->typec.dp_altmode_enter = 0;
537 ctx->typec.cust_altmode_enter = 0;
539 for (i = 0; i < MAX_ALTMODE; i++)
540 if (ctx->typec.amode[i]) {
541 typec_unregister_altmode(ctx->typec.amode[i]);
542 ctx->typec.amode[i] = NULL;
545 ctx->typec.pin_assignment = 0;
548 static int anx7411_typec_register_altmode(struct anx7411_data *ctx,
551 struct device *dev = &ctx->spi_client->dev;
552 struct typec_altmode_desc desc;
559 for (i = 0; i < MAX_ALTMODE; i++)
560 if (!ctx->typec.amode[i])
563 desc.mode = i + 1; /* start with 1 */
565 if (i >= MAX_ALTMODE) {
566 dev_err(dev, "no altmode space for registering\n");
570 ctx->typec.amode[i] = typec_partner_register_altmode(ctx->typec.partner,
572 if (IS_ERR(ctx->typec.amode[i])) {
573 dev_err(dev, "failed to register altmode\n");
574 err = PTR_ERR(ctx->typec.amode[i]);
575 ctx->typec.amode[i] = NULL;
582 static void anx7411_unregister_partner(struct anx7411_data *ctx)
584 if (ctx->typec.partner) {
585 typec_unregister_partner(ctx->typec.partner);
586 ctx->typec.partner = NULL;
590 static int anx7411_update_altmode(struct anx7411_data *ctx, int svid)
595 ctx->typec.dp_altmode_enter = 1;
597 ctx->typec.cust_altmode_enter = 1;
599 for (i = 0; i < MAX_ALTMODE; i++) {
600 if (!ctx->typec.amode[i])
603 if (ctx->typec.amode[i]->svid == svid) {
604 typec_altmode_update_active(ctx->typec.amode[i], true);
605 typec_altmode_notify(ctx->typec.amode[i],
606 ctx->typec.pin_assignment,
615 static int anx7411_register_altmode(struct anx7411_data *ctx,
616 bool dp_altmode, u8 *buf)
622 if (!ctx->typec.partner)
627 mid = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
629 return anx7411_typec_register_altmode(ctx, svid, mid);
632 svid = (buf[3] << 8) | buf[2];
633 if ((buf[0] & VDM_CMD_AND_ACK_MASK) != (VDM_ACK | VDM_CMD_ENTER_MODE))
634 return anx7411_update_altmode(ctx, svid);
636 if ((buf[0] & VDM_CMD_AND_ACK_MASK) != (VDM_ACK | VDM_CMD_DIS_MOD))
639 mid = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24);
641 ret = anx7411_typec_register_altmode(ctx, svid, mid);
642 if (ctx->typec.cust_altmode_enter)
643 ret |= anx7411_update_altmode(ctx, svid);
648 static int anx7411_parse_cmd(struct anx7411_data *ctx, u8 type, u8 *buf, u8 len)
650 struct device *dev = &ctx->spi_client->dev;
651 u8 cur_50ma, vol_100mv;
655 cur_50ma = anx7411_reg_read(ctx->spi_client, REQUEST_CURRENT);
656 vol_100mv = anx7411_reg_read(ctx->spi_client, REQUEST_VOLTAGE);
658 ctx->typec.request_voltage = vol_100mv * VOLTAGE_UNIT;
659 ctx->typec.request_current = cur_50ma * CURRENT_UNIT;
661 ctx->psy_online = ANX7411_PSY_FIXED_ONLINE;
662 ctx->usb_type = POWER_SUPPLY_USB_TYPE_PD;
663 power_supply_changed(ctx->psy);
669 case TYPE_SNK_IDENTITY:
671 case TYPE_GET_DP_ALT_ENTER:
672 /* DP alt mode enter success */
674 anx7411_update_altmode(ctx, DP_SVID);
676 case TYPE_DP_ALT_ENTER:
677 /* Update DP altmode */
678 anx7411_update_altmode(ctx, DP_SVID);
681 anx7411_detect_power_mode(ctx);
683 case TYPE_DP_CONFIGURE:
684 anx7411_set_mux(ctx, buf[1]);
686 case TYPE_DP_DISCOVER_MODES_INFO:
687 /* Make sure discover modes valid */
689 /* Register DP Altmode */
690 anx7411_register_altmode(ctx, 1, buf);
693 /* Register other altmode */
694 anx7411_register_altmode(ctx, 0, buf);
697 dev_err(dev, "ignore message(0x%.02x).\n", type);
704 static u8 checksum(struct device *dev, u8 *buf, u8 len)
709 for (i = 0; i < len; i++)
715 static int anx7411_read_msg_ctrl_status(struct i2c_client *client)
717 return anx7411_reg_read(client, CMD_SEND_BUF);
720 static int anx7411_wait_msg_empty(struct i2c_client *client)
724 return readx_poll_timeout(anx7411_read_msg_ctrl_status,
725 client, val, (val < 0) || (val == 0),
729 static int anx7411_send_msg(struct anx7411_data *ctx, u8 type, u8 *buf, u8 size)
731 struct device *dev = &ctx->spi_client->dev;
732 struct fw_msg *msg = &ctx->send_msg;
736 size = min_t(u8, size, (u8)MAX_BUF_LEN);
737 memcpy(msg->buf, buf, size);
738 msg->msg_type = type;
739 /* msg len equals buffer length + msg_type */
740 msg->msg_len = size + 1;
742 /* Do CRC check for all buffer data and msg_len and msg_type */
743 crc = checksum(dev, (u8 *)msg, size + HEADER_LEN);
744 msg->buf[size] = 0 - crc;
746 ret = anx7411_wait_msg_empty(ctx->spi_client);
750 ret = anx7411_reg_block_write(ctx->spi_client,
751 CMD_SEND_BUF + 1, size + HEADER_LEN,
753 ret |= anx7411_reg_write(ctx->spi_client, CMD_SEND_BUF,
758 static int anx7411_process_cmd(struct anx7411_data *ctx)
760 struct device *dev = &ctx->spi_client->dev;
761 struct fw_msg *msg = &ctx->recv_msg;
766 /* Read message from firmware */
767 ret = anx7411_reg_block_read(ctx->spi_client, CMD_RECV_BUF,
775 ret = anx7411_reg_write(ctx->spi_client, CMD_RECV_BUF, 0);
779 len = msg->msg_len & MSG_LEN_MASK;
780 crc = checksum(dev, (u8 *)msg, len + HEADER_LEN);
782 dev_err(dev, "message error crc(0x%.02x)\n", crc);
786 return anx7411_parse_cmd(ctx, msg->msg_type, msg->buf, len - 1);
789 static void anx7411_translate_payload(struct device *dev, __le32 *payload,
790 u32 *pdo, int nr, const char *type)
794 if (nr > PDO_MAX_OBJECTS) {
795 dev_err(dev, "nr(%d) exceed PDO_MAX_OBJECTS(%d)\n",
796 nr, PDO_MAX_OBJECTS);
801 for (i = 0; i < nr; i++)
802 payload[i] = cpu_to_le32(pdo[i]);
805 static int anx7411_config(struct anx7411_data *ctx)
807 struct device *dev = &ctx->spi_client->dev;
808 struct typec_params *typecp = &ctx->typec;
809 __le32 payload[PDO_MAX_OBJECTS];
812 /* Config PD FW work under PD 2.0 */
813 ret = anx7411_reg_write(ctx->spi_client, PD_REV_INIT, PD_REV20);
814 ret |= anx7411_reg_write(ctx->tcpc_client, FW_CTRL_0,
815 UNSTRUCT_VDM_EN | DELAY_200MS |
817 ret |= anx7411_reg_write(ctx->spi_client, FW_CTRL_1,
818 AUTO_PD_EN | FORCE_SEND_RDO);
820 /* Set VBUS current threshold */
821 ret |= anx7411_reg_write(ctx->tcpc_client, VBUS_THRESHOLD_H, 0xff);
822 ret |= anx7411_reg_write(ctx->tcpc_client, VBUS_THRESHOLD_L, 0x03);
824 /* Fix dongle compatible issue */
825 ret |= anx7411_reg_write(ctx->tcpc_client, FW_PARAM,
826 anx7411_reg_read(ctx->tcpc_client, FW_PARAM) |
828 ret |= anx7411_reg_write(ctx->spi_client, INT_MASK, 0);
830 ret |= anx7411_reg_write(ctx->spi_client, PD_EXT_MSG_CTRL, 0xFF);
834 if (typecp->caps_flags & HAS_SOURCE_CAP) {
835 anx7411_translate_payload(dev, payload, typecp->src_pdo,
836 typecp->src_pdo_nr, "source");
837 anx7411_send_msg(ctx, TYPE_SRC_CAP, (u8 *)&payload,
838 typecp->src_pdo_nr * 4);
839 anx7411_send_msg(ctx, TYPE_SNK_IDENTITY, snk_identity,
840 sizeof(snk_identity));
841 anx7411_send_msg(ctx, TYPE_SET_SNK_DP_CAP, dp_caps,
845 if (typecp->caps_flags & HAS_SINK_CAP) {
846 anx7411_translate_payload(dev, payload, typecp->sink_pdo,
847 typecp->sink_pdo_nr, "sink");
848 anx7411_send_msg(ctx, TYPE_SNK_CAP, (u8 *)&payload,
849 typecp->sink_pdo_nr * 4);
852 if (typecp->caps_flags & HAS_SINK_WATT) {
853 if (typecp->sink_watt) {
854 ret |= anx7411_reg_write(ctx->spi_client, MAX_POWER,
856 /* Set min power to 1W */
857 ret |= anx7411_reg_write(ctx->spi_client, MIN_POWER, 2);
860 if (typecp->sink_voltage)
861 ret |= anx7411_reg_write(ctx->spi_client, MAX_VOLTAGE,
862 typecp->sink_voltage);
867 if (!typecp->caps_flags)
868 usleep_range(5000, 6000);
870 ctx->fw_version = anx7411_reg_read(ctx->spi_client, FW_VER);
871 ctx->fw_subversion = anx7411_reg_read(ctx->spi_client, FW_SUBVER);
876 static void anx7411_chip_standby(struct anx7411_data *ctx)
880 struct device *dev = &ctx->spi_client->dev;
882 ret = anx7411_reg_write(ctx->spi_client, OCM_CTRL_0,
883 anx7411_reg_read(ctx->spi_client, OCM_CTRL_0) |
885 ret |= anx7411_reg_write(ctx->tcpc_client, ANALOG_CTRL_10, 0x80);
886 /* Set TCPC to RD and DRP enable */
887 cc1 = TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT;
888 cc2 = TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT;
889 ret |= anx7411_reg_write(ctx->tcpc_client, TCPC_ROLE_CTRL,
890 TCPC_ROLE_CTRL_DRP | cc1 | cc2);
892 /* Send DRP toggle command */
893 ret |= anx7411_reg_write(ctx->tcpc_client, TCPC_COMMAND,
894 TCPC_CMD_LOOK4CONNECTION);
896 /* Send TCPC enter standby command */
897 ret |= anx7411_reg_write(ctx->tcpc_client,
898 TCPC_COMMAND, TCPC_CMD_I2C_IDLE);
900 dev_err(dev, "Chip standby failed\n");
903 static void anx7411_work_func(struct work_struct *work)
907 u8 int_change; /* Interrupt change */
908 u8 int_status; /* Firmware status update */
909 u8 alert0, alert1; /* Interrupt alert source */
910 struct anx7411_data *ctx = container_of(work, struct anx7411_data, work);
911 struct device *dev = &ctx->spi_client->dev;
913 mutex_lock(&ctx->lock);
915 /* Read interrupt change status */
916 ret = anx7411_reg_block_read(ctx->spi_client, INT_STS, STATUS_LEN, buf);
918 /* Power standby mode, just return */
924 /* Read alert register */
925 ret = anx7411_reg_block_read(ctx->tcpc_client, ALERT_0, STATUS_LEN, buf);
932 /* Clear interrupt and alert status */
933 ret = anx7411_reg_write(ctx->spi_client, INT_STS, 0);
934 ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_0, alert0);
935 ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_1, alert1);
939 if (alert1 & INTP_POW_OFF) {
940 anx7411_partner_unregister_altmode(ctx);
941 if (anx7411_set_usb_role(ctx, USB_ROLE_NONE))
942 dev_err(dev, "Set usb role\n");
943 anx7411_unregister_partner(ctx);
944 ctx->psy_online = ANX7411_PSY_OFFLINE;
945 ctx->usb_type = POWER_SUPPLY_USB_TYPE_C;
946 ctx->typec.request_voltage = 0;
947 ctx->typec.request_current = 0;
948 power_supply_changed(ctx->psy);
949 anx7411_chip_standby(ctx);
953 if ((alert0 & SOFTWARE_INT) && (int_change & OCM_BOOT_UP)) {
954 if (anx7411_config(ctx))
955 dev_err(dev, "Config failed\n");
956 if (anx7411_data_role_detect(ctx))
957 dev_err(dev, "set PD data role\n");
958 if (anx7411_power_role_detect(ctx))
959 dev_err(dev, "set PD power role\n");
960 anx7411_set_mux(ctx, SELECT_PIN_ASSIGMENT_C);
963 if (alert0 & RECEIVED_MSG)
964 anx7411_process_cmd(ctx);
966 ret = (int_status & DATA_ROLE) ? TYPEC_HOST : TYPEC_DEVICE;
967 if (ctx->typec.data_role != ret)
968 if (anx7411_data_role_detect(ctx))
969 dev_err(dev, "set PD data role\n");
971 ret = (int_status & SINK_STATUS) ? TYPEC_SINK : TYPEC_SOURCE;
972 if (ctx->typec.power_role != ret)
973 if (anx7411_power_role_detect(ctx))
974 dev_err(dev, "set PD power role\n");
976 if ((alert0 & SOFTWARE_INT) && (int_change & CC_STATUS_CHANGE))
977 anx7411_cc_status_detect(ctx);
980 mutex_unlock(&ctx->lock);
983 static irqreturn_t anx7411_intr_isr(int irq, void *data)
985 struct anx7411_data *ctx = (struct anx7411_data *)data;
987 queue_work(ctx->workqueue, &ctx->work);
992 static int anx7411_register_i2c_dummy_clients(struct anx7411_data *ctx,
993 struct i2c_client *client)
998 for (i = 0; i < ARRAY_SIZE(anx7411_i2c_addr); i++) {
999 if (client->addr == (anx7411_i2c_addr[i].tcpc_address >> 1)) {
1000 spi_addr = anx7411_i2c_addr[i].spi_address >> 1;
1001 ctx->spi_client = i2c_new_dummy_device(client->adapter,
1003 if (!IS_ERR(ctx->spi_client))
1008 dev_err(&client->dev, "unable to get SPI slave\n");
1012 static void anx7411_port_unregister_altmodes(struct typec_altmode **adev)
1016 for (i = 0; i < MAX_ALTMODE; i++)
1018 typec_unregister_altmode(adev[i]);
1023 static int anx7411_usb_mux_set(struct typec_mux_dev *mux,
1024 struct typec_mux_state *state)
1026 struct anx7411_data *ctx = typec_mux_get_drvdata(mux);
1027 struct device *dev = &ctx->spi_client->dev;
1030 has_dp = (state->alt && state->alt->svid == USB_TYPEC_DP_SID &&
1031 state->alt->mode == USB_TYPEC_DP_MODE);
1033 dev_err(dev, "dp altmode not register\n");
1038 static int anx7411_usb_set_orientation(struct typec_switch_dev *sw,
1039 enum typec_orientation orientation)
1046 static int anx7411_register_switch(struct anx7411_data *ctx,
1048 struct fwnode_handle *fwnode)
1050 struct typec_switch_desc sw_desc = { };
1052 sw_desc.fwnode = fwnode;
1053 sw_desc.drvdata = ctx;
1054 sw_desc.name = fwnode_get_name(fwnode);
1055 sw_desc.set = anx7411_usb_set_orientation;
1057 ctx->typec.typec_switch = typec_switch_register(dev, &sw_desc);
1058 if (IS_ERR(ctx->typec.typec_switch)) {
1059 dev_err(dev, "switch register failed\n");
1060 return PTR_ERR(ctx->typec.typec_switch);
1066 static int anx7411_register_mux(struct anx7411_data *ctx,
1068 struct fwnode_handle *fwnode)
1070 struct typec_mux_desc mux_desc = { };
1072 mux_desc.fwnode = fwnode;
1073 mux_desc.drvdata = ctx;
1074 mux_desc.name = fwnode_get_name(fwnode);
1075 mux_desc.set = anx7411_usb_mux_set;
1077 ctx->typec.typec_mux = typec_mux_register(dev, &mux_desc);
1078 if (IS_ERR(ctx->typec.typec_mux)) {
1079 dev_err(dev, "mux register failed\n");
1080 return PTR_ERR(ctx->typec.typec_mux);
1086 static void anx7411_unregister_mux(struct anx7411_data *ctx)
1088 if (ctx->typec.typec_mux) {
1089 typec_mux_unregister(ctx->typec.typec_mux);
1090 ctx->typec.typec_mux = NULL;
1094 static void anx7411_unregister_switch(struct anx7411_data *ctx)
1096 if (ctx->typec.typec_switch) {
1097 typec_switch_unregister(ctx->typec.typec_switch);
1098 ctx->typec.typec_switch = NULL;
1102 static int anx7411_typec_switch_probe(struct anx7411_data *ctx,
1106 struct device_node *node;
1108 node = of_get_child_by_name(dev->of_node, "orientation_switch");
1112 ret = anx7411_register_switch(ctx, dev, &node->fwnode);
1114 dev_err(dev, "failed register switch");
1118 node = of_get_child_by_name(dev->of_node, "mode_switch");
1120 dev_err(dev, "no typec mux exist");
1122 goto unregister_switch;
1125 ret = anx7411_register_mux(ctx, dev, &node->fwnode);
1127 dev_err(dev, "failed register mode switch");
1129 goto unregister_switch;
1135 anx7411_unregister_switch(ctx);
1140 static int anx7411_typec_port_probe(struct anx7411_data *ctx,
1143 struct typec_capability *cap = &ctx->typec.caps;
1144 struct typec_params *typecp = &ctx->typec;
1145 struct fwnode_handle *fwnode;
1149 fwnode = device_get_named_child_node(dev, "connector");
1153 ret = fwnode_property_read_string(fwnode, "power-role", &buf);
1155 dev_err(dev, "power-role not found: %d\n", ret);
1159 ret = typec_find_port_power_role(buf);
1164 ret = fwnode_property_read_string(fwnode, "data-role", &buf);
1166 dev_err(dev, "data-role not found: %d\n", ret);
1170 ret = typec_find_port_data_role(buf);
1175 ret = fwnode_property_read_string(fwnode, "try-power-role", &buf);
1177 dev_err(dev, "try-power-role not found: %d\n", ret);
1181 ret = typec_find_power_role(buf);
1184 cap->prefer_role = ret;
1186 /* Get source pdos */
1187 ret = fwnode_property_count_u32(fwnode, "source-pdos");
1189 typecp->src_pdo_nr = min_t(u8, ret, PDO_MAX_OBJECTS);
1190 ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
1192 typecp->src_pdo_nr);
1194 dev_err(dev, "source cap validate failed: %d\n", ret);
1198 typecp->caps_flags |= HAS_SOURCE_CAP;
1201 ret = fwnode_property_count_u32(fwnode, "sink-pdos");
1203 typecp->sink_pdo_nr = min_t(u8, ret, PDO_MAX_OBJECTS);
1204 ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
1206 typecp->sink_pdo_nr);
1208 dev_err(dev, "sink cap validate failed: %d\n", ret);
1212 for (i = 0; i < typecp->sink_pdo_nr; i++) {
1214 switch (pdo_type(typecp->sink_pdo[i])) {
1215 case PDO_TYPE_FIXED:
1216 ret = pdo_fixed_voltage(typecp->sink_pdo[i]);
1220 ret = pdo_max_voltage(typecp->sink_pdo[i]);
1228 /* 100mv per unit */
1229 typecp->sink_voltage = max(5000, ret) / 100;
1232 typecp->caps_flags |= HAS_SINK_CAP;
1235 if (!fwnode_property_read_u32(fwnode, "op-sink-microwatt", &ret)) {
1236 typecp->sink_watt = ret / 500000; /* 500mw per unit */
1237 typecp->caps_flags |= HAS_SINK_WATT;
1240 cap->fwnode = fwnode;
1242 ctx->typec.role_sw = usb_role_switch_get(dev);
1243 if (IS_ERR(ctx->typec.role_sw)) {
1244 dev_err(dev, "USB role switch not found.\n");
1245 ctx->typec.role_sw = NULL;
1248 ctx->typec.port = typec_register_port(dev, cap);
1249 if (IS_ERR(ctx->typec.port)) {
1250 ret = PTR_ERR(ctx->typec.port);
1251 ctx->typec.port = NULL;
1252 dev_err(dev, "Failed to register type c port %d\n", ret);
1256 typec_port_register_altmodes(ctx->typec.port, NULL, ctx,
1257 ctx->typec.port_amode,
1262 static int anx7411_typec_check_connection(struct anx7411_data *ctx)
1266 ret = anx7411_reg_read(ctx->spi_client, FW_VER);
1268 return 0; /* No device attached in typec port */
1270 /* Clear interrupt and alert status */
1271 ret = anx7411_reg_write(ctx->spi_client, INT_STS, 0);
1272 ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_0, 0xFF);
1273 ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_1, 0xFF);
1277 ret = anx7411_cc_status_detect(ctx);
1278 ret |= anx7411_power_role_detect(ctx);
1279 ret |= anx7411_data_role_detect(ctx);
1280 ret |= anx7411_set_mux(ctx, SELECT_PIN_ASSIGMENT_C);
1284 ret = anx7411_send_msg(ctx, TYPE_GET_DP_ALT_ENTER, NULL, 0);
1285 ret |= anx7411_send_msg(ctx, TYPE_GET_DP_DISCOVER_MODES_INFO, NULL, 0);
1290 static int __maybe_unused anx7411_runtime_pm_suspend(struct device *dev)
1292 struct anx7411_data *ctx = dev_get_drvdata(dev);
1294 mutex_lock(&ctx->lock);
1296 anx7411_partner_unregister_altmode(ctx);
1298 if (ctx->typec.partner)
1299 anx7411_unregister_partner(ctx);
1301 mutex_unlock(&ctx->lock);
1306 static int __maybe_unused anx7411_runtime_pm_resume(struct device *dev)
1308 struct anx7411_data *ctx = dev_get_drvdata(dev);
1310 mutex_lock(&ctx->lock);
1311 /* Detect PD connection */
1312 if (anx7411_typec_check_connection(ctx))
1313 dev_err(dev, "check connection");
1315 mutex_unlock(&ctx->lock);
1320 static const struct dev_pm_ops anx7411_pm_ops = {
1321 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1322 pm_runtime_force_resume)
1323 SET_RUNTIME_PM_OPS(anx7411_runtime_pm_suspend,
1324 anx7411_runtime_pm_resume, NULL)
1327 static void anx7411_get_gpio_irq(struct anx7411_data *ctx)
1329 struct device *dev = &ctx->tcpc_client->dev;
1331 ctx->intp_gpiod = devm_gpiod_get_optional(dev, "interrupt", GPIOD_IN);
1332 if (IS_ERR_OR_NULL(ctx->intp_gpiod)) {
1333 dev_err(dev, "no interrupt gpio property\n");
1337 ctx->intp_irq = gpiod_to_irq(ctx->intp_gpiod);
1338 if (ctx->intp_irq < 0)
1339 dev_err(dev, "failed to get GPIO IRQ\n");
1342 static enum power_supply_usb_type anx7411_psy_usb_types[] = {
1343 POWER_SUPPLY_USB_TYPE_C,
1344 POWER_SUPPLY_USB_TYPE_PD,
1345 POWER_SUPPLY_USB_TYPE_PD_PPS,
1348 static enum power_supply_property anx7411_psy_props[] = {
1349 POWER_SUPPLY_PROP_USB_TYPE,
1350 POWER_SUPPLY_PROP_ONLINE,
1351 POWER_SUPPLY_PROP_VOLTAGE_MIN,
1352 POWER_SUPPLY_PROP_VOLTAGE_MAX,
1353 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1354 POWER_SUPPLY_PROP_CURRENT_MAX,
1355 POWER_SUPPLY_PROP_CURRENT_NOW,
1358 static int anx7411_psy_set_prop(struct power_supply *psy,
1359 enum power_supply_property psp,
1360 const union power_supply_propval *val)
1362 struct anx7411_data *ctx = power_supply_get_drvdata(psy);
1365 if (psp == POWER_SUPPLY_PROP_ONLINE)
1366 ctx->psy_online = val->intval;
1370 power_supply_changed(ctx->psy);
1374 static int anx7411_psy_prop_writeable(struct power_supply *psy,
1375 enum power_supply_property psp)
1377 return psp == POWER_SUPPLY_PROP_ONLINE;
1380 static int anx7411_psy_get_prop(struct power_supply *psy,
1381 enum power_supply_property psp,
1382 union power_supply_propval *val)
1384 struct anx7411_data *ctx = power_supply_get_drvdata(psy);
1388 case POWER_SUPPLY_PROP_USB_TYPE:
1389 val->intval = ctx->usb_type;
1391 case POWER_SUPPLY_PROP_ONLINE:
1392 val->intval = ctx->psy_online;
1394 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1395 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
1396 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
1397 val->intval = (ctx->psy_online) ?
1398 ctx->typec.request_voltage * 1000 : 0;
1400 case POWER_SUPPLY_PROP_CURRENT_NOW:
1401 case POWER_SUPPLY_PROP_CURRENT_MAX:
1402 val->intval = (ctx->psy_online) ?
1403 ctx->typec.request_current * 1000 : 0;
1412 static int anx7411_psy_register(struct anx7411_data *ctx)
1414 struct power_supply_desc *psy_desc = &ctx->psy_desc;
1415 struct power_supply_config psy_cfg = {};
1418 psy_name = devm_kasprintf(ctx->dev, GFP_KERNEL, "anx7411-source-psy-%s",
1419 dev_name(ctx->dev));
1423 psy_desc->name = psy_name;
1424 psy_desc->type = POWER_SUPPLY_TYPE_USB;
1425 psy_desc->usb_types = anx7411_psy_usb_types;
1426 psy_desc->num_usb_types = ARRAY_SIZE(anx7411_psy_usb_types);
1427 psy_desc->properties = anx7411_psy_props;
1428 psy_desc->num_properties = ARRAY_SIZE(anx7411_psy_props);
1430 psy_desc->get_property = anx7411_psy_get_prop;
1431 psy_desc->set_property = anx7411_psy_set_prop;
1432 psy_desc->property_is_writeable = anx7411_psy_prop_writeable;
1434 ctx->usb_type = POWER_SUPPLY_USB_TYPE_C;
1435 ctx->psy = devm_power_supply_register(ctx->dev, psy_desc, &psy_cfg);
1437 if (IS_ERR(ctx->psy))
1438 dev_warn(ctx->dev, "unable to register psy\n");
1440 return PTR_ERR_OR_ZERO(ctx->psy);
1443 static int anx7411_i2c_probe(struct i2c_client *client,
1444 const struct i2c_device_id *id)
1446 struct anx7411_data *plat;
1447 struct device *dev = &client->dev;
1450 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
1453 plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
1457 plat->tcpc_client = client;
1458 i2c_set_clientdata(client, plat);
1460 mutex_init(&plat->lock);
1462 ret = anx7411_register_i2c_dummy_clients(plat, client);
1464 dev_err(dev, "fail to reserve I2C bus\n");
1468 ret = anx7411_typec_switch_probe(plat, dev);
1470 dev_err(dev, "fail to probe typec switch\n");
1471 goto free_i2c_dummy;
1474 ret = anx7411_typec_port_probe(plat, dev);
1476 dev_err(dev, "fail to probe typec property.\n");
1478 goto free_typec_switch;
1481 plat->intp_irq = client->irq;
1483 anx7411_get_gpio_irq(plat);
1485 if (!plat->intp_irq) {
1486 dev_err(dev, "fail to get interrupt IRQ\n");
1488 goto free_typec_port;
1492 plat->psy_online = ANX7411_PSY_OFFLINE;
1493 ret = anx7411_psy_register(plat);
1495 dev_err(dev, "register psy\n");
1496 goto free_typec_port;
1499 INIT_WORK(&plat->work, anx7411_work_func);
1500 plat->workqueue = alloc_workqueue("anx7411_work",
1504 if (!plat->workqueue) {
1505 dev_err(dev, "fail to create work queue\n");
1507 goto free_typec_port;
1510 ret = devm_request_threaded_irq(dev, plat->intp_irq,
1511 NULL, anx7411_intr_isr,
1512 IRQF_TRIGGER_FALLING |
1514 "anx7411-intp", plat);
1516 dev_err(dev, "fail to request irq\n");
1520 if (anx7411_typec_check_connection(plat))
1521 dev_err(dev, "check status\n");
1523 pm_runtime_enable(dev);
1528 destroy_workqueue(plat->workqueue);
1531 typec_unregister_port(plat->typec.port);
1532 anx7411_port_unregister_altmodes(plat->typec.port_amode);
1535 anx7411_unregister_switch(plat);
1536 anx7411_unregister_mux(plat);
1539 i2c_unregister_device(plat->spi_client);
1544 static void anx7411_i2c_remove(struct i2c_client *client)
1546 struct anx7411_data *plat = i2c_get_clientdata(client);
1548 anx7411_partner_unregister_altmode(plat);
1549 anx7411_unregister_partner(plat);
1551 if (plat->workqueue)
1552 destroy_workqueue(plat->workqueue);
1554 if (plat->spi_client)
1555 i2c_unregister_device(plat->spi_client);
1557 if (plat->typec.role_sw)
1558 usb_role_switch_put(plat->typec.role_sw);
1560 anx7411_unregister_mux(plat);
1562 anx7411_unregister_switch(plat);
1564 if (plat->typec.port)
1565 typec_unregister_port(plat->typec.port);
1567 anx7411_port_unregister_altmodes(plat->typec.port_amode);
1570 static const struct i2c_device_id anx7411_id[] = {
1575 MODULE_DEVICE_TABLE(i2c, anx7411_id);
1577 static const struct of_device_id anx_match_table[] = {
1578 {.compatible = "analogix,anx7411",},
1582 static struct i2c_driver anx7411_driver = {
1585 .of_match_table = anx_match_table,
1586 .pm = &anx7411_pm_ops,
1588 .probe = anx7411_i2c_probe,
1589 .remove = anx7411_i2c_remove,
1591 .id_table = anx7411_id,
1594 module_i2c_driver(anx7411_driver);
1596 MODULE_DESCRIPTION("Anx7411 USB Type-C PD driver");
1597 MODULE_AUTHOR("Xin Ji <xji@analogixsemi.com>");
1598 MODULE_LICENSE("GPL");
1599 MODULE_VERSION("0.1.5");