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 
41 struct 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 
113 enum 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 
222 enum 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 
235 struct 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
266 struct fw_msg {
267 	u8 msg_len;
268 	u8 msg_type;
269 	u8 buf[MAX_BUF_LEN];
270 } __packed;
271 
272 struct 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 };
294 
295 static u8 snk_identity[] = {
296 	LOBYTE(VID_ANALOGIX), HIBYTE(VID_ANALOGIX), 0x00, 0x82, /* snk_id_hdr */
297 	0x00, 0x00, 0x00, 0x00,                                 /* snk_cert */
298 	0x00, 0x00, LOBYTE(PID_ANALOGIX), HIBYTE(PID_ANALOGIX), /* 5snk_ama */
299 };
300 
301 static u8 dp_caps[4] = {0xC6, 0x00, 0x00, 0x00};
302 
anx7411_reg_read(struct i2c_client * client,u8 reg_addr)303 static int anx7411_reg_read(struct i2c_client *client,
304 			    u8 reg_addr)
305 {
306 	return i2c_smbus_read_byte_data(client, reg_addr);
307 }
308 
anx7411_reg_block_read(struct i2c_client * client,u8 reg_addr,u8 len,u8 * buf)309 static int anx7411_reg_block_read(struct i2c_client *client,
310 				  u8 reg_addr, u8 len, u8 *buf)
311 {
312 	return i2c_smbus_read_i2c_block_data(client, reg_addr, len, buf);
313 }
314 
anx7411_reg_write(struct i2c_client * client,u8 reg_addr,u8 reg_val)315 static int anx7411_reg_write(struct i2c_client *client,
316 			     u8 reg_addr, u8 reg_val)
317 {
318 	return i2c_smbus_write_byte_data(client, reg_addr, reg_val);
319 }
320 
anx7411_reg_block_write(struct i2c_client * client,u8 reg_addr,u8 len,u8 * buf)321 static int anx7411_reg_block_write(struct i2c_client *client,
322 				   u8 reg_addr, u8 len, u8 *buf)
323 {
324 	return i2c_smbus_write_i2c_block_data(client, reg_addr, len, buf);
325 }
326 
327 static struct anx7411_i2c_select anx7411_i2c_addr[] = {
328 	{TCPC_ADDRESS1, SPI_ADDRESS1},
329 	{TCPC_ADDRESS2, SPI_ADDRESS2},
330 	{TCPC_ADDRESS3, SPI_ADDRESS3},
331 	{TCPC_ADDRESS4, SPI_ADDRESS4},
332 };
333 
anx7411_detect_power_mode(struct anx7411_data * ctx)334 static int anx7411_detect_power_mode(struct anx7411_data *ctx)
335 {
336 	int ret;
337 	int mode;
338 
339 	ret = anx7411_reg_read(ctx->spi_client, REQUEST_CURRENT);
340 	if (ret < 0)
341 		return ret;
342 
343 	ctx->typec.request_current = ret * CURRENT_UNIT; /* 50ma per unit */
344 
345 	ret = anx7411_reg_read(ctx->spi_client, REQUEST_VOLTAGE);
346 	if (ret < 0)
347 		return ret;
348 
349 	ctx->typec.request_voltage = ret * VOLTAGE_UNIT; /* 100mv per unit */
350 
351 	if (ctx->psy_online == ANX7411_PSY_OFFLINE) {
352 		ctx->psy_online = ANX7411_PSY_FIXED_ONLINE;
353 		ctx->usb_type = POWER_SUPPLY_USB_TYPE_PD;
354 		power_supply_changed(ctx->psy);
355 	}
356 
357 	if (!ctx->typec.cc_orientation_valid)
358 		return 0;
359 
360 	if (ctx->typec.cc_connect == CC1_CONNECTED)
361 		mode = CC1_RP(ctx->typec.cc_status);
362 	else
363 		mode = CC2_RP(ctx->typec.cc_status);
364 	if (mode) {
365 		typec_set_pwr_opmode(ctx->typec.port, mode - 1);
366 		return 0;
367 	}
368 
369 	typec_set_pwr_opmode(ctx->typec.port, TYPEC_PWR_MODE_PD);
370 
371 	return 0;
372 }
373 
anx7411_register_partner(struct anx7411_data * ctx,int pd,int accessory)374 static int anx7411_register_partner(struct anx7411_data *ctx,
375 				    int pd, int accessory)
376 {
377 	struct typec_partner_desc desc;
378 	struct typec_partner *partner;
379 
380 	if (ctx->typec.partner)
381 		return 0;
382 
383 	desc.usb_pd = pd;
384 	desc.accessory = accessory;
385 	desc.identity = NULL;
386 	partner = typec_register_partner(ctx->typec.port, &desc);
387 	if (IS_ERR(partner))
388 		return PTR_ERR(partner);
389 
390 	ctx->typec.partner = partner;
391 
392 	return 0;
393 }
394 
anx7411_detect_cc_orientation(struct anx7411_data * ctx)395 static int anx7411_detect_cc_orientation(struct anx7411_data *ctx)
396 {
397 	struct device *dev = &ctx->spi_client->dev;
398 	int ret;
399 	int cc1_rd, cc2_rd;
400 	int cc1_ra, cc2_ra;
401 	int cc1_rp, cc2_rp;
402 
403 	ret = anx7411_reg_read(ctx->spi_client, CC_STATUS);
404 	if (ret < 0)
405 		return ret;
406 
407 	ctx->typec.cc_status = ret;
408 
409 	cc1_rd = ret & CC1_RD ? 1 : 0;
410 	cc2_rd = ret & CC2_RD ? 1 : 0;
411 	cc1_ra = ret & CC1_RA ? 1 : 0;
412 	cc2_ra = ret & CC2_RA ? 1 : 0;
413 	cc1_rp = CC1_RP(ret);
414 	cc2_rp = CC2_RP(ret);
415 
416 	/* Debug cable, nothing to do */
417 	if (cc1_rd && cc2_rd) {
418 		ctx->typec.cc_orientation_valid = 0;
419 		return anx7411_register_partner(ctx, 0, TYPEC_ACCESSORY_DEBUG);
420 	}
421 
422 	if (cc1_ra && cc2_ra) {
423 		ctx->typec.cc_orientation_valid = 0;
424 		return anx7411_register_partner(ctx, 0, TYPEC_ACCESSORY_AUDIO);
425 	}
426 
427 	ctx->typec.cc_orientation_valid = 1;
428 
429 	ret = anx7411_register_partner(ctx, 1, TYPEC_ACCESSORY_NONE);
430 	if (ret) {
431 		dev_err(dev, "register partner\n");
432 		return ret;
433 	}
434 
435 	if (cc1_rd || cc1_rp) {
436 		typec_set_orientation(ctx->typec.port, TYPEC_ORIENTATION_NORMAL);
437 		ctx->typec.cc_connect = CC1_CONNECTED;
438 	}
439 
440 	if (cc2_rd || cc2_rp) {
441 		typec_set_orientation(ctx->typec.port, TYPEC_ORIENTATION_REVERSE);
442 		ctx->typec.cc_connect = CC2_CONNECTED;
443 	}
444 
445 	return 0;
446 }
447 
anx7411_set_mux(struct anx7411_data * ctx,int pin_assignment)448 static int anx7411_set_mux(struct anx7411_data *ctx, int pin_assignment)
449 {
450 	int mode = TYPEC_STATE_SAFE;
451 
452 	switch (pin_assignment) {
453 	case SELECT_PIN_ASSIGMENT_U:
454 		/* default 4 line USB 3.1 */
455 		mode = TYPEC_STATE_MODAL;
456 		break;
457 	case SELECT_PIN_ASSIGMENT_C:
458 	case SELECT_PIN_ASSIGMENT_E:
459 		/* 4 line DP */
460 		mode = TYPEC_STATE_SAFE;
461 		break;
462 	case SELECT_PIN_ASSIGMENT_D:
463 		/* 2 line DP, 2 line USB */
464 		mode = TYPEC_MODE_USB3;
465 		break;
466 	default:
467 		mode = TYPEC_STATE_SAFE;
468 		break;
469 	}
470 
471 	ctx->typec.pin_assignment = pin_assignment;
472 
473 	return typec_set_mode(ctx->typec.port, mode);
474 }
475 
anx7411_set_usb_role(struct anx7411_data * ctx,enum usb_role role)476 static int anx7411_set_usb_role(struct anx7411_data *ctx, enum usb_role role)
477 {
478 	if (!ctx->typec.role_sw)
479 		return 0;
480 
481 	return usb_role_switch_set_role(ctx->typec.role_sw, role);
482 }
483 
anx7411_data_role_detect(struct anx7411_data * ctx)484 static int anx7411_data_role_detect(struct anx7411_data *ctx)
485 {
486 	int ret;
487 
488 	ret = anx7411_reg_read(ctx->spi_client, SYSTEM_STSTUS);
489 	if (ret < 0)
490 		return ret;
491 
492 	ctx->typec.data_role = (ret & DATA_ROLE) ? TYPEC_HOST : TYPEC_DEVICE;
493 	ctx->typec.vconn_role = (ret & VCONN_STATUS) ? TYPEC_SOURCE : TYPEC_SINK;
494 
495 	typec_set_data_role(ctx->typec.port, ctx->typec.data_role);
496 
497 	typec_set_vconn_role(ctx->typec.port, ctx->typec.vconn_role);
498 
499 	if (ctx->typec.data_role == TYPEC_HOST)
500 		return anx7411_set_usb_role(ctx, USB_ROLE_HOST);
501 
502 	return anx7411_set_usb_role(ctx, USB_ROLE_DEVICE);
503 }
504 
anx7411_power_role_detect(struct anx7411_data * ctx)505 static int anx7411_power_role_detect(struct anx7411_data *ctx)
506 {
507 	int ret;
508 
509 	ret = anx7411_reg_read(ctx->spi_client, SYSTEM_STSTUS);
510 	if (ret < 0)
511 		return ret;
512 
513 	ctx->typec.power_role = (ret & SINK_STATUS) ? TYPEC_SINK : TYPEC_SOURCE;
514 
515 	if (ctx->typec.power_role == TYPEC_SOURCE) {
516 		ctx->typec.request_current = DEF_1_5A;
517 		ctx->typec.request_voltage = DEF_5V;
518 	}
519 
520 	typec_set_pwr_role(ctx->typec.port, ctx->typec.power_role);
521 
522 	return 0;
523 }
524 
anx7411_cc_status_detect(struct anx7411_data * ctx)525 static int anx7411_cc_status_detect(struct anx7411_data *ctx)
526 {
527 	anx7411_detect_cc_orientation(ctx);
528 	anx7411_detect_power_mode(ctx);
529 
530 	return 0;
531 }
532 
anx7411_partner_unregister_altmode(struct anx7411_data * ctx)533 static void anx7411_partner_unregister_altmode(struct anx7411_data *ctx)
534 {
535 	int i;
536 
537 	ctx->typec.dp_altmode_enter = 0;
538 	ctx->typec.cust_altmode_enter = 0;
539 
540 	for (i = 0; i < MAX_ALTMODE; i++)
541 		if (ctx->typec.amode[i]) {
542 			typec_unregister_altmode(ctx->typec.amode[i]);
543 			ctx->typec.amode[i] = NULL;
544 		}
545 
546 	ctx->typec.pin_assignment = 0;
547 }
548 
anx7411_typec_register_altmode(struct anx7411_data * ctx,int svid,int vdo)549 static int anx7411_typec_register_altmode(struct anx7411_data *ctx,
550 					  int svid, int vdo)
551 {
552 	struct device *dev = &ctx->spi_client->dev;
553 	struct typec_altmode_desc desc;
554 	int err;
555 	int i;
556 
557 	desc.svid = svid;
558 	desc.vdo = vdo;
559 
560 	for (i = 0; i < MAX_ALTMODE; i++)
561 		if (!ctx->typec.amode[i])
562 			break;
563 
564 	desc.mode = i + 1; /* start with 1 */
565 
566 	if (i >= MAX_ALTMODE) {
567 		dev_err(dev, "no altmode space for registering\n");
568 		return -ENOMEM;
569 	}
570 
571 	ctx->typec.amode[i] = typec_partner_register_altmode(ctx->typec.partner,
572 							     &desc);
573 	if (IS_ERR(ctx->typec.amode[i])) {
574 		dev_err(dev, "failed to register altmode\n");
575 		err = PTR_ERR(ctx->typec.amode[i]);
576 		ctx->typec.amode[i] = NULL;
577 		return err;
578 	}
579 
580 	return 0;
581 }
582 
anx7411_unregister_partner(struct anx7411_data * ctx)583 static void anx7411_unregister_partner(struct anx7411_data *ctx)
584 {
585 	if (ctx->typec.partner) {
586 		typec_unregister_partner(ctx->typec.partner);
587 		ctx->typec.partner = NULL;
588 	}
589 }
590 
anx7411_update_altmode(struct anx7411_data * ctx,int svid)591 static int anx7411_update_altmode(struct anx7411_data *ctx, int svid)
592 {
593 	int i;
594 
595 	if (svid == DP_SVID)
596 		ctx->typec.dp_altmode_enter = 1;
597 	else
598 		ctx->typec.cust_altmode_enter = 1;
599 
600 	for (i = 0; i < MAX_ALTMODE; i++) {
601 		if (!ctx->typec.amode[i])
602 			continue;
603 
604 		if (ctx->typec.amode[i]->svid == svid) {
605 			typec_altmode_update_active(ctx->typec.amode[i], true);
606 			typec_altmode_notify(ctx->typec.amode[i],
607 					     ctx->typec.pin_assignment,
608 					     &ctx->typec.data);
609 			break;
610 		}
611 	}
612 
613 	return 0;
614 }
615 
anx7411_register_altmode(struct anx7411_data * ctx,bool dp_altmode,u8 * buf)616 static int anx7411_register_altmode(struct anx7411_data *ctx,
617 				    bool dp_altmode, u8 *buf)
618 {
619 	int ret;
620 	int svid;
621 	int mid;
622 
623 	if (!ctx->typec.partner)
624 		return 0;
625 
626 	svid = DP_SVID;
627 	if (dp_altmode) {
628 		mid = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
629 
630 		return anx7411_typec_register_altmode(ctx, svid, mid);
631 	}
632 
633 	svid = (buf[3] << 8) | buf[2];
634 	if ((buf[0] & VDM_CMD_AND_ACK_MASK) != (VDM_ACK | VDM_CMD_ENTER_MODE))
635 		return anx7411_update_altmode(ctx, svid);
636 
637 	if ((buf[0] & VDM_CMD_AND_ACK_MASK) != (VDM_ACK | VDM_CMD_DIS_MOD))
638 		return 0;
639 
640 	mid = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24);
641 
642 	ret = anx7411_typec_register_altmode(ctx, svid, mid);
643 	if (ctx->typec.cust_altmode_enter)
644 		ret |= anx7411_update_altmode(ctx, svid);
645 
646 	return ret;
647 }
648 
anx7411_parse_cmd(struct anx7411_data * ctx,u8 type,u8 * buf,u8 len)649 static int anx7411_parse_cmd(struct anx7411_data *ctx, u8 type, u8 *buf, u8 len)
650 {
651 	struct device *dev = &ctx->spi_client->dev;
652 	u8 cur_50ma, vol_100mv;
653 
654 	switch (type) {
655 	case TYPE_SRC_CAP:
656 		cur_50ma = anx7411_reg_read(ctx->spi_client, REQUEST_CURRENT);
657 		vol_100mv = anx7411_reg_read(ctx->spi_client, REQUEST_VOLTAGE);
658 
659 		ctx->typec.request_voltage = vol_100mv * VOLTAGE_UNIT;
660 		ctx->typec.request_current = cur_50ma * CURRENT_UNIT;
661 
662 		ctx->psy_online = ANX7411_PSY_FIXED_ONLINE;
663 		ctx->usb_type = POWER_SUPPLY_USB_TYPE_PD;
664 		power_supply_changed(ctx->psy);
665 		break;
666 	case TYPE_SNK_CAP:
667 		break;
668 	case TYPE_SVID:
669 		break;
670 	case TYPE_SNK_IDENTITY:
671 		break;
672 	case TYPE_GET_DP_ALT_ENTER:
673 		/* DP alt mode enter success */
674 		if (buf[0])
675 			anx7411_update_altmode(ctx, DP_SVID);
676 		break;
677 	case TYPE_DP_ALT_ENTER:
678 		/* Update DP altmode */
679 		anx7411_update_altmode(ctx, DP_SVID);
680 		break;
681 	case TYPE_OBJ_REQ:
682 		anx7411_detect_power_mode(ctx);
683 		break;
684 	case TYPE_DP_CONFIGURE:
685 		anx7411_set_mux(ctx, buf[1]);
686 		break;
687 	case TYPE_DP_DISCOVER_MODES_INFO:
688 		/* Make sure discover modes valid */
689 		if (buf[0] | buf[1])
690 			/* Register DP Altmode */
691 			anx7411_register_altmode(ctx, 1, buf);
692 		break;
693 	case TYPE_VDM:
694 		/* Register other altmode */
695 		anx7411_register_altmode(ctx, 0, buf);
696 		break;
697 	default:
698 		dev_err(dev, "ignore message(0x%.02x).\n", type);
699 		break;
700 	}
701 
702 	return 0;
703 }
704 
checksum(struct device * dev,u8 * buf,u8 len)705 static u8 checksum(struct device *dev, u8 *buf, u8 len)
706 {
707 	u8 ret = 0;
708 	u8 i;
709 
710 	for (i = 0; i < len; i++)
711 		ret += buf[i];
712 
713 	return ret;
714 }
715 
anx7411_read_msg_ctrl_status(struct i2c_client * client)716 static int anx7411_read_msg_ctrl_status(struct i2c_client *client)
717 {
718 	return anx7411_reg_read(client, CMD_SEND_BUF);
719 }
720 
anx7411_wait_msg_empty(struct i2c_client * client)721 static int anx7411_wait_msg_empty(struct i2c_client *client)
722 {
723 	int val;
724 
725 	return readx_poll_timeout(anx7411_read_msg_ctrl_status,
726 				  client, val, (val < 0) || (val == 0),
727 				  2000, 2000 * 150);
728 }
729 
anx7411_send_msg(struct anx7411_data * ctx,u8 type,u8 * buf,u8 size)730 static int anx7411_send_msg(struct anx7411_data *ctx, u8 type, u8 *buf, u8 size)
731 {
732 	struct device *dev = &ctx->spi_client->dev;
733 	struct fw_msg *msg = &ctx->send_msg;
734 	u8 crc;
735 	int ret;
736 
737 	size = min_t(u8, size, (u8)MAX_BUF_LEN);
738 	memcpy(msg->buf, buf, size);
739 	msg->msg_type = type;
740 	/* msg len equals buffer length + msg_type */
741 	msg->msg_len = size + 1;
742 
743 	/* Do CRC check for all buffer data and msg_len and msg_type */
744 	crc = checksum(dev, (u8 *)msg, size + HEADER_LEN);
745 	msg->buf[size] = 0 - crc;
746 
747 	ret = anx7411_wait_msg_empty(ctx->spi_client);
748 	if (ret)
749 		return ret;
750 
751 	ret = anx7411_reg_block_write(ctx->spi_client,
752 				      CMD_SEND_BUF + 1, size + HEADER_LEN,
753 				      &msg->msg_type);
754 	ret |= anx7411_reg_write(ctx->spi_client, CMD_SEND_BUF,
755 				 msg->msg_len);
756 	return ret;
757 }
758 
anx7411_process_cmd(struct anx7411_data * ctx)759 static int anx7411_process_cmd(struct anx7411_data *ctx)
760 {
761 	struct device *dev = &ctx->spi_client->dev;
762 	struct fw_msg *msg = &ctx->recv_msg;
763 	u8 len;
764 	u8 crc;
765 	int ret;
766 
767 	/* Read message from firmware */
768 	ret = anx7411_reg_block_read(ctx->spi_client, CMD_RECV_BUF,
769 				     MSG_LEN, (u8 *)msg);
770 	if (ret < 0)
771 		return 0;
772 
773 	if (!msg->msg_len)
774 		return 0;
775 
776 	ret = anx7411_reg_write(ctx->spi_client, CMD_RECV_BUF, 0);
777 	if (ret)
778 		return ret;
779 
780 	len = msg->msg_len & MSG_LEN_MASK;
781 	crc = checksum(dev, (u8 *)msg, len + HEADER_LEN);
782 	if (crc) {
783 		dev_err(dev, "message error crc(0x%.02x)\n", crc);
784 		return -ERANGE;
785 	}
786 
787 	return anx7411_parse_cmd(ctx, msg->msg_type, msg->buf, len - 1);
788 }
789 
anx7411_translate_payload(struct device * dev,__le32 * payload,u32 * pdo,int nr,const char * type)790 static void anx7411_translate_payload(struct device *dev, __le32 *payload,
791 				      u32 *pdo, int nr, const char *type)
792 {
793 	int i;
794 
795 	if (nr > PDO_MAX_OBJECTS) {
796 		dev_err(dev, "nr(%d) exceed PDO_MAX_OBJECTS(%d)\n",
797 			nr, PDO_MAX_OBJECTS);
798 
799 		return;
800 	}
801 
802 	for (i = 0; i < nr; i++)
803 		payload[i] = cpu_to_le32(pdo[i]);
804 }
805 
anx7411_config(struct anx7411_data * ctx)806 static int anx7411_config(struct anx7411_data *ctx)
807 {
808 	struct device *dev = &ctx->spi_client->dev;
809 	struct typec_params *typecp = &ctx->typec;
810 	__le32 payload[PDO_MAX_OBJECTS];
811 	int ret;
812 
813 	/* Config PD FW work under PD 2.0 */
814 	ret = anx7411_reg_write(ctx->spi_client, PD_REV_INIT, PD_REV20);
815 	ret |= anx7411_reg_write(ctx->tcpc_client, FW_CTRL_0,
816 				 UNSTRUCT_VDM_EN | DELAY_200MS |
817 				 VSAFE1 | FRS_EN);
818 	ret |= anx7411_reg_write(ctx->spi_client, FW_CTRL_1,
819 				 AUTO_PD_EN | FORCE_SEND_RDO);
820 
821 	/* Set VBUS current threshold */
822 	ret |= anx7411_reg_write(ctx->tcpc_client, VBUS_THRESHOLD_H, 0xff);
823 	ret |= anx7411_reg_write(ctx->tcpc_client, VBUS_THRESHOLD_L, 0x03);
824 
825 	/* Fix dongle compatible issue */
826 	ret |= anx7411_reg_write(ctx->tcpc_client, FW_PARAM,
827 				 anx7411_reg_read(ctx->tcpc_client, FW_PARAM) |
828 				 DONGLE_IOP);
829 	ret |= anx7411_reg_write(ctx->spi_client, INT_MASK, 0);
830 
831 	ret |= anx7411_reg_write(ctx->spi_client, PD_EXT_MSG_CTRL, 0xFF);
832 	if (ret)
833 		return ret;
834 
835 	if (typecp->caps_flags & HAS_SOURCE_CAP) {
836 		anx7411_translate_payload(dev, payload, typecp->src_pdo,
837 					  typecp->src_pdo_nr, "source");
838 		anx7411_send_msg(ctx, TYPE_SRC_CAP, (u8 *)&payload,
839 				 typecp->src_pdo_nr * 4);
840 		anx7411_send_msg(ctx, TYPE_SNK_IDENTITY, snk_identity,
841 				 sizeof(snk_identity));
842 		anx7411_send_msg(ctx, TYPE_SET_SNK_DP_CAP, dp_caps,
843 				 sizeof(dp_caps));
844 	}
845 
846 	if (typecp->caps_flags & HAS_SINK_CAP) {
847 		anx7411_translate_payload(dev, payload, typecp->sink_pdo,
848 					  typecp->sink_pdo_nr, "sink");
849 		anx7411_send_msg(ctx, TYPE_SNK_CAP, (u8 *)&payload,
850 				 typecp->sink_pdo_nr * 4);
851 	}
852 
853 	if (typecp->caps_flags & HAS_SINK_WATT) {
854 		if (typecp->sink_watt) {
855 			ret |= anx7411_reg_write(ctx->spi_client, MAX_POWER,
856 						 typecp->sink_watt);
857 			/* Set min power to 1W */
858 			ret |= anx7411_reg_write(ctx->spi_client, MIN_POWER, 2);
859 		}
860 
861 		if (typecp->sink_voltage)
862 			ret |= anx7411_reg_write(ctx->spi_client, MAX_VOLTAGE,
863 					  typecp->sink_voltage);
864 		if (ret)
865 			return ret;
866 	}
867 
868 	if (!typecp->caps_flags)
869 		usleep_range(5000, 6000);
870 
871 	ctx->fw_version = anx7411_reg_read(ctx->spi_client, FW_VER);
872 	ctx->fw_subversion = anx7411_reg_read(ctx->spi_client, FW_SUBVER);
873 
874 	return 0;
875 }
876 
anx7411_chip_standby(struct anx7411_data * ctx)877 static void anx7411_chip_standby(struct anx7411_data *ctx)
878 {
879 	int ret;
880 	u8 cc1, cc2;
881 	struct device *dev = &ctx->spi_client->dev;
882 
883 	ret = anx7411_reg_write(ctx->spi_client, OCM_CTRL_0,
884 				anx7411_reg_read(ctx->spi_client, OCM_CTRL_0) |
885 				OCM_RESET);
886 	ret |= anx7411_reg_write(ctx->tcpc_client, ANALOG_CTRL_10, 0x80);
887 	/* Set TCPC to RD and DRP enable */
888 	cc1 = FIELD_PREP(TCPC_ROLE_CTRL_CC1, TCPC_ROLE_CTRL_CC_RD);
889 	cc2 = FIELD_PREP(TCPC_ROLE_CTRL_CC2, TCPC_ROLE_CTRL_CC_RD);
890 	ret |= anx7411_reg_write(ctx->tcpc_client, TCPC_ROLE_CTRL,
891 				 TCPC_ROLE_CTRL_DRP | cc1 | cc2);
892 
893 	/* Send DRP toggle command */
894 	ret |= anx7411_reg_write(ctx->tcpc_client, TCPC_COMMAND,
895 				 TCPC_CMD_LOOK4CONNECTION);
896 
897 	/* Send TCPC enter standby command */
898 	ret |= anx7411_reg_write(ctx->tcpc_client,
899 				 TCPC_COMMAND, TCPC_CMD_I2C_IDLE);
900 	if (ret)
901 		dev_err(dev, "Chip standby failed\n");
902 }
903 
anx7411_work_func(struct work_struct * work)904 static void anx7411_work_func(struct work_struct *work)
905 {
906 	int ret;
907 	u8 buf[STATUS_LEN];
908 	u8 int_change; /* Interrupt change */
909 	u8 int_status; /* Firmware status update */
910 	u8 alert0, alert1; /* Interrupt alert source */
911 	struct anx7411_data *ctx = container_of(work, struct anx7411_data, work);
912 	struct device *dev = &ctx->spi_client->dev;
913 
914 	mutex_lock(&ctx->lock);
915 
916 	/* Read interrupt change status */
917 	ret = anx7411_reg_block_read(ctx->spi_client, INT_STS, STATUS_LEN, buf);
918 	if (ret < 0) {
919 		/* Power standby mode, just return */
920 		goto unlock;
921 	}
922 	int_change = buf[0];
923 	int_status = buf[1];
924 
925 	/* Read alert register */
926 	ret = anx7411_reg_block_read(ctx->tcpc_client, ALERT_0, STATUS_LEN, buf);
927 	if (ret < 0)
928 		goto unlock;
929 
930 	alert0 = buf[0];
931 	alert1 = buf[1];
932 
933 	/* Clear interrupt and alert status */
934 	ret = anx7411_reg_write(ctx->spi_client, INT_STS, 0);
935 	ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_0, alert0);
936 	ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_1, alert1);
937 	if (ret)
938 		goto unlock;
939 
940 	if (alert1 & INTP_POW_OFF) {
941 		anx7411_partner_unregister_altmode(ctx);
942 		if (anx7411_set_usb_role(ctx, USB_ROLE_NONE))
943 			dev_err(dev, "Set usb role\n");
944 		anx7411_unregister_partner(ctx);
945 		ctx->psy_online = ANX7411_PSY_OFFLINE;
946 		ctx->usb_type = POWER_SUPPLY_USB_TYPE_C;
947 		ctx->typec.request_voltage = 0;
948 		ctx->typec.request_current = 0;
949 		power_supply_changed(ctx->psy);
950 		anx7411_chip_standby(ctx);
951 		goto unlock;
952 	}
953 
954 	if ((alert0 & SOFTWARE_INT) && (int_change & OCM_BOOT_UP)) {
955 		if (anx7411_config(ctx))
956 			dev_err(dev, "Config failed\n");
957 		if (anx7411_data_role_detect(ctx))
958 			dev_err(dev, "set PD data role\n");
959 		if (anx7411_power_role_detect(ctx))
960 			dev_err(dev, "set PD power role\n");
961 		anx7411_set_mux(ctx, SELECT_PIN_ASSIGMENT_C);
962 	}
963 
964 	if (alert0 & RECEIVED_MSG)
965 		anx7411_process_cmd(ctx);
966 
967 	ret = (int_status & DATA_ROLE) ? TYPEC_HOST : TYPEC_DEVICE;
968 	if (ctx->typec.data_role != ret)
969 		if (anx7411_data_role_detect(ctx))
970 			dev_err(dev, "set PD data role\n");
971 
972 	ret = (int_status & SINK_STATUS) ? TYPEC_SINK : TYPEC_SOURCE;
973 	if (ctx->typec.power_role != ret)
974 		if (anx7411_power_role_detect(ctx))
975 			dev_err(dev, "set PD power role\n");
976 
977 	if ((alert0 & SOFTWARE_INT) && (int_change & CC_STATUS_CHANGE))
978 		anx7411_cc_status_detect(ctx);
979 
980 unlock:
981 	mutex_unlock(&ctx->lock);
982 }
983 
anx7411_intr_isr(int irq,void * data)984 static irqreturn_t anx7411_intr_isr(int irq, void *data)
985 {
986 	struct anx7411_data *ctx = (struct anx7411_data *)data;
987 
988 	queue_work(ctx->workqueue, &ctx->work);
989 
990 	return IRQ_HANDLED;
991 }
992 
anx7411_register_i2c_dummy_clients(struct anx7411_data * ctx,struct i2c_client * client)993 static int anx7411_register_i2c_dummy_clients(struct anx7411_data *ctx,
994 					      struct i2c_client *client)
995 {
996 	int i;
997 	u8 spi_addr;
998 
999 	for (i = 0; i < ARRAY_SIZE(anx7411_i2c_addr); i++) {
1000 		if (client->addr == (anx7411_i2c_addr[i].tcpc_address >> 1)) {
1001 			spi_addr = anx7411_i2c_addr[i].spi_address >> 1;
1002 			ctx->spi_client = i2c_new_dummy_device(client->adapter,
1003 							       spi_addr);
1004 			if (!IS_ERR(ctx->spi_client))
1005 				return 0;
1006 		}
1007 	}
1008 
1009 	dev_err(&client->dev, "unable to get SPI slave\n");
1010 	return -ENOMEM;
1011 }
1012 
anx7411_port_unregister_altmodes(struct typec_altmode ** adev)1013 static void anx7411_port_unregister_altmodes(struct typec_altmode **adev)
1014 {
1015 	int i;
1016 
1017 	for (i = 0; i < MAX_ALTMODE; i++)
1018 		if (adev[i]) {
1019 			typec_unregister_altmode(adev[i]);
1020 			adev[i] = NULL;
1021 		}
1022 }
1023 
anx7411_usb_mux_set(struct typec_mux_dev * mux,struct typec_mux_state * state)1024 static int anx7411_usb_mux_set(struct typec_mux_dev *mux,
1025 			       struct typec_mux_state *state)
1026 {
1027 	struct anx7411_data *ctx = typec_mux_get_drvdata(mux);
1028 	struct device *dev = &ctx->spi_client->dev;
1029 	int has_dp;
1030 
1031 	has_dp = (state->alt && state->alt->svid == USB_TYPEC_DP_SID &&
1032 		  state->alt->mode == USB_TYPEC_DP_MODE);
1033 	if (!has_dp)
1034 		dev_err(dev, "dp altmode not register\n");
1035 
1036 	return 0;
1037 }
1038 
anx7411_usb_set_orientation(struct typec_switch_dev * sw,enum typec_orientation orientation)1039 static int anx7411_usb_set_orientation(struct typec_switch_dev *sw,
1040 				       enum typec_orientation orientation)
1041 {
1042 	/* No need set */
1043 
1044 	return 0;
1045 }
1046 
anx7411_register_switch(struct anx7411_data * ctx,struct device * dev,struct fwnode_handle * fwnode)1047 static int anx7411_register_switch(struct anx7411_data *ctx,
1048 				   struct device *dev,
1049 				   struct fwnode_handle *fwnode)
1050 {
1051 	struct typec_switch_desc sw_desc = { };
1052 
1053 	sw_desc.fwnode = fwnode;
1054 	sw_desc.drvdata = ctx;
1055 	sw_desc.name = fwnode_get_name(fwnode);
1056 	sw_desc.set = anx7411_usb_set_orientation;
1057 
1058 	ctx->typec.typec_switch = typec_switch_register(dev, &sw_desc);
1059 	if (IS_ERR(ctx->typec.typec_switch)) {
1060 		dev_err(dev, "switch register failed\n");
1061 		return PTR_ERR(ctx->typec.typec_switch);
1062 	}
1063 
1064 	return 0;
1065 }
1066 
anx7411_register_mux(struct anx7411_data * ctx,struct device * dev,struct fwnode_handle * fwnode)1067 static int anx7411_register_mux(struct anx7411_data *ctx,
1068 				struct device *dev,
1069 				struct fwnode_handle *fwnode)
1070 {
1071 	struct typec_mux_desc mux_desc = { };
1072 
1073 	mux_desc.fwnode = fwnode;
1074 	mux_desc.drvdata = ctx;
1075 	mux_desc.name = fwnode_get_name(fwnode);
1076 	mux_desc.set = anx7411_usb_mux_set;
1077 
1078 	ctx->typec.typec_mux = typec_mux_register(dev, &mux_desc);
1079 	if (IS_ERR(ctx->typec.typec_mux)) {
1080 		dev_err(dev, "mux register failed\n");
1081 		return PTR_ERR(ctx->typec.typec_mux);
1082 	}
1083 
1084 	return 0;
1085 }
1086 
anx7411_unregister_mux(struct anx7411_data * ctx)1087 static void anx7411_unregister_mux(struct anx7411_data *ctx)
1088 {
1089 	if (ctx->typec.typec_mux) {
1090 		typec_mux_unregister(ctx->typec.typec_mux);
1091 		ctx->typec.typec_mux = NULL;
1092 	}
1093 }
1094 
anx7411_unregister_switch(struct anx7411_data * ctx)1095 static void anx7411_unregister_switch(struct anx7411_data *ctx)
1096 {
1097 	if (ctx->typec.typec_switch) {
1098 		typec_switch_unregister(ctx->typec.typec_switch);
1099 		ctx->typec.typec_switch = NULL;
1100 	}
1101 }
1102 
anx7411_typec_switch_probe(struct anx7411_data * ctx,struct device * dev)1103 static int anx7411_typec_switch_probe(struct anx7411_data *ctx,
1104 				      struct device *dev)
1105 {
1106 	int ret;
1107 	struct device_node *node;
1108 
1109 	node = of_get_child_by_name(dev->of_node, "orientation_switch");
1110 	if (!node)
1111 		return 0;
1112 
1113 	ret = anx7411_register_switch(ctx, dev, &node->fwnode);
1114 	if (ret) {
1115 		dev_err(dev, "failed register switch");
1116 		return ret;
1117 	}
1118 
1119 	node = of_get_child_by_name(dev->of_node, "mode_switch");
1120 	if (!node) {
1121 		dev_err(dev, "no typec mux exist");
1122 		ret = -ENODEV;
1123 		goto unregister_switch;
1124 	}
1125 
1126 	ret = anx7411_register_mux(ctx, dev, &node->fwnode);
1127 	if (ret) {
1128 		dev_err(dev, "failed register mode switch");
1129 		ret = -ENODEV;
1130 		goto unregister_switch;
1131 	}
1132 
1133 	return 0;
1134 
1135 unregister_switch:
1136 	anx7411_unregister_switch(ctx);
1137 
1138 	return ret;
1139 }
1140 
anx7411_typec_port_probe(struct anx7411_data * ctx,struct device * dev)1141 static int anx7411_typec_port_probe(struct anx7411_data *ctx,
1142 				    struct device *dev)
1143 {
1144 	struct typec_capability *cap = &ctx->typec.caps;
1145 	struct typec_params *typecp = &ctx->typec;
1146 	struct fwnode_handle *fwnode;
1147 	const char *buf;
1148 	int ret, i;
1149 
1150 	fwnode = device_get_named_child_node(dev, "connector");
1151 	if (!fwnode)
1152 		return -EINVAL;
1153 
1154 	ret = fwnode_property_read_string(fwnode, "power-role", &buf);
1155 	if (ret) {
1156 		dev_err(dev, "power-role not found: %d\n", ret);
1157 		return ret;
1158 	}
1159 
1160 	ret = typec_find_port_power_role(buf);
1161 	if (ret < 0)
1162 		return ret;
1163 	cap->type = ret;
1164 
1165 	ret = fwnode_property_read_string(fwnode, "data-role", &buf);
1166 	if (ret) {
1167 		dev_err(dev, "data-role not found: %d\n", ret);
1168 		return ret;
1169 	}
1170 
1171 	ret = typec_find_port_data_role(buf);
1172 	if (ret < 0)
1173 		return ret;
1174 	cap->data = ret;
1175 
1176 	ret = fwnode_property_read_string(fwnode, "try-power-role", &buf);
1177 	if (ret) {
1178 		dev_err(dev, "try-power-role not found: %d\n", ret);
1179 		return ret;
1180 	}
1181 
1182 	ret = typec_find_power_role(buf);
1183 	if (ret < 0)
1184 		return ret;
1185 	cap->prefer_role = ret;
1186 
1187 	/* Get source pdos */
1188 	ret = fwnode_property_count_u32(fwnode, "source-pdos");
1189 	if (ret > 0) {
1190 		typecp->src_pdo_nr = min_t(u8, ret, PDO_MAX_OBJECTS);
1191 		ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
1192 						     typecp->src_pdo,
1193 						     typecp->src_pdo_nr);
1194 		if (ret < 0) {
1195 			dev_err(dev, "source cap validate failed: %d\n", ret);
1196 			return -EINVAL;
1197 		}
1198 
1199 		typecp->caps_flags |= HAS_SOURCE_CAP;
1200 	}
1201 
1202 	ret = fwnode_property_count_u32(fwnode, "sink-pdos");
1203 	if (ret > 0) {
1204 		typecp->sink_pdo_nr = min_t(u8, ret, PDO_MAX_OBJECTS);
1205 		ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
1206 						     typecp->sink_pdo,
1207 						     typecp->sink_pdo_nr);
1208 		if (ret < 0) {
1209 			dev_err(dev, "sink cap validate failed: %d\n", ret);
1210 			return -EINVAL;
1211 		}
1212 
1213 		for (i = 0; i < typecp->sink_pdo_nr; i++) {
1214 			ret = 0;
1215 			switch (pdo_type(typecp->sink_pdo[i])) {
1216 			case PDO_TYPE_FIXED:
1217 				ret = pdo_fixed_voltage(typecp->sink_pdo[i]);
1218 				break;
1219 			case PDO_TYPE_BATT:
1220 			case PDO_TYPE_VAR:
1221 				ret = pdo_max_voltage(typecp->sink_pdo[i]);
1222 				break;
1223 			case PDO_TYPE_APDO:
1224 			default:
1225 				ret = 0;
1226 				break;
1227 			}
1228 
1229 			/* 100mv per unit */
1230 			typecp->sink_voltage = max(5000, ret) / 100;
1231 		}
1232 
1233 		typecp->caps_flags |= HAS_SINK_CAP;
1234 	}
1235 
1236 	if (!fwnode_property_read_u32(fwnode, "op-sink-microwatt", &ret)) {
1237 		typecp->sink_watt = ret / 500000; /* 500mw per unit */
1238 		typecp->caps_flags |= HAS_SINK_WATT;
1239 	}
1240 
1241 	cap->fwnode = fwnode;
1242 
1243 	ctx->typec.role_sw = usb_role_switch_get(dev);
1244 	if (IS_ERR(ctx->typec.role_sw)) {
1245 		dev_err(dev, "USB role switch not found.\n");
1246 		ctx->typec.role_sw = NULL;
1247 	}
1248 
1249 	ctx->typec.port = typec_register_port(dev, cap);
1250 	if (IS_ERR(ctx->typec.port)) {
1251 		ret = PTR_ERR(ctx->typec.port);
1252 		ctx->typec.port = NULL;
1253 		dev_err(dev, "Failed to register type c port %d\n", ret);
1254 		return ret;
1255 	}
1256 
1257 	typec_port_register_altmodes(ctx->typec.port, NULL, ctx,
1258 				     ctx->typec.port_amode,
1259 				     MAX_ALTMODE);
1260 	return 0;
1261 }
1262 
anx7411_typec_check_connection(struct anx7411_data * ctx)1263 static int anx7411_typec_check_connection(struct anx7411_data *ctx)
1264 {
1265 	int ret;
1266 
1267 	ret = anx7411_reg_read(ctx->spi_client, FW_VER);
1268 	if (ret < 0)
1269 		return 0; /* No device attached in typec port */
1270 
1271 	/* Clear interrupt and alert status */
1272 	ret = anx7411_reg_write(ctx->spi_client, INT_STS, 0);
1273 	ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_0, 0xFF);
1274 	ret |= anx7411_reg_write(ctx->tcpc_client, ALERT_1, 0xFF);
1275 	if (ret)
1276 		return ret;
1277 
1278 	ret = anx7411_cc_status_detect(ctx);
1279 	ret |= anx7411_power_role_detect(ctx);
1280 	ret |= anx7411_data_role_detect(ctx);
1281 	ret |= anx7411_set_mux(ctx, SELECT_PIN_ASSIGMENT_C);
1282 	if (ret)
1283 		return ret;
1284 
1285 	ret = anx7411_send_msg(ctx, TYPE_GET_DP_ALT_ENTER, NULL, 0);
1286 	ret |= anx7411_send_msg(ctx, TYPE_GET_DP_DISCOVER_MODES_INFO, NULL, 0);
1287 
1288 	return ret;
1289 }
1290 
anx7411_runtime_pm_suspend(struct device * dev)1291 static int __maybe_unused anx7411_runtime_pm_suspend(struct device *dev)
1292 {
1293 	struct anx7411_data *ctx = dev_get_drvdata(dev);
1294 
1295 	mutex_lock(&ctx->lock);
1296 
1297 	anx7411_partner_unregister_altmode(ctx);
1298 
1299 	if (ctx->typec.partner)
1300 		anx7411_unregister_partner(ctx);
1301 
1302 	mutex_unlock(&ctx->lock);
1303 
1304 	return 0;
1305 }
1306 
anx7411_runtime_pm_resume(struct device * dev)1307 static int __maybe_unused anx7411_runtime_pm_resume(struct device *dev)
1308 {
1309 	struct anx7411_data *ctx = dev_get_drvdata(dev);
1310 
1311 	mutex_lock(&ctx->lock);
1312 	/* Detect PD connection */
1313 	if (anx7411_typec_check_connection(ctx))
1314 		dev_err(dev, "check connection");
1315 
1316 	mutex_unlock(&ctx->lock);
1317 
1318 	return 0;
1319 }
1320 
1321 static const struct dev_pm_ops anx7411_pm_ops = {
1322 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1323 				pm_runtime_force_resume)
1324 	SET_RUNTIME_PM_OPS(anx7411_runtime_pm_suspend,
1325 			   anx7411_runtime_pm_resume, NULL)
1326 };
1327 
anx7411_get_gpio_irq(struct anx7411_data * ctx)1328 static void anx7411_get_gpio_irq(struct anx7411_data *ctx)
1329 {
1330 	struct device *dev = &ctx->tcpc_client->dev;
1331 
1332 	ctx->intp_gpiod = devm_gpiod_get_optional(dev, "interrupt", GPIOD_IN);
1333 	if (IS_ERR_OR_NULL(ctx->intp_gpiod)) {
1334 		dev_err(dev, "no interrupt gpio property\n");
1335 		return;
1336 	}
1337 
1338 	ctx->intp_irq = gpiod_to_irq(ctx->intp_gpiod);
1339 	if (ctx->intp_irq < 0)
1340 		dev_err(dev, "failed to get GPIO IRQ\n");
1341 }
1342 
1343 static enum power_supply_property anx7411_psy_props[] = {
1344 	POWER_SUPPLY_PROP_USB_TYPE,
1345 	POWER_SUPPLY_PROP_ONLINE,
1346 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
1347 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
1348 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
1349 	POWER_SUPPLY_PROP_CURRENT_MAX,
1350 	POWER_SUPPLY_PROP_CURRENT_NOW,
1351 };
1352 
anx7411_psy_set_prop(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)1353 static int anx7411_psy_set_prop(struct power_supply *psy,
1354 				enum power_supply_property psp,
1355 				const union power_supply_propval *val)
1356 {
1357 	struct anx7411_data *ctx = power_supply_get_drvdata(psy);
1358 	int ret = 0;
1359 
1360 	if (psp == POWER_SUPPLY_PROP_ONLINE)
1361 		ctx->psy_online = val->intval;
1362 	else
1363 		ret = -EINVAL;
1364 
1365 	power_supply_changed(ctx->psy);
1366 	return ret;
1367 }
1368 
anx7411_psy_prop_writeable(struct power_supply * psy,enum power_supply_property psp)1369 static int anx7411_psy_prop_writeable(struct power_supply *psy,
1370 				      enum power_supply_property psp)
1371 {
1372 	return psp == POWER_SUPPLY_PROP_ONLINE;
1373 }
1374 
anx7411_psy_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)1375 static int anx7411_psy_get_prop(struct power_supply *psy,
1376 				enum power_supply_property psp,
1377 				union power_supply_propval *val)
1378 {
1379 	struct anx7411_data *ctx = power_supply_get_drvdata(psy);
1380 	int ret = 0;
1381 
1382 	switch (psp) {
1383 	case POWER_SUPPLY_PROP_USB_TYPE:
1384 		val->intval = ctx->usb_type;
1385 		break;
1386 	case POWER_SUPPLY_PROP_ONLINE:
1387 		val->intval = ctx->psy_online;
1388 		break;
1389 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1390 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
1391 	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
1392 		val->intval = (ctx->psy_online) ?
1393 			ctx->typec.request_voltage * 1000 : 0;
1394 		break;
1395 	case POWER_SUPPLY_PROP_CURRENT_NOW:
1396 	case POWER_SUPPLY_PROP_CURRENT_MAX:
1397 		val->intval = (ctx->psy_online) ?
1398 			ctx->typec.request_current * 1000 : 0;
1399 		break;
1400 	default:
1401 		ret = -EINVAL;
1402 		break;
1403 	}
1404 	return ret;
1405 }
1406 
anx7411_psy_register(struct anx7411_data * ctx)1407 static int anx7411_psy_register(struct anx7411_data *ctx)
1408 {
1409 	struct power_supply_desc *psy_desc = &ctx->psy_desc;
1410 	struct power_supply_config psy_cfg = {};
1411 	char *psy_name;
1412 
1413 	psy_name = devm_kasprintf(ctx->dev, GFP_KERNEL, "anx7411-source-psy-%s",
1414 				  dev_name(ctx->dev));
1415 	if (!psy_name)
1416 		return -ENOMEM;
1417 
1418 	psy_desc->name = psy_name;
1419 	psy_desc->type = POWER_SUPPLY_TYPE_USB;
1420 	psy_desc->usb_types = BIT(POWER_SUPPLY_USB_TYPE_C)  |
1421 			      BIT(POWER_SUPPLY_USB_TYPE_PD) |
1422 			      BIT(POWER_SUPPLY_USB_TYPE_PD_PPS);
1423 	psy_desc->properties = anx7411_psy_props;
1424 	psy_desc->num_properties = ARRAY_SIZE(anx7411_psy_props);
1425 
1426 	psy_desc->get_property = anx7411_psy_get_prop;
1427 	psy_desc->set_property = anx7411_psy_set_prop;
1428 	psy_desc->property_is_writeable = anx7411_psy_prop_writeable;
1429 
1430 	ctx->usb_type = POWER_SUPPLY_USB_TYPE_C;
1431 	ctx->psy = devm_power_supply_register(ctx->dev, psy_desc, &psy_cfg);
1432 
1433 	if (IS_ERR(ctx->psy))
1434 		dev_warn(ctx->dev, "unable to register psy\n");
1435 
1436 	return PTR_ERR_OR_ZERO(ctx->psy);
1437 }
1438 
anx7411_i2c_probe(struct i2c_client * client)1439 static int anx7411_i2c_probe(struct i2c_client *client)
1440 {
1441 	struct anx7411_data *plat;
1442 	struct device *dev = &client->dev;
1443 	int ret;
1444 
1445 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
1446 		return -ENODEV;
1447 
1448 	plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
1449 	if (!plat)
1450 		return -ENOMEM;
1451 
1452 	plat->tcpc_client = client;
1453 	i2c_set_clientdata(client, plat);
1454 
1455 	mutex_init(&plat->lock);
1456 
1457 	ret = anx7411_register_i2c_dummy_clients(plat, client);
1458 	if (ret) {
1459 		dev_err(dev, "fail to reserve I2C bus\n");
1460 		return ret;
1461 	}
1462 
1463 	ret = anx7411_typec_switch_probe(plat, dev);
1464 	if (ret) {
1465 		dev_err(dev, "fail to probe typec switch\n");
1466 		goto free_i2c_dummy;
1467 	}
1468 
1469 	ret = anx7411_typec_port_probe(plat, dev);
1470 	if (ret) {
1471 		dev_err(dev, "fail to probe typec property.\n");
1472 		ret = -ENODEV;
1473 		goto free_typec_switch;
1474 	}
1475 
1476 	plat->intp_irq = client->irq;
1477 	if (!client->irq)
1478 		anx7411_get_gpio_irq(plat);
1479 
1480 	if (!plat->intp_irq) {
1481 		dev_err(dev, "fail to get interrupt IRQ\n");
1482 		ret = -EINVAL;
1483 		goto free_typec_port;
1484 	}
1485 
1486 	plat->dev = dev;
1487 	plat->psy_online = ANX7411_PSY_OFFLINE;
1488 	ret = anx7411_psy_register(plat);
1489 	if (ret) {
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 
anx7411_i2c_remove(struct i2c_client * client)1539 static void 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 	i2c_unregister_device(plat->spi_client);
1550 
1551 	if (plat->typec.role_sw)
1552 		usb_role_switch_put(plat->typec.role_sw);
1553 
1554 	anx7411_unregister_mux(plat);
1555 
1556 	anx7411_unregister_switch(plat);
1557 
1558 	if (plat->typec.port)
1559 		typec_unregister_port(plat->typec.port);
1560 
1561 	anx7411_port_unregister_altmodes(plat->typec.port_amode);
1562 }
1563 
1564 static const struct i2c_device_id anx7411_id[] = {
1565 	{ "anx7411" },
1566 	{}
1567 };
1568 
1569 MODULE_DEVICE_TABLE(i2c, anx7411_id);
1570 
1571 static const struct of_device_id anx_match_table[] = {
1572 	{.compatible = "analogix,anx7411",},
1573 	{},
1574 };
1575 MODULE_DEVICE_TABLE(of, anx_match_table);
1576 
1577 static struct i2c_driver anx7411_driver = {
1578 	.driver = {
1579 		.name = "anx7411",
1580 		.of_match_table = anx_match_table,
1581 		.pm = &anx7411_pm_ops,
1582 	},
1583 	.probe = anx7411_i2c_probe,
1584 	.remove = anx7411_i2c_remove,
1585 
1586 	.id_table = anx7411_id,
1587 };
1588 
1589 module_i2c_driver(anx7411_driver);
1590 
1591 MODULE_DESCRIPTION("Anx7411 USB Type-C PD driver");
1592 MODULE_AUTHOR("Xin Ji <xji@analogixsemi.com>");
1593 MODULE_LICENSE("GPL");
1594 MODULE_VERSION("0.1.5");
1595