xref: /linux/drivers/usb/typec/tipd/core.c (revision 88c7e458af53ff781a3f1051708395892b325576)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for TI TPS6598x USB Power Delivery controller family
4  *
5  * Copyright (C) 2017, Intel Corporation
6  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7  */
8 
9 #include <linux/i2c.h>
10 #include <linux/acpi.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/power_supply.h>
15 #include <linux/regmap.h>
16 #include <linux/interrupt.h>
17 #include <linux/usb/typec.h>
18 #include <linux/usb/typec_altmode.h>
19 #include <linux/usb/typec_dp.h>
20 #include <linux/usb/typec_mux.h>
21 #include <linux/usb/typec_tbt.h>
22 #include <linux/usb/role.h>
23 #include <linux/workqueue.h>
24 #include <linux/firmware.h>
25 
26 #include "tps6598x.h"
27 #include "trace.h"
28 
29 /* Register offsets */
30 #define TPS_REG_VID			0x00
31 #define TPS_REG_MODE			0x03
32 #define TPS_REG_CMD1			0x08
33 #define TPS_REG_DATA1			0x09
34 #define TPS_REG_VERSION			0x0F
35 #define TPS_REG_INT_EVENT1		0x14
36 #define TPS_REG_INT_EVENT2		0x15
37 #define TPS_REG_INT_MASK1		0x16
38 #define TPS_REG_INT_MASK2		0x17
39 #define TPS_REG_INT_CLEAR1		0x18
40 #define TPS_REG_INT_CLEAR2		0x19
41 #define TPS_REG_STATUS			0x1a
42 #define TPS_REG_SYSTEM_POWER_STATE	0x20
43 #define TPS_REG_USB4_STATUS		0x24
44 #define TPS_REG_SYSTEM_CONF		0x28
45 #define TPS_REG_CTRL_CONF		0x29
46 #define TPS_REG_BOOT_STATUS		0x2D
47 #define TPS_REG_POWER_STATUS		0x3f
48 #define TPS_REG_PD_STATUS		0x40
49 #define TPS_REG_RX_IDENTITY_SOP		0x48
50 #define TPS_REG_CF_VID_STATUS		0x5e
51 #define TPS_REG_DP_SID_STATUS		0x58
52 #define TPS_REG_INTEL_VID_STATUS	0x59
53 #define TPS_REG_DATA_STATUS		0x5f
54 #define TPS_REG_SLEEP_CONF		0x70
55 
56 /* TPS_REG_SYSTEM_CONF bits */
57 #define TPS_SYSCONF_PORTINFO(c)		((c) & 7)
58 
59 /*
60  * BPMs task timeout, recommended 5 seconds
61  * pg.48 TPS2575 Host Interface Technical Reference
62  * Manual (Rev. A)
63  * https://www.ti.com/lit/ug/slvuc05a/slvuc05a.pdf
64  */
65 #define TPS_BUNDLE_TIMEOUT	0x32
66 
67 /* BPMs return code */
68 #define TPS_TASK_BPMS_INVALID_BUNDLE_SIZE	0x4
69 #define TPS_TASK_BPMS_INVALID_SLAVE_ADDR	0x5
70 #define TPS_TASK_BPMS_INVALID_TIMEOUT		0x6
71 
72 /* PBMc data out */
73 #define TPS_PBMC_RC	0 /* Return code */
74 #define TPS_PBMC_DPCS	2 /* device patch complete status */
75 
76 /* reset de-assertion to ready for operation */
77 #define TPS_SETUP_MS			1000
78 
79 enum {
80 	TPS_PORTINFO_SINK,
81 	TPS_PORTINFO_SINK_ACCESSORY,
82 	TPS_PORTINFO_DRP_UFP,
83 	TPS_PORTINFO_DRP_UFP_DRD,
84 	TPS_PORTINFO_DRP_DFP,
85 	TPS_PORTINFO_DRP_DFP_DRD,
86 	TPS_PORTINFO_SOURCE,
87 };
88 
89 /* TPS_REG_RX_IDENTITY_SOP */
90 struct tps6598x_rx_identity_reg {
91 	u8 status;
92 	struct usb_pd_identity identity;
93 } __packed;
94 
95 /* TPS_REG_USB4_STATUS */
96 struct tps6598x_usb4_status_reg {
97 	u8 mode_status;
98 	__le32 eudo;
99 	__le32 unknown;
100 } __packed;
101 
102 /* TPS_REG_DP_SID_STATUS */
103 struct tps6598x_dp_sid_status_reg {
104 	u8 mode_status;
105 	__le32 status_tx;
106 	__le32 status_rx;
107 	__le32 configure;
108 	__le32 mode_data;
109 } __packed;
110 
111 /* TPS_REG_INTEL_VID_STATUS */
112 struct tps6598x_intel_vid_status_reg {
113 	u8 mode_status;
114 	__le32 attention_vdo;
115 	__le16 enter_vdo;
116 	__le16 device_mode;
117 } __packed;
118 
119 /* Standard Task return codes */
120 #define TPS_TASK_TIMEOUT		1
121 #define TPS_TASK_REJECTED		3
122 
123 /* Debounce delay for mode changes, in milliseconds */
124 #define CD321X_DEBOUNCE_DELAY_MS 500
125 
126 enum {
127 	TPS_MODE_APP,
128 	TPS_MODE_BOOT,
129 	TPS_MODE_BIST,
130 	TPS_MODE_DISC,
131 	TPS_MODE_PTCH,
132 	TPS_MODE_APP1,
133 };
134 
135 static const char *const modes[] = {
136 	[TPS_MODE_APP]	= "APP ",
137 	[TPS_MODE_BOOT]	= "BOOT",
138 	[TPS_MODE_BIST]	= "BIST",
139 	[TPS_MODE_DISC]	= "DISC",
140 	[TPS_MODE_PTCH] = "PTCH",
141 	[TPS_MODE_APP1]	= "APP1",
142 };
143 
144 /* Unrecognized commands will be replaced with "!CMD" */
145 #define INVALID_CMD(_cmd_)		(_cmd_ == 0x444d4321)
146 
147 struct tps6598x;
148 
149 struct tipd_data {
150 	irq_handler_t irq_handler;
151 	u64 irq_mask1;
152 	size_t tps_struct_size;
153 	void (*remove)(struct tps6598x *tps);
154 	int (*register_port)(struct tps6598x *tps, struct fwnode_handle *node);
155 	void (*unregister_port)(struct tps6598x *tps);
156 	void (*trace_data_status)(u32 status);
157 	void (*trace_power_status)(u16 status);
158 	void (*trace_status)(u32 status);
159 	int (*apply_patch)(struct tps6598x *tps);
160 	int (*init)(struct tps6598x *tps);
161 	int (*switch_power_state)(struct tps6598x *tps, u8 target_state);
162 	bool (*read_data_status)(struct tps6598x *tps);
163 	bool (*read_power_status)(struct tps6598x *tps);
164 	int (*reset)(struct tps6598x *tps);
165 	int (*connect)(struct tps6598x *tps, u32 status);
166 };
167 
168 struct tps6598x {
169 	struct device *dev;
170 	struct regmap *regmap;
171 	struct mutex lock; /* device lock */
172 	u8 i2c_protocol:1;
173 
174 	struct gpio_desc *reset;
175 	struct typec_port *port;
176 	struct typec_partner *partner;
177 	struct usb_pd_identity partner_identity;
178 	struct usb_role_switch *role_sw;
179 	struct typec_capability typec_cap;
180 
181 	struct power_supply *psy;
182 	struct power_supply_desc psy_desc;
183 	enum power_supply_usb_type usb_type;
184 
185 	int wakeup;
186 	u32 status; /* status reg */
187 	u32 data_status;
188 	u16 pwr_status;
189 	struct delayed_work	wq_poll;
190 
191 	const struct tipd_data *data;
192 };
193 
194 struct cd321x_status {
195 	u32 status;
196 	u32 pwr_status;
197 	u32 data_status;
198 	u32 status_changed;
199 	struct usb_pd_identity partner_identity;
200 	struct tps6598x_dp_sid_status_reg dp_sid_status;
201 	struct tps6598x_intel_vid_status_reg intel_vid_status;
202 	struct tps6598x_usb4_status_reg usb4_status;
203 };
204 
205 struct cd321x {
206 	struct tps6598x tps;
207 
208 	struct tps6598x_dp_sid_status_reg dp_sid_status;
209 	struct tps6598x_intel_vid_status_reg intel_vid_status;
210 	struct tps6598x_usb4_status_reg usb4_status;
211 
212 	struct typec_altmode *port_altmode_dp;
213 	struct typec_altmode *port_altmode_tbt;
214 
215 	struct typec_mux *mux;
216 	struct typec_mux_state state;
217 
218 	struct cd321x_status update_status;
219 	struct delayed_work update_work;
220 	struct usb_pd_identity cur_partner_identity;
221 };
222 
223 static enum power_supply_property tps6598x_psy_props[] = {
224 	POWER_SUPPLY_PROP_USB_TYPE,
225 	POWER_SUPPLY_PROP_ONLINE,
226 };
227 
228 static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-";
229 
230 /*
231  * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
232  * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
233  */
234 #define TPS_MAX_LEN	64
235 
236 static int
237 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
238 {
239 	u8 data[TPS_MAX_LEN + 1];
240 	int ret;
241 
242 	if (len + 1 > sizeof(data))
243 		return -EINVAL;
244 
245 	if (!tps->i2c_protocol)
246 		return regmap_raw_read(tps->regmap, reg, val, len);
247 
248 	ret = regmap_raw_read(tps->regmap, reg, data, len + 1);
249 	if (ret)
250 		return ret;
251 
252 	if (data[0] < len)
253 		return -EIO;
254 
255 	memcpy(val, &data[1], len);
256 	return 0;
257 }
258 
259 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
260 				const void *val, size_t len)
261 {
262 	u8 data[TPS_MAX_LEN + 1];
263 
264 	if (len + 1 > sizeof(data))
265 		return -EINVAL;
266 
267 	if (!tps->i2c_protocol)
268 		return regmap_raw_write(tps->regmap, reg, val, len);
269 
270 	data[0] = len;
271 	memcpy(&data[1], val, len);
272 
273 	return regmap_raw_write(tps->regmap, reg, data, len + 1);
274 }
275 
276 static inline int tps6598x_read8(struct tps6598x *tps, u8 reg, u8 *val)
277 {
278 	return tps6598x_block_read(tps, reg, val, sizeof(u8));
279 }
280 
281 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
282 {
283 	return tps6598x_block_read(tps, reg, val, sizeof(u16));
284 }
285 
286 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
287 {
288 	return tps6598x_block_read(tps, reg, val, sizeof(u32));
289 }
290 
291 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
292 {
293 	return tps6598x_block_read(tps, reg, val, sizeof(u64));
294 }
295 
296 static inline int tps6598x_write8(struct tps6598x *tps, u8 reg, u8 val)
297 {
298 	return tps6598x_block_write(tps, reg, &val, sizeof(u8));
299 }
300 
301 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
302 {
303 	return tps6598x_block_write(tps, reg, &val, sizeof(u64));
304 }
305 
306 static inline int
307 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
308 {
309 	return tps6598x_block_write(tps, reg, val, 4);
310 }
311 
312 static int tps6598x_read_partner_identity(struct tps6598x *tps)
313 {
314 	struct tps6598x_rx_identity_reg id;
315 	int ret;
316 
317 	ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
318 				  &id, sizeof(id));
319 	if (ret)
320 		return ret;
321 
322 	tps->partner_identity = id.identity;
323 
324 	return 0;
325 }
326 
327 static void tps6598x_set_data_role(struct tps6598x *tps,
328 				   enum typec_data_role role, bool connected)
329 {
330 	enum usb_role role_val;
331 
332 	if (role == TYPEC_HOST)
333 		role_val = USB_ROLE_HOST;
334 	else
335 		role_val = USB_ROLE_DEVICE;
336 
337 	if (!connected)
338 		role_val = USB_ROLE_NONE;
339 
340 	usb_role_switch_set_role(tps->role_sw, role_val);
341 	typec_set_data_role(tps->port, role);
342 }
343 
344 static int tps6598x_connect(struct tps6598x *tps, u32 status)
345 {
346 	struct typec_partner_desc desc = { };
347 	enum typec_pwr_opmode mode;
348 	int ret;
349 
350 	if (tps->partner)
351 		return 0;
352 
353 	mode = TPS_POWER_STATUS_PWROPMODE(tps->pwr_status);
354 
355 	desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
356 	desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
357 
358 	if (desc.usb_pd) {
359 		ret = tps6598x_read_partner_identity(tps);
360 		if (ret)
361 			return ret;
362 		desc.identity = &tps->partner_identity;
363 	}
364 
365 	typec_set_pwr_opmode(tps->port, mode);
366 	typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
367 	typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
368 	if (TPS_STATUS_TO_UPSIDE_DOWN(status))
369 		typec_set_orientation(tps->port, TYPEC_ORIENTATION_REVERSE);
370 	else
371 		typec_set_orientation(tps->port, TYPEC_ORIENTATION_NORMAL);
372 	typec_set_mode(tps->port, TYPEC_STATE_USB);
373 	tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), true);
374 
375 	tps->partner = typec_register_partner(tps->port, &desc);
376 	if (IS_ERR(tps->partner))
377 		return PTR_ERR(tps->partner);
378 
379 	if (desc.identity)
380 		typec_partner_set_identity(tps->partner);
381 
382 	power_supply_changed(tps->psy);
383 
384 	return 0;
385 }
386 
387 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
388 {
389 	if (!IS_ERR(tps->partner))
390 		typec_unregister_partner(tps->partner);
391 	tps->partner = NULL;
392 	typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
393 	typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
394 	typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
395 	typec_set_orientation(tps->port, TYPEC_ORIENTATION_NONE);
396 	typec_set_mode(tps->port, TYPEC_STATE_SAFE);
397 	tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), false);
398 
399 	power_supply_changed(tps->psy);
400 }
401 
402 static int tps6598x_exec_cmd_tmo(struct tps6598x *tps, const char *cmd,
403 			     size_t in_len, const u8 *in_data,
404 			     size_t out_len, u8 *out_data,
405 			     u32 cmd_timeout_ms, u32 res_delay_ms)
406 {
407 	unsigned long timeout;
408 	u32 val;
409 	int ret;
410 
411 	ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
412 	if (ret)
413 		return ret;
414 	if (val && !INVALID_CMD(val))
415 		return -EBUSY;
416 
417 	if (in_len) {
418 		ret = tps6598x_block_write(tps, TPS_REG_DATA1,
419 					   in_data, in_len);
420 		if (ret)
421 			return ret;
422 	}
423 
424 	ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
425 	if (ret < 0)
426 		return ret;
427 
428 	timeout = jiffies + msecs_to_jiffies(cmd_timeout_ms);
429 
430 	do {
431 		ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
432 		if (ret)
433 			return ret;
434 		if (INVALID_CMD(val))
435 			return -EINVAL;
436 
437 		if (time_is_before_jiffies(timeout))
438 			return -ETIMEDOUT;
439 	} while (val);
440 
441 	/* some commands require delay for the result to be available */
442 	mdelay(res_delay_ms);
443 
444 	if (out_len) {
445 		ret = tps6598x_block_read(tps, TPS_REG_DATA1,
446 					  out_data, out_len);
447 		if (ret)
448 			return ret;
449 		val = out_data[0];
450 	} else {
451 		ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
452 		if (ret)
453 			return ret;
454 	}
455 
456 	switch (val) {
457 	case TPS_TASK_TIMEOUT:
458 		return -ETIMEDOUT;
459 	case TPS_TASK_REJECTED:
460 		return -EPERM;
461 	default:
462 		break;
463 	}
464 
465 	return 0;
466 }
467 
468 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
469 			     size_t in_len, const u8 *in_data,
470 			     size_t out_len, u8 *out_data)
471 {
472 	return tps6598x_exec_cmd_tmo(tps, cmd, in_len, in_data,
473 				     out_len, out_data, 1000, 0);
474 }
475 
476 static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
477 {
478 	const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
479 	struct tps6598x *tps = typec_get_drvdata(port);
480 	u32 status;
481 	int ret;
482 
483 	mutex_lock(&tps->lock);
484 
485 	ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
486 	if (ret)
487 		goto out_unlock;
488 
489 	ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
490 	if (ret)
491 		goto out_unlock;
492 
493 	if (role != TPS_STATUS_TO_TYPEC_DATAROLE(status)) {
494 		ret = -EPROTO;
495 		goto out_unlock;
496 	}
497 
498 	tps6598x_set_data_role(tps, role, true);
499 
500 out_unlock:
501 	mutex_unlock(&tps->lock);
502 
503 	return ret;
504 }
505 
506 static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
507 {
508 	const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
509 	struct tps6598x *tps = typec_get_drvdata(port);
510 	u32 status;
511 	int ret;
512 
513 	mutex_lock(&tps->lock);
514 
515 	ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
516 	if (ret)
517 		goto out_unlock;
518 
519 	ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
520 	if (ret)
521 		goto out_unlock;
522 
523 	if (role != TPS_STATUS_TO_TYPEC_PORTROLE(status)) {
524 		ret = -EPROTO;
525 		goto out_unlock;
526 	}
527 
528 	typec_set_pwr_role(tps->port, role);
529 
530 out_unlock:
531 	mutex_unlock(&tps->lock);
532 
533 	return ret;
534 }
535 
536 static const struct typec_operations tps6598x_ops = {
537 	.dr_set = tps6598x_dr_set,
538 	.pr_set = tps6598x_pr_set,
539 };
540 
541 static bool tps6598x_read_status(struct tps6598x *tps, u32 *status)
542 {
543 	int ret;
544 
545 	ret = tps6598x_read32(tps, TPS_REG_STATUS, status);
546 	if (ret) {
547 		dev_err(tps->dev, "%s: failed to read status\n", __func__);
548 		return false;
549 	}
550 
551 	if (tps->data->trace_status)
552 		tps->data->trace_status(*status);
553 
554 	return true;
555 }
556 
557 static bool tps6598x_read_data_status(struct tps6598x *tps)
558 {
559 	u32 data_status;
560 	int ret;
561 
562 	ret = tps6598x_read32(tps, TPS_REG_DATA_STATUS, &data_status);
563 	if (ret < 0) {
564 		dev_err(tps->dev, "failed to read data status: %d\n", ret);
565 		return false;
566 	}
567 	tps->data_status = data_status;
568 
569 	if (tps->data->trace_data_status)
570 		tps->data->trace_data_status(data_status);
571 
572 	return true;
573 }
574 
575 static bool cd321x_read_data_status(struct tps6598x *tps)
576 {
577 	struct cd321x *cd321x = container_of(tps, struct cd321x, tps);
578 	int ret;
579 
580 	ret = tps6598x_read_data_status(tps);
581 	if (!ret)
582 		return false;
583 
584 	if (tps->data_status & TPS_DATA_STATUS_DP_CONNECTION) {
585 		ret = tps6598x_block_read(tps, TPS_REG_DP_SID_STATUS,
586 				&cd321x->dp_sid_status, sizeof(cd321x->dp_sid_status));
587 		if (ret) {
588 			dev_err(tps->dev, "Failed to read DP SID Status: %d\n",
589 				ret);
590 			return false;
591 		}
592 	}
593 
594 	if (tps->data_status & TPS_DATA_STATUS_TBT_CONNECTION) {
595 		ret = tps6598x_block_read(tps, TPS_REG_INTEL_VID_STATUS,
596 				&cd321x->intel_vid_status, sizeof(cd321x->intel_vid_status));
597 		if (ret) {
598 			dev_err(tps->dev, "Failed to read Intel VID Status: %d\n", ret);
599 			return false;
600 		}
601 	}
602 
603 	if (tps->data_status & CD321X_DATA_STATUS_USB4_CONNECTION) {
604 		ret = tps6598x_block_read(tps, TPS_REG_USB4_STATUS,
605 				&cd321x->usb4_status, sizeof(cd321x->usb4_status));
606 		if (ret) {
607 			dev_err(tps->dev,
608 				"Failed to read USB4 Status: %d\n", ret);
609 			return false;
610 		}
611 	}
612 
613 	return true;
614 }
615 
616 static bool tps6598x_read_power_status(struct tps6598x *tps)
617 {
618 	u16 pwr_status;
619 	int ret;
620 
621 	ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
622 	if (ret < 0) {
623 		dev_err(tps->dev, "failed to read power status: %d\n", ret);
624 		return false;
625 	}
626 	tps->pwr_status = pwr_status;
627 
628 	if (tps->data->trace_power_status)
629 		tps->data->trace_power_status(pwr_status);
630 
631 	return true;
632 }
633 
634 /*
635  * TPS66993 deprecated Power_Status register (0x3F). BC1.2 is not supported
636  * and the remaining bits are redundant with STATUS register (0x1A).
637  * Synthesize pwr_status from the already-read STATUS register.
638  */
639 static bool tps66993_read_power_status(struct tps6598x *tps)
640 {
641 	u16 pwr_status = 0;
642 
643 	/* Same masks as TPS_POWER_STATUS_CONNECTION() / SOURCESINK() / PWROPMODE() in tps6598x.h */
644 	if (tps->status & TPS_STATUS_PLUG_PRESENT)
645 		pwr_status |= FIELD_PREP(TPS_POWER_STATUS_CONNECTION_MASK, 1);
646 
647 	/* SOURCESINK: 1=sink; STATUS.PortRole 1=source, opposite convention */
648 	if (!TPS_STATUS_TO_TYPEC_PORTROLE(tps->status))
649 		pwr_status |= FIELD_PREP(TPS_POWER_STATUS_SOURCESINK_MASK, 1);
650 
651 	if (TPS_STATUS_VBUS_STATUS(tps->status) == TPS_STATUS_VBUS_STATUS_PD)
652 		pwr_status |= FIELD_PREP(TPS_POWER_STATUS_TYPEC_CURRENT_MASK,
653 					 TPS_POWER_STATUS_TYPEC_CURRENT_PD);
654 
655 	tps->pwr_status = pwr_status;
656 
657 	tps->data->trace_power_status(pwr_status);
658 
659 	return true;
660 }
661 
662 static void tps6598x_handle_plug_event(struct tps6598x *tps, u32 status)
663 {
664 	int ret;
665 
666 	if (status & TPS_STATUS_PLUG_PRESENT) {
667 		ret = tps6598x_connect(tps, status);
668 		if (ret)
669 			dev_err(tps->dev, "failed to register partner\n");
670 	} else {
671 		tps6598x_disconnect(tps, status);
672 	}
673 }
674 
675 static void cd321x_typec_update_mode(struct tps6598x *tps, struct cd321x_status *st)
676 {
677 	struct cd321x *cd321x = container_of(tps, struct cd321x, tps);
678 
679 	if (!(st->data_status & TPS_DATA_STATUS_DATA_CONNECTION)) {
680 		if (cd321x->state.mode == TYPEC_STATE_SAFE)
681 			return;
682 		cd321x->state.alt = NULL;
683 		cd321x->state.mode = TYPEC_STATE_SAFE;
684 		cd321x->state.data = NULL;
685 		typec_mux_set(cd321x->mux, &cd321x->state);
686 	} else if (st->data_status & TPS_DATA_STATUS_DP_CONNECTION) {
687 		struct typec_displayport_data dp_data;
688 		unsigned long mode;
689 
690 		switch (TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT(st->data_status)) {
691 		case TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_A:
692 			mode = TYPEC_DP_STATE_A;
693 			break;
694 		case TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_B:
695 			mode = TYPEC_DP_STATE_B;
696 			break;
697 		case TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_C:
698 			mode = TYPEC_DP_STATE_C;
699 			break;
700 		case TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_D:
701 			mode = TYPEC_DP_STATE_D;
702 			break;
703 		case TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_E:
704 			mode = TYPEC_DP_STATE_E;
705 			break;
706 		case TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_F:
707 			mode = TYPEC_DP_STATE_F;
708 			break;
709 		default:
710 			dev_err(tps->dev, "Invalid DP pin assignment\n");
711 			return;
712 		}
713 
714 		if (cd321x->state.alt == cd321x->port_altmode_dp &&
715 		   cd321x->state.mode == mode) {
716 			return;
717 		}
718 
719 		dp_data.status = le32_to_cpu(st->dp_sid_status.status_rx);
720 		dp_data.conf = le32_to_cpu(st->dp_sid_status.configure);
721 		cd321x->state.alt = cd321x->port_altmode_dp;
722 		cd321x->state.data = &dp_data;
723 		cd321x->state.mode = mode;
724 		typec_mux_set(cd321x->mux, &cd321x->state);
725 	} else if (st->data_status & TPS_DATA_STATUS_TBT_CONNECTION) {
726 		struct typec_thunderbolt_data tbt_data;
727 
728 		if (cd321x->state.alt == cd321x->port_altmode_tbt &&
729 		   cd321x->state.mode == TYPEC_TBT_MODE)
730 			return;
731 
732 		tbt_data.cable_mode = TBT_MODE |
733 			TBT_SET_CABLE_SPEED(TPS_DATA_STATUS_TBT_CABLE_SPEED(st->data_status)) |
734 			TBT_SET_CABLE_ROUNDED(TPS_DATA_STATUS_TBT_CABLE_GEN(st->data_status));
735 		if (st->data_status & TPS_DATA_STATUS_OPTICAL_CABLE)
736 			tbt_data.cable_mode |= TBT_CABLE_OPTICAL;
737 		if (st->data_status & TPS_DATA_STATUS_ACTIVE_LINK_TRAIN)
738 			tbt_data.cable_mode |= TBT_CABLE_LINK_TRAINING;
739 		if (st->data_status & TPS_DATA_STATUS_ACTIVE_CABLE)
740 			tbt_data.cable_mode |= TBT_CABLE_ACTIVE_PASSIVE;
741 		tbt_data.device_mode = TBT_MODE |
742 			(u32)le16_to_cpu(st->intel_vid_status.device_mode) << 16;
743 		tbt_data.enter_vdo =
744 			(u32)le16_to_cpu(st->intel_vid_status.enter_vdo) << 16;
745 		cd321x->state.alt = cd321x->port_altmode_tbt;
746 		cd321x->state.mode = TYPEC_TBT_MODE;
747 		cd321x->state.data = &tbt_data;
748 		typec_mux_set(cd321x->mux, &cd321x->state);
749 	} else if (st->data_status & CD321X_DATA_STATUS_USB4_CONNECTION) {
750 		struct enter_usb_data eusb_data;
751 
752 		if (cd321x->state.alt == NULL && cd321x->state.mode == TYPEC_MODE_USB4)
753 			return;
754 
755 		eusb_data.eudo = le32_to_cpu(st->usb4_status.eudo);
756 		eusb_data.active_link_training =
757 			!!(st->data_status & TPS_DATA_STATUS_ACTIVE_LINK_TRAIN);
758 
759 		cd321x->state.alt = NULL;
760 		cd321x->state.data = &eusb_data;
761 		cd321x->state.mode = TYPEC_MODE_USB4;
762 		typec_mux_set(cd321x->mux, &cd321x->state);
763 	} else {
764 		if (cd321x->state.alt == NULL && cd321x->state.mode == TYPEC_STATE_USB)
765 			return;
766 		cd321x->state.alt = NULL;
767 		cd321x->state.mode = TYPEC_STATE_USB;
768 		cd321x->state.data = NULL;
769 		typec_mux_set(cd321x->mux, &cd321x->state);
770 	}
771 
772 	/* Clear data since it's no longer used after typec_mux_set and points to the stack */
773 	cd321x->state.data = NULL;
774 }
775 
776 static void cd321x_update_work(struct work_struct *work)
777 {
778 	struct cd321x *cd321x = container_of(to_delayed_work(work),
779 					    struct cd321x, update_work);
780 	struct tps6598x *tps = &cd321x->tps;
781 	struct cd321x_status st;
782 
783 	guard(mutex)(&tps->lock);
784 
785 	st = cd321x->update_status;
786 	cd321x->update_status.status_changed = 0;
787 
788 	bool old_connected = !!tps->partner;
789 	bool new_connected = st.status & TPS_STATUS_PLUG_PRESENT;
790 	bool was_disconnected = st.status_changed & TPS_STATUS_PLUG_PRESENT;
791 
792 	bool usb_connection = st.data_status &
793 			      (TPS_DATA_STATUS_USB2_CONNECTION | TPS_DATA_STATUS_USB3_CONNECTION);
794 
795 	enum usb_role old_role = usb_role_switch_get_role(tps->role_sw);
796 	enum usb_role new_role = USB_ROLE_NONE;
797 	enum typec_pwr_opmode pwr_opmode = TYPEC_PWR_MODE_USB;
798 	enum typec_orientation orientation = TYPEC_ORIENTATION_NONE;
799 
800 	if (usb_connection) {
801 		if (tps->data_status & TPS_DATA_STATUS_USB_DATA_ROLE)
802 			new_role = USB_ROLE_DEVICE;
803 		else
804 			new_role = USB_ROLE_HOST;
805 	}
806 
807 	if (new_connected) {
808 		pwr_opmode = TPS_POWER_STATUS_PWROPMODE(st.pwr_status);
809 		orientation = TPS_STATUS_TO_UPSIDE_DOWN(st.status) ?
810 			TYPEC_ORIENTATION_REVERSE : TYPEC_ORIENTATION_NORMAL;
811 	}
812 
813 	bool is_pd = pwr_opmode == TYPEC_PWR_MODE_PD;
814 	bool partner_changed = old_connected && new_connected &&
815 		(was_disconnected ||
816 		 (is_pd && memcmp(&st.partner_identity,
817 				  &cd321x->cur_partner_identity, sizeof(struct usb_pd_identity))));
818 
819 	/* If we are switching from an active role, transition to USB_ROLE_NONE first */
820 	if (old_role != USB_ROLE_NONE && (new_role != old_role || was_disconnected))
821 		usb_role_switch_set_role(tps->role_sw, USB_ROLE_NONE);
822 
823 	/* Process partner disconnection or change */
824 	if (!new_connected || partner_changed) {
825 		if (!IS_ERR(tps->partner))
826 			typec_unregister_partner(tps->partner);
827 		tps->partner = NULL;
828 	}
829 
830 	/* If there was a disconnection, set PHY to off */
831 	if (!new_connected || was_disconnected) {
832 		cd321x->state.alt = NULL;
833 		cd321x->state.mode = TYPEC_STATE_SAFE;
834 		cd321x->state.data = NULL;
835 		typec_set_mode(tps->port, TYPEC_STATE_SAFE);
836 	}
837 
838 	/* Update Type-C properties */
839 	typec_set_pwr_opmode(tps->port, pwr_opmode);
840 	typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(st.status));
841 	typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(st.status));
842 	typec_set_orientation(tps->port, orientation);
843 	typec_set_data_role(tps->port, TPS_STATUS_TO_TYPEC_DATAROLE(st.status));
844 	power_supply_changed(tps->psy);
845 
846 	/* If the plug is disconnected, we are done */
847 	if (!new_connected)
848 		return;
849 
850 	/* Set up partner if we were previously disconnected (or changed). */
851 	if (!tps->partner) {
852 		struct typec_partner_desc desc = { };
853 
854 		desc.usb_pd = is_pd;
855 		desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
856 
857 		if (desc.usb_pd)
858 			desc.identity = &st.partner_identity;
859 
860 		tps->partner = typec_register_partner(tps->port, &desc);
861 		if (IS_ERR(tps->partner)) {
862 			dev_warn(tps->dev, "%s: failed to register partner\n", __func__);
863 			return;
864 		}
865 
866 		if (desc.identity) {
867 			typec_partner_set_identity(tps->partner);
868 			cd321x->cur_partner_identity = st.partner_identity;
869 		}
870 	}
871 
872 	/* Update the TypeC MUX/PHY state */
873 	cd321x_typec_update_mode(tps, &st);
874 
875 	/* Launch the USB role switch */
876 	usb_role_switch_set_role(tps->role_sw, new_role);
877 
878 	power_supply_changed(tps->psy);
879 }
880 
881 static void cd321x_queue_status(struct cd321x *cd321x)
882 {
883 	cd321x->update_status.status_changed |= cd321x->update_status.status ^ cd321x->tps.status;
884 
885 	cd321x->update_status.status = cd321x->tps.status;
886 	cd321x->update_status.pwr_status = cd321x->tps.pwr_status;
887 	cd321x->update_status.data_status = cd321x->tps.data_status;
888 
889 	cd321x->update_status.partner_identity = cd321x->tps.partner_identity;
890 	cd321x->update_status.dp_sid_status = cd321x->dp_sid_status;
891 	cd321x->update_status.intel_vid_status = cd321x->intel_vid_status;
892 	cd321x->update_status.usb4_status = cd321x->usb4_status;
893 }
894 
895 static int cd321x_connect(struct tps6598x *tps, u32 status)
896 {
897 	struct cd321x *cd321x = container_of(tps, struct cd321x, tps);
898 
899 	tps->status = status;
900 	cd321x_queue_status(cd321x);
901 
902 	/*
903 	 * Cancel pending work if not already running, then requeue after CD321X_DEBOUNCE_DELAY_MS
904 	 * regardless since the work function will check for any plug or altmodes changes since
905 	 * its last run anyway.
906 	 */
907 	cancel_delayed_work(&cd321x->update_work);
908 	schedule_delayed_work(&cd321x->update_work, msecs_to_jiffies(CD321X_DEBOUNCE_DELAY_MS));
909 
910 	return 0;
911 }
912 
913 static irqreturn_t cd321x_interrupt(int irq, void *data)
914 {
915 	struct tps6598x *tps = data;
916 	u64 event = 0;
917 	u32 status;
918 	int ret;
919 
920 	mutex_lock(&tps->lock);
921 
922 	ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event);
923 	if (ret) {
924 		dev_err(tps->dev, "%s: failed to read events\n", __func__);
925 		goto err_unlock;
926 	}
927 	trace_cd321x_irq(event);
928 
929 	if (!event)
930 		goto err_unlock;
931 
932 	tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event);
933 
934 	if (!tps6598x_read_status(tps, &status))
935 		goto err_unlock;
936 
937 	if (event & APPLE_CD_REG_INT_POWER_STATUS_UPDATE) {
938 		if (!tps->data->read_power_status(tps))
939 			goto err_unlock;
940 		if (TPS_POWER_STATUS_PWROPMODE(tps->pwr_status) == TYPEC_PWR_MODE_PD) {
941 			if (tps6598x_read_partner_identity(tps)) {
942 				dev_err(tps->dev, "failed to read partner identity\n");
943 				tps->partner_identity = (struct usb_pd_identity) {0};
944 			}
945 		}
946 	}
947 
948 	if (event & APPLE_CD_REG_INT_DATA_STATUS_UPDATE)
949 		if (!tps->data->read_data_status(tps))
950 			goto err_unlock;
951 
952 	/* Can be called uncondtionally since it will check for any changes itself */
953 	cd321x_connect(tps, status);
954 
955 err_unlock:
956 	mutex_unlock(&tps->lock);
957 
958 	if (event)
959 		return IRQ_HANDLED;
960 	return IRQ_NONE;
961 }
962 
963 static bool tps6598x_has_role_changed(struct tps6598x *tps, u32 status)
964 {
965 	status ^= tps->status;
966 
967 	return status & (TPS_STATUS_PORTROLE | TPS_STATUS_DATAROLE);
968 }
969 
970 static irqreturn_t tps25750_interrupt(int irq, void *data)
971 {
972 	struct tps6598x *tps = data;
973 	u64 event[2] = { };
974 	u32 status;
975 	int ret;
976 
977 	mutex_lock(&tps->lock);
978 
979 	ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event, 11);
980 	if (ret) {
981 		dev_err(tps->dev, "%s: failed to read events\n", __func__);
982 		goto err_unlock;
983 	}
984 	trace_tps25750_irq(event[0]);
985 
986 	if (!(event[0] | event[1]))
987 		goto err_unlock;
988 
989 	if (!tps6598x_read_status(tps, &status))
990 		goto err_clear_ints;
991 
992 	if (event[0] & TPS_REG_INT_POWER_STATUS_UPDATE)
993 		if (!tps->data->read_power_status(tps))
994 			goto err_clear_ints;
995 
996 	if (event[0] & TPS_REG_INT_DATA_STATUS_UPDATE)
997 		if (!tps->data->read_data_status(tps))
998 			goto err_clear_ints;
999 
1000 	/*
1001 	 * data/port roles could be updated independently after
1002 	 * a plug event. Therefore, we need to check
1003 	 * for pr/dr status change to set TypeC dr/pr accordingly.
1004 	 */
1005 	if (event[0] & TPS_REG_INT_PLUG_EVENT ||
1006 	    tps6598x_has_role_changed(tps, status))
1007 		tps6598x_handle_plug_event(tps, status);
1008 
1009 	tps->status = status;
1010 
1011 err_clear_ints:
1012 	tps6598x_block_write(tps, TPS_REG_INT_CLEAR1, event, 11);
1013 
1014 err_unlock:
1015 	mutex_unlock(&tps->lock);
1016 
1017 	if (event[0] | event[1])
1018 		return IRQ_HANDLED;
1019 	return IRQ_NONE;
1020 }
1021 
1022 static irqreturn_t tps6598x_interrupt(int irq, void *data)
1023 {
1024 	int intev_len = TPS_65981_2_6_INTEVENT_LEN;
1025 	struct tps6598x *tps = data;
1026 	u64 event1[2] = { };
1027 	u64 event2[2] = { };
1028 	u32 version;
1029 	u32 status;
1030 	int ret;
1031 
1032 	mutex_lock(&tps->lock);
1033 
1034 	ret = tps6598x_read32(tps, TPS_REG_VERSION, &version);
1035 	if (ret)
1036 		dev_warn(tps->dev, "%s: failed to read version (%d)\n",
1037 			 __func__, ret);
1038 
1039 	if (TPS_VERSION_HW_VERSION(version) == TPS_VERSION_HW_65987_8_DH ||
1040 	    TPS_VERSION_HW_VERSION(version) == TPS_VERSION_HW_65987_8_DK)
1041 		intev_len = TPS_65987_8_INTEVENT_LEN;
1042 
1043 	ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event1, intev_len);
1044 
1045 	ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event1, intev_len);
1046 	if (ret) {
1047 		dev_err(tps->dev, "%s: failed to read event1\n", __func__);
1048 		goto err_unlock;
1049 	}
1050 	ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT2, event2, intev_len);
1051 	if (ret) {
1052 		dev_err(tps->dev, "%s: failed to read event2\n", __func__);
1053 		goto err_unlock;
1054 	}
1055 	trace_tps6598x_irq(event1[0], event2[0]);
1056 
1057 	if (!(event1[0] | event1[1] | event2[0] | event2[1]))
1058 		goto err_unlock;
1059 
1060 	tps6598x_block_write(tps, TPS_REG_INT_CLEAR1, event1, intev_len);
1061 	tps6598x_block_write(tps, TPS_REG_INT_CLEAR2, event2, intev_len);
1062 
1063 	if (!tps6598x_read_status(tps, &status))
1064 		goto err_unlock;
1065 
1066 	tps->status = status;
1067 
1068 	if ((event1[0] | event2[0]) & TPS_REG_INT_POWER_STATUS_UPDATE)
1069 		if (!tps->data->read_power_status(tps))
1070 			goto err_unlock;
1071 
1072 	if ((event1[0] | event2[0]) & TPS_REG_INT_DATA_STATUS_UPDATE)
1073 		if (!tps->data->read_data_status(tps))
1074 			goto err_unlock;
1075 
1076 	/*
1077 	 * Refresh power status before connect - needed for TPS66993 which
1078 	 * synthesizes pwr_status from STATUS and never gets POWER_STATUS_UPDATE.
1079 	 */
1080 	if ((event1[0] | event2[0]) & TPS_REG_INT_PLUG_EVENT) {
1081 		if (!tps->data->read_power_status(tps))
1082 			goto err_unlock;
1083 		tps6598x_handle_plug_event(tps, status);
1084 	}
1085 
1086 err_unlock:
1087 	mutex_unlock(&tps->lock);
1088 
1089 	if (event1[0] | event1[1] | event2[0] | event2[1])
1090 		return IRQ_HANDLED;
1091 
1092 	return IRQ_NONE;
1093 }
1094 
1095 /* Time interval for Polling */
1096 #define POLL_INTERVAL	500 /* msecs */
1097 static void tps6598x_poll_work(struct work_struct *work)
1098 {
1099 	struct tps6598x *tps = container_of(to_delayed_work(work),
1100 					    struct tps6598x, wq_poll);
1101 
1102 	tps->data->irq_handler(0, tps);
1103 	queue_delayed_work(system_power_efficient_wq,
1104 			   &tps->wq_poll, msecs_to_jiffies(POLL_INTERVAL));
1105 }
1106 
1107 static int tps6598x_check_mode(struct tps6598x *tps)
1108 {
1109 	char mode[5] = { };
1110 	int ret;
1111 
1112 	ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
1113 	if (ret)
1114 		return ret;
1115 
1116 	ret = match_string(modes, ARRAY_SIZE(modes), mode);
1117 
1118 	switch (ret) {
1119 	case TPS_MODE_APP:
1120 	case TPS_MODE_APP1:
1121 	case TPS_MODE_PTCH:
1122 		return ret;
1123 	case TPS_MODE_BOOT:
1124 		dev_warn(tps->dev, "dead-battery condition\n");
1125 		return ret;
1126 	case TPS_MODE_BIST:
1127 	case TPS_MODE_DISC:
1128 	default:
1129 		dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
1130 			mode);
1131 		break;
1132 	}
1133 
1134 	return -ENODEV;
1135 }
1136 
1137 static const struct regmap_config tps6598x_regmap_config = {
1138 	.reg_bits = 8,
1139 	.val_bits = 8,
1140 	.max_register = 0x7F,
1141 };
1142 
1143 static int tps6598x_psy_get_online(struct tps6598x *tps,
1144 				   union power_supply_propval *val)
1145 {
1146 	if (TPS_POWER_STATUS_CONNECTION(tps->pwr_status) &&
1147 	    TPS_POWER_STATUS_SOURCESINK(tps->pwr_status)) {
1148 		val->intval = 1;
1149 	} else {
1150 		val->intval = 0;
1151 	}
1152 	return 0;
1153 }
1154 
1155 static int tps6598x_psy_get_prop(struct power_supply *psy,
1156 				 enum power_supply_property psp,
1157 				 union power_supply_propval *val)
1158 {
1159 	struct tps6598x *tps = power_supply_get_drvdata(psy);
1160 	int ret = 0;
1161 
1162 	switch (psp) {
1163 	case POWER_SUPPLY_PROP_USB_TYPE:
1164 		if (TPS_POWER_STATUS_PWROPMODE(tps->pwr_status) == TYPEC_PWR_MODE_PD)
1165 			val->intval = POWER_SUPPLY_USB_TYPE_PD;
1166 		else
1167 			val->intval = POWER_SUPPLY_USB_TYPE_C;
1168 		break;
1169 	case POWER_SUPPLY_PROP_ONLINE:
1170 		ret = tps6598x_psy_get_online(tps, val);
1171 		break;
1172 	default:
1173 		ret = -EINVAL;
1174 		break;
1175 	}
1176 
1177 	return ret;
1178 }
1179 
1180 static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state)
1181 {
1182 	u8 state;
1183 	int ret;
1184 
1185 	ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
1186 	if (ret)
1187 		return ret;
1188 
1189 	if (state == target_state)
1190 		return 0;
1191 
1192 	ret = tps6598x_exec_cmd(tps, "SSPS", sizeof(u8), &target_state, 0, NULL);
1193 	if (ret)
1194 		return ret;
1195 
1196 	ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
1197 	if (ret)
1198 		return ret;
1199 
1200 	if (state != target_state)
1201 		return -EINVAL;
1202 
1203 	return 0;
1204 }
1205 
1206 static int devm_tps6598_psy_register(struct tps6598x *tps)
1207 {
1208 	struct power_supply_config psy_cfg = {};
1209 	const char *port_dev_name = dev_name(tps->dev);
1210 	char *psy_name;
1211 
1212 	psy_cfg.drv_data = tps;
1213 	psy_cfg.fwnode = dev_fwnode(tps->dev);
1214 
1215 	psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
1216 				  port_dev_name);
1217 	if (!psy_name)
1218 		return -ENOMEM;
1219 
1220 	tps->psy_desc.name = psy_name;
1221 	tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
1222 	tps->psy_desc.usb_types = BIT(POWER_SUPPLY_USB_TYPE_C) |
1223 				  BIT(POWER_SUPPLY_USB_TYPE_PD);
1224 	tps->psy_desc.properties = tps6598x_psy_props;
1225 	tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
1226 	tps->psy_desc.get_property = tps6598x_psy_get_prop;
1227 
1228 	tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
1229 
1230 	tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
1231 					       &psy_cfg);
1232 	return PTR_ERR_OR_ZERO(tps->psy);
1233 }
1234 
1235 static int
1236 tps6598x_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
1237 {
1238 	int ret;
1239 	u32 conf;
1240 	struct typec_capability typec_cap = { };
1241 
1242 	ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
1243 	if (ret)
1244 		return ret;
1245 
1246 	typec_cap.revision = USB_TYPEC_REV_1_2;
1247 	typec_cap.pd_revision = 0x200;
1248 	typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
1249 	typec_cap.driver_data = tps;
1250 	typec_cap.ops = &tps6598x_ops;
1251 	typec_cap.fwnode = fwnode;
1252 
1253 	switch (TPS_SYSCONF_PORTINFO(conf)) {
1254 	case TPS_PORTINFO_SINK_ACCESSORY:
1255 	case TPS_PORTINFO_SINK:
1256 		typec_cap.type = TYPEC_PORT_SNK;
1257 		typec_cap.data = TYPEC_PORT_UFP;
1258 		break;
1259 	case TPS_PORTINFO_DRP_UFP_DRD:
1260 	case TPS_PORTINFO_DRP_DFP_DRD:
1261 		typec_cap.type = TYPEC_PORT_DRP;
1262 		typec_cap.data = TYPEC_PORT_DRD;
1263 		break;
1264 	case TPS_PORTINFO_DRP_UFP:
1265 		typec_cap.type = TYPEC_PORT_DRP;
1266 		typec_cap.data = TYPEC_PORT_UFP;
1267 		break;
1268 	case TPS_PORTINFO_DRP_DFP:
1269 		typec_cap.type = TYPEC_PORT_DRP;
1270 		typec_cap.data = TYPEC_PORT_DFP;
1271 		break;
1272 	case TPS_PORTINFO_SOURCE:
1273 		typec_cap.type = TYPEC_PORT_SRC;
1274 		typec_cap.data = TYPEC_PORT_DFP;
1275 		break;
1276 	default:
1277 		return -ENODEV;
1278 	}
1279 
1280 	tps->port = typec_register_port(tps->dev, &typec_cap);
1281 	if (IS_ERR(tps->port))
1282 		return PTR_ERR(tps->port);
1283 
1284 	return 0;
1285 }
1286 
1287 static int cd321x_register_port_altmodes(struct cd321x *cd321x)
1288 {
1289 	struct typec_altmode_desc desc;
1290 	struct typec_altmode *amode;
1291 
1292 	memset(&desc, 0, sizeof(desc));
1293 	desc.svid = USB_TYPEC_DP_SID;
1294 	desc.mode = USB_TYPEC_DP_MODE;
1295 	desc.vdo = DP_CONF_SET_PIN_ASSIGN(BIT(DP_PIN_ASSIGN_C) | BIT(DP_PIN_ASSIGN_D));
1296 	desc.vdo |= DP_CAP_DFP_D;
1297 	amode = typec_port_register_altmode(cd321x->tps.port, &desc);
1298 	if (IS_ERR(amode))
1299 		return PTR_ERR(amode);
1300 	cd321x->port_altmode_dp = amode;
1301 
1302 	memset(&desc, 0, sizeof(desc));
1303 	desc.svid = USB_TYPEC_TBT_SID;
1304 	desc.mode = TYPEC_ANY_MODE;
1305 	amode = typec_port_register_altmode(cd321x->tps.port, &desc);
1306 	if (IS_ERR(amode)) {
1307 		typec_unregister_altmode(cd321x->port_altmode_dp);
1308 		cd321x->port_altmode_dp = NULL;
1309 		return PTR_ERR(amode);
1310 	}
1311 	cd321x->port_altmode_tbt = amode;
1312 
1313 	return 0;
1314 }
1315 
1316 static int
1317 cd321x_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
1318 {
1319 	struct cd321x *cd321x = container_of(tps, struct cd321x, tps);
1320 	int ret;
1321 
1322 	INIT_DELAYED_WORK(&cd321x->update_work, cd321x_update_work);
1323 
1324 	ret = tps6598x_register_port(tps, fwnode);
1325 	if (ret)
1326 		return ret;
1327 
1328 	ret = cd321x_register_port_altmodes(cd321x);
1329 	if (ret)
1330 		goto err_unregister_port;
1331 
1332 	cd321x->mux = fwnode_typec_mux_get(fwnode);
1333 	if (IS_ERR(cd321x->mux)) {
1334 		ret = PTR_ERR(cd321x->mux);
1335 		goto err_unregister_altmodes;
1336 	}
1337 
1338 	cd321x->state.alt = NULL;
1339 	cd321x->state.mode = TYPEC_STATE_SAFE;
1340 	cd321x->state.data = NULL;
1341 	typec_set_mode(tps->port, TYPEC_STATE_SAFE);
1342 
1343 	return 0;
1344 
1345 err_unregister_altmodes:
1346 	typec_unregister_altmode(cd321x->port_altmode_dp);
1347 	typec_unregister_altmode(cd321x->port_altmode_tbt);
1348 	cd321x->port_altmode_dp = NULL;
1349 	cd321x->port_altmode_tbt = NULL;
1350 err_unregister_port:
1351 	typec_unregister_port(tps->port);
1352 	return ret;
1353 }
1354 
1355 static void
1356 tps6598x_unregister_port(struct tps6598x *tps)
1357 {
1358 	typec_unregister_port(tps->port);
1359 }
1360 
1361 static void
1362 cd321x_unregister_port(struct tps6598x *tps)
1363 {
1364 	struct cd321x *cd321x = container_of(tps, struct cd321x, tps);
1365 
1366 	typec_mux_put(cd321x->mux);
1367 	cd321x->mux = NULL;
1368 	typec_unregister_altmode(cd321x->port_altmode_dp);
1369 	cd321x->port_altmode_dp = NULL;
1370 	typec_unregister_altmode(cd321x->port_altmode_tbt);
1371 	cd321x->port_altmode_tbt = NULL;
1372 	typec_unregister_port(tps->port);
1373 }
1374 
1375 static int tps_request_firmware(struct tps6598x *tps, const struct firmware **fw,
1376 				const char **firmware_name)
1377 {
1378 	int ret;
1379 
1380 	ret = device_property_read_string(tps->dev, "firmware-name",
1381 					  firmware_name);
1382 	if (ret)
1383 		return ret;
1384 
1385 	ret = request_firmware(fw, *firmware_name, tps->dev);
1386 	if (ret) {
1387 		dev_err(tps->dev, "failed to retrieve \"%s\"\n", *firmware_name);
1388 		return ret;
1389 	}
1390 
1391 	if ((*fw)->size == 0) {
1392 		release_firmware(*fw);
1393 		ret = -EINVAL;
1394 	}
1395 
1396 	return ret;
1397 }
1398 
1399 static int
1400 tps25750_write_firmware(struct tps6598x *tps,
1401 			u8 bpms_addr, const u8 *data, size_t len)
1402 {
1403 	struct i2c_client *client = to_i2c_client(tps->dev);
1404 	int ret;
1405 	u8 slave_addr;
1406 	int timeout;
1407 
1408 	slave_addr = client->addr;
1409 	timeout = client->adapter->timeout;
1410 
1411 	/*
1412 	 * binary configuration size is around ~16Kbytes
1413 	 * which might take some time to finish writing it
1414 	 */
1415 	client->adapter->timeout = msecs_to_jiffies(5000);
1416 	client->addr = bpms_addr;
1417 
1418 	ret = regmap_raw_write(tps->regmap, data[0], &data[1], len - 1);
1419 
1420 	client->addr = slave_addr;
1421 	client->adapter->timeout = timeout;
1422 
1423 	return ret;
1424 }
1425 
1426 static int
1427 tps25750_exec_pbms(struct tps6598x *tps, u8 *in_data, size_t in_len)
1428 {
1429 	int ret;
1430 	u8 rc;
1431 
1432 	ret = tps6598x_exec_cmd_tmo(tps, "PBMs", in_len, in_data,
1433 				    sizeof(rc), &rc, 4000, 0);
1434 	if (ret)
1435 		return ret;
1436 
1437 	switch (rc) {
1438 	case TPS_TASK_BPMS_INVALID_BUNDLE_SIZE:
1439 		dev_err(tps->dev, "%s: invalid fw size\n", __func__);
1440 		return -EINVAL;
1441 	case TPS_TASK_BPMS_INVALID_SLAVE_ADDR:
1442 		dev_err(tps->dev, "%s: invalid slave address\n", __func__);
1443 		return -EINVAL;
1444 	case TPS_TASK_BPMS_INVALID_TIMEOUT:
1445 		dev_err(tps->dev, "%s: timed out\n", __func__);
1446 		return -ETIMEDOUT;
1447 	default:
1448 		break;
1449 	}
1450 
1451 	return 0;
1452 }
1453 
1454 static int tps25750_abort_patch_process(struct tps6598x *tps)
1455 {
1456 	int ret;
1457 
1458 	ret = tps6598x_exec_cmd(tps, "PBMe", 0, NULL, 0, NULL);
1459 	if (ret)
1460 		return ret;
1461 
1462 	ret = tps6598x_check_mode(tps);
1463 	if (ret != TPS_MODE_PTCH)
1464 		dev_err(tps->dev, "failed to switch to \"PTCH\" mode\n");
1465 
1466 	return ret;
1467 }
1468 
1469 static int tps25750_start_patch_burst_mode(struct tps6598x *tps)
1470 {
1471 	int ret;
1472 	const struct firmware *fw;
1473 	const char *firmware_name;
1474 	struct {
1475 		u32 fw_size;
1476 		u8 addr;
1477 		u8 timeout;
1478 	} __packed bpms_data;
1479 	u32 addr;
1480 	struct device_node *np = tps->dev->of_node;
1481 
1482 	ret = tps_request_firmware(tps, &fw, &firmware_name);
1483 	if (ret)
1484 		return ret;
1485 
1486 	ret = of_property_match_string(np, "reg-names", "patch-address");
1487 	if (ret < 0) {
1488 		dev_err(tps->dev, "failed to get patch-address %d\n", ret);
1489 		goto release_fw;
1490 	}
1491 
1492 	ret = of_property_read_u32_index(np, "reg", ret, &addr);
1493 	if (ret)
1494 		goto release_fw;
1495 
1496 	if (addr == 0 || (addr >= 0x20 && addr <= 0x23)) {
1497 		dev_err(tps->dev, "wrong patch address %u\n", addr);
1498 		ret = -EINVAL;
1499 		goto release_fw;
1500 	}
1501 
1502 	bpms_data.addr = (u8)addr;
1503 	bpms_data.fw_size = fw->size;
1504 	bpms_data.timeout = TPS_BUNDLE_TIMEOUT;
1505 
1506 	ret = tps25750_exec_pbms(tps, (u8 *)&bpms_data, sizeof(bpms_data));
1507 	if (ret)
1508 		goto release_fw;
1509 
1510 	ret = tps25750_write_firmware(tps, bpms_data.addr, fw->data, fw->size);
1511 	if (ret) {
1512 		dev_err(tps->dev, "Failed to write patch %s of %zu bytes\n",
1513 			firmware_name, fw->size);
1514 		goto release_fw;
1515 	}
1516 
1517 	/*
1518 	 * A delay of 500us is required after the firmware is written
1519 	 * based on pg.62 in tps6598x Host Interface Technical
1520 	 * Reference Manual
1521 	 * https://www.ti.com/lit/ug/slvuc05a/slvuc05a.pdf
1522 	 */
1523 	udelay(500);
1524 
1525 release_fw:
1526 	release_firmware(fw);
1527 
1528 	return ret;
1529 }
1530 
1531 static int tps25750_complete_patch_process(struct tps6598x *tps)
1532 {
1533 	int ret;
1534 	u8 out_data[40];
1535 	u8 dummy[2] = { };
1536 
1537 	/*
1538 	 * Without writing something to DATA_IN, this command would
1539 	 * return an error
1540 	 */
1541 	ret = tps6598x_exec_cmd_tmo(tps, "PBMc", sizeof(dummy), dummy,
1542 				    sizeof(out_data), out_data, 2000, 20);
1543 	if (ret)
1544 		return ret;
1545 
1546 	if (out_data[TPS_PBMC_RC]) {
1547 		dev_err(tps->dev,
1548 			"%s: pbmc failed: %u\n", __func__,
1549 			out_data[TPS_PBMC_RC]);
1550 		return -EIO;
1551 	}
1552 
1553 	if (out_data[TPS_PBMC_DPCS]) {
1554 		dev_err(tps->dev,
1555 			"%s: failed device patch complete status: %u\n",
1556 			__func__, out_data[TPS_PBMC_DPCS]);
1557 		return -EIO;
1558 	}
1559 
1560 	return 0;
1561 }
1562 
1563 static int tps25750_apply_patch(struct tps6598x *tps)
1564 {
1565 	int ret;
1566 	unsigned long timeout;
1567 	u64 status = 0;
1568 
1569 	ret = tps6598x_block_read(tps, TPS_REG_BOOT_STATUS, &status, 5);
1570 	if (ret)
1571 		return ret;
1572 	/*
1573 	 * Nothing to be done if the configuration
1574 	 * is being loaded from EERPOM
1575 	 */
1576 	if (status & TPS_BOOT_STATUS_I2C_EEPROM_PRESENT)
1577 		goto wait_for_app;
1578 
1579 	ret = tps25750_start_patch_burst_mode(tps);
1580 	if (ret) {
1581 		tps25750_abort_patch_process(tps);
1582 		return ret;
1583 	}
1584 
1585 	ret = tps25750_complete_patch_process(tps);
1586 	if (ret)
1587 		return ret;
1588 
1589 wait_for_app:
1590 	timeout = jiffies + msecs_to_jiffies(1000);
1591 
1592 	do {
1593 		ret = tps6598x_check_mode(tps);
1594 		if (ret < 0)
1595 			return ret;
1596 
1597 		if (time_is_before_jiffies(timeout))
1598 			return -ETIMEDOUT;
1599 
1600 	} while (ret != TPS_MODE_APP);
1601 
1602 	/*
1603 	 * The dead battery flag may be triggered when the controller
1604 	 * port is connected to a device that can source power and
1605 	 * attempts to power up both the controller and the board it is on.
1606 	 * To restore controller functionality, it is necessary to clear
1607 	 * this flag
1608 	 */
1609 	if (status & TPS_BOOT_STATUS_DEAD_BATTERY_FLAG) {
1610 		ret = tps6598x_exec_cmd(tps, "DBfg", 0, NULL, 0, NULL);
1611 		if (ret) {
1612 			dev_err(tps->dev, "failed to clear dead battery %d\n", ret);
1613 			return ret;
1614 		}
1615 	}
1616 
1617 	dev_info(tps->dev, "controller switched to \"APP\" mode\n");
1618 
1619 	return 0;
1620 };
1621 
1622 static int tps6598x_apply_patch(struct tps6598x *tps)
1623 {
1624 	u8 in = TPS_PTCS_CONTENT_DEV | TPS_PTCS_CONTENT_APP;
1625 	u8 out[TPS_MAX_LEN] = {0};
1626 	size_t in_len = sizeof(in);
1627 	size_t copied_bytes = 0;
1628 	size_t bytes_left;
1629 	const struct firmware *fw;
1630 	const char *firmware_name;
1631 	int ret;
1632 
1633 	ret = tps_request_firmware(tps, &fw, &firmware_name);
1634 	if (ret)
1635 		return ret;
1636 
1637 	ret = tps6598x_exec_cmd(tps, "PTCs", in_len, &in,
1638 				TPS_PTCS_OUT_BYTES, out);
1639 	if (ret || out[TPS_PTCS_STATUS] == TPS_PTCS_STATUS_FAIL) {
1640 		if (!ret)
1641 			ret = -EBUSY;
1642 		dev_err(tps->dev, "Update start failed (%d)\n", ret);
1643 		goto release_fw;
1644 	}
1645 
1646 	bytes_left = fw->size;
1647 	while (bytes_left) {
1648 		in_len = min(bytes_left, TPS_MAX_LEN);
1649 		ret = tps6598x_exec_cmd(tps, "PTCd", in_len,
1650 					fw->data + copied_bytes,
1651 					TPS_PTCD_OUT_BYTES, out);
1652 		if (ret || out[TPS_PTCD_TRANSFER_STATUS] ||
1653 		    out[TPS_PTCD_LOADING_STATE] == TPS_PTCD_LOAD_ERR) {
1654 			if (!ret)
1655 				ret = -EBUSY;
1656 			dev_err(tps->dev, "Patch download failed (%d)\n", ret);
1657 			goto release_fw;
1658 		}
1659 		copied_bytes += in_len;
1660 		bytes_left -= in_len;
1661 	}
1662 
1663 	ret = tps6598x_exec_cmd(tps, "PTCc", 0, NULL, TPS_PTCC_OUT_BYTES, out);
1664 	if (ret || out[TPS_PTCC_DEV] || out[TPS_PTCC_APP]) {
1665 		if (!ret)
1666 			ret = -EBUSY;
1667 		dev_err(tps->dev, "Update completion failed (%d)\n", ret);
1668 		goto release_fw;
1669 	}
1670 	msleep(TPS_SETUP_MS);
1671 	dev_info(tps->dev, "Firmware update succeeded\n");
1672 
1673 release_fw:
1674 	if (ret) {
1675 		dev_err(tps->dev, "Failed to write patch %s of %zu bytes\n",
1676 			firmware_name, fw->size);
1677 	}
1678 	release_firmware(fw);
1679 
1680 	return ret;
1681 }
1682 
1683 static int cd321x_init(struct tps6598x *tps)
1684 {
1685 	return 0;
1686 }
1687 
1688 static int tps25750_init(struct tps6598x *tps)
1689 {
1690 	int ret;
1691 
1692 	ret = tps->data->apply_patch(tps);
1693 	if (ret)
1694 		return ret;
1695 
1696 	ret = tps6598x_write8(tps, TPS_REG_SLEEP_CONF,
1697 			      TPS_SLEEP_CONF_SLEEP_MODE_ALLOWED);
1698 	if (ret)
1699 		dev_warn(tps->dev,
1700 			 "%s: failed to enable sleep mode: %d\n",
1701 			 __func__, ret);
1702 
1703 	return 0;
1704 }
1705 
1706 static int tps6598x_init(struct tps6598x *tps)
1707 {
1708 	return tps->data->apply_patch(tps);
1709 }
1710 
1711 static int cd321x_reset(struct tps6598x *tps)
1712 {
1713 	return 0;
1714 }
1715 
1716 static int tps25750_reset(struct tps6598x *tps)
1717 {
1718 	return tps6598x_exec_cmd_tmo(tps, "GAID", 0, NULL, 0, NULL, 2000, 0);
1719 }
1720 
1721 static int tps6598x_reset(struct tps6598x *tps)
1722 {
1723 	return 0;
1724 }
1725 
1726 static int
1727 tps25750_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
1728 {
1729 	struct typec_capability typec_cap = { };
1730 	const char *data_role;
1731 	u8 pd_status;
1732 	int ret;
1733 
1734 	ret = tps6598x_read8(tps, TPS_REG_PD_STATUS, &pd_status);
1735 	if (ret)
1736 		return ret;
1737 
1738 	ret = fwnode_property_read_string(fwnode, "data-role", &data_role);
1739 	if (ret) {
1740 		dev_err(tps->dev, "data-role not found: %d\n", ret);
1741 		return ret;
1742 	}
1743 
1744 	ret = typec_find_port_data_role(data_role);
1745 	if (ret < 0) {
1746 		dev_err(tps->dev, "unknown data-role: %s\n", data_role);
1747 		return ret;
1748 	}
1749 
1750 	typec_cap.data = ret;
1751 	typec_cap.revision = USB_TYPEC_REV_1_3;
1752 	typec_cap.pd_revision = 0x300;
1753 	typec_cap.orientation_aware = true;
1754 	typec_cap.driver_data = tps;
1755 	typec_cap.ops = &tps6598x_ops;
1756 	typec_cap.fwnode = fwnode;
1757 	typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
1758 
1759 	switch (TPS_PD_STATUS_PORT_TYPE(pd_status)) {
1760 	case TPS_PD_STATUS_PORT_TYPE_SINK_SOURCE:
1761 	case TPS_PD_STATUS_PORT_TYPE_SOURCE_SINK:
1762 		typec_cap.type = TYPEC_PORT_DRP;
1763 		break;
1764 	case TPS_PD_STATUS_PORT_TYPE_SINK:
1765 		typec_cap.type = TYPEC_PORT_SNK;
1766 		break;
1767 	case TPS_PD_STATUS_PORT_TYPE_SOURCE:
1768 		typec_cap.type = TYPEC_PORT_SRC;
1769 		break;
1770 	default:
1771 		return -ENODEV;
1772 	}
1773 
1774 	tps->port = typec_register_port(tps->dev, &typec_cap);
1775 	if (IS_ERR(tps->port))
1776 		return PTR_ERR(tps->port);
1777 
1778 	return 0;
1779 }
1780 
1781 static void cd321x_remove(struct tps6598x *tps)
1782 {
1783 	struct cd321x *cd321x = container_of(tps, struct cd321x, tps);
1784 
1785 	cancel_delayed_work_sync(&cd321x->update_work);
1786 }
1787 
1788 static int tps6598x_probe(struct i2c_client *client)
1789 {
1790 	const struct tipd_data *data;
1791 	struct tps6598x *tps;
1792 	struct fwnode_handle *fwnode;
1793 	bool patch_loaded = false;
1794 	u32 status;
1795 	u32 vid = 0;
1796 	int ret;
1797 
1798 	data = i2c_get_match_data(client);
1799 	if (!data)
1800 		return -EINVAL;
1801 
1802 	tps = devm_kzalloc(&client->dev, data->tps_struct_size, GFP_KERNEL);
1803 	if (!tps)
1804 		return -ENOMEM;
1805 
1806 	mutex_init(&tps->lock);
1807 	tps->dev = &client->dev;
1808 	tps->data = data;
1809 
1810 	tps->reset = devm_gpiod_get_optional(tps->dev, "reset", GPIOD_OUT_LOW);
1811 	if (IS_ERR(tps->reset))
1812 		return dev_err_probe(tps->dev, PTR_ERR(tps->reset),
1813 				     "failed to get reset GPIO\n");
1814 	if (tps->reset)
1815 		msleep(TPS_SETUP_MS);
1816 
1817 	tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
1818 	if (IS_ERR(tps->regmap))
1819 		return PTR_ERR(tps->regmap);
1820 
1821 	if (!device_is_compatible(tps->dev, "ti,tps25750")) {
1822 		ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
1823 		if (ret < 0 || !vid) {
1824 			dev_err(tps->dev, "failed to read vendor ID: %d, vid: %#x\n",
1825 				ret, vid);
1826 			return -ENODEV;
1827 		}
1828 	}
1829 
1830 	/*
1831 	 * Checking can the adapter handle SMBus protocol. If it can not, the
1832 	 * driver needs to take care of block reads separately.
1833 	 */
1834 	if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
1835 		tps->i2c_protocol = true;
1836 
1837 	if (tps->data->switch_power_state) {
1838 		ret = tps->data->switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0);
1839 		if (ret)
1840 			return ret;
1841 	}
1842 
1843 	/* Make sure the controller has application firmware running */
1844 	ret = tps6598x_check_mode(tps);
1845 	if (ret < 0)
1846 		return ret;
1847 
1848 	if (ret == TPS_MODE_PTCH) {
1849 		patch_loaded = true;
1850 		ret = tps->data->init(tps);
1851 		if (ret)
1852 			return ret;
1853 	}
1854 
1855 	ret = tps6598x_write64(tps, TPS_REG_INT_MASK1, tps->data->irq_mask1);
1856 	if (ret)
1857 		goto err_reset_controller;
1858 
1859 	if (!tps6598x_read_status(tps, &status)) {
1860 		ret = -ENODEV;
1861 		goto err_clear_mask;
1862 	}
1863 
1864 	tps->status = status;
1865 
1866 	/*
1867 	 * This fwnode has a "compatible" property, but is never populated as a
1868 	 * struct device. Instead we simply parse it to read the properties.
1869 	 * This breaks fw_devlink=on. To maintain backward compatibility
1870 	 * with existing DT files, we work around this by deleting any
1871 	 * fwnode_links to/from this fwnode.
1872 	 */
1873 	fwnode = device_get_named_child_node(&client->dev, "connector");
1874 	if (fwnode)
1875 		fw_devlink_purge_absent_suppliers(fwnode);
1876 
1877 	tps->role_sw = fwnode_usb_role_switch_get(fwnode);
1878 	if (IS_ERR(tps->role_sw)) {
1879 		ret = PTR_ERR(tps->role_sw);
1880 		goto err_fwnode_put;
1881 	}
1882 
1883 	ret = devm_tps6598_psy_register(tps);
1884 	if (ret)
1885 		goto err_role_put;
1886 
1887 	ret = tps->data->register_port(tps, fwnode);
1888 	if (ret)
1889 		goto err_role_put;
1890 
1891 	if (status & TPS_STATUS_PLUG_PRESENT) {
1892 		ret = -EINVAL;
1893 		if (!tps->data->read_power_status(tps))
1894 			goto err_unregister_port;
1895 		if (!tps->data->read_data_status(tps))
1896 			goto err_unregister_port;
1897 		ret = tps->data->connect(tps, status);
1898 		if (ret)
1899 			dev_err(&client->dev, "failed to register partner\n");
1900 	}
1901 
1902 	if (client->irq) {
1903 		ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
1904 						tps->data->irq_handler,
1905 						IRQF_SHARED | IRQF_ONESHOT,
1906 						dev_name(&client->dev), tps);
1907 	} else {
1908 		dev_dbg(tps->dev, "no IRQ specified, using polling mode\n");
1909 		INIT_DELAYED_WORK(&tps->wq_poll, tps6598x_poll_work);
1910 		queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,
1911 				   msecs_to_jiffies(POLL_INTERVAL));
1912 	}
1913 
1914 	if (ret)
1915 		goto err_disconnect;
1916 
1917 	i2c_set_clientdata(client, tps);
1918 	fwnode_handle_put(fwnode);
1919 
1920 	tps->wakeup = device_property_read_bool(tps->dev, "wakeup-source");
1921 	if (tps->wakeup && client->irq) {
1922 		devm_device_init_wakeup(&client->dev);
1923 		enable_irq_wake(client->irq);
1924 	}
1925 
1926 	return 0;
1927 
1928 err_disconnect:
1929 	tps6598x_disconnect(tps, 0);
1930 err_unregister_port:
1931 	tps->data->unregister_port(tps);
1932 err_role_put:
1933 	usb_role_switch_put(tps->role_sw);
1934 err_fwnode_put:
1935 	fwnode_handle_put(fwnode);
1936 err_clear_mask:
1937 	tps6598x_write64(tps, TPS_REG_INT_MASK1, 0);
1938 err_reset_controller:
1939 	/* Reset PD controller to remove any applied patch */
1940 	if (patch_loaded)
1941 		tps->data->reset(tps);
1942 
1943 	return ret;
1944 }
1945 
1946 static void tps6598x_remove(struct i2c_client *client)
1947 {
1948 	struct tps6598x *tps = i2c_get_clientdata(client);
1949 
1950 	if (!client->irq)
1951 		cancel_delayed_work_sync(&tps->wq_poll);
1952 	else
1953 		devm_free_irq(tps->dev, client->irq, tps);
1954 
1955 	if (tps->data->remove)
1956 		tps->data->remove(tps);
1957 
1958 	tps6598x_disconnect(tps, 0);
1959 	tps->data->unregister_port(tps);
1960 	usb_role_switch_put(tps->role_sw);
1961 
1962 	/* Reset PD controller to remove any applied patch */
1963 	tps->data->reset(tps);
1964 
1965 	if (tps->reset)
1966 		gpiod_set_value_cansleep(tps->reset, 1);
1967 }
1968 
1969 static int __maybe_unused tps6598x_suspend(struct device *dev)
1970 {
1971 	struct i2c_client *client = to_i2c_client(dev);
1972 	struct tps6598x *tps = i2c_get_clientdata(client);
1973 
1974 	if (tps->wakeup) {
1975 		disable_irq(client->irq);
1976 		enable_irq_wake(client->irq);
1977 	} else if (tps->reset) {
1978 		gpiod_set_value_cansleep(tps->reset, 1);
1979 	}
1980 
1981 	if (!client->irq)
1982 		cancel_delayed_work_sync(&tps->wq_poll);
1983 
1984 	return 0;
1985 }
1986 
1987 static int __maybe_unused tps6598x_resume(struct device *dev)
1988 {
1989 	struct i2c_client *client = to_i2c_client(dev);
1990 	struct tps6598x *tps = i2c_get_clientdata(client);
1991 	int ret;
1992 
1993 	ret = tps6598x_check_mode(tps);
1994 	if (ret < 0)
1995 		return ret;
1996 
1997 	if (ret == TPS_MODE_PTCH) {
1998 		ret = tps->data->init(tps);
1999 		if (ret)
2000 			return ret;
2001 	}
2002 
2003 	if (tps->wakeup) {
2004 		disable_irq_wake(client->irq);
2005 		enable_irq(client->irq);
2006 	} else if (tps->reset) {
2007 		gpiod_set_value_cansleep(tps->reset, 0);
2008 		msleep(TPS_SETUP_MS);
2009 	}
2010 
2011 	if (!client->irq)
2012 		queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,
2013 				   msecs_to_jiffies(POLL_INTERVAL));
2014 
2015 	return 0;
2016 }
2017 
2018 static const struct dev_pm_ops tps6598x_pm_ops = {
2019 	SET_SYSTEM_SLEEP_PM_OPS(tps6598x_suspend, tps6598x_resume)
2020 };
2021 
2022 static const struct tipd_data cd321x_data = {
2023 	.irq_handler = cd321x_interrupt,
2024 	.irq_mask1 = APPLE_CD_REG_INT_POWER_STATUS_UPDATE |
2025 		     APPLE_CD_REG_INT_DATA_STATUS_UPDATE |
2026 		     APPLE_CD_REG_INT_PLUG_EVENT,
2027 	.tps_struct_size = sizeof(struct cd321x),
2028 	.remove = cd321x_remove,
2029 	.register_port = cd321x_register_port,
2030 	.unregister_port = cd321x_unregister_port,
2031 	.trace_data_status = trace_cd321x_data_status,
2032 	.trace_power_status = trace_tps6598x_power_status,
2033 	.trace_status = trace_tps6598x_status,
2034 	.init = cd321x_init,
2035 	.read_data_status = cd321x_read_data_status,
2036 	.read_power_status = tps6598x_read_power_status,
2037 	.reset = cd321x_reset,
2038 	.switch_power_state = cd321x_switch_power_state,
2039 	.connect = cd321x_connect,
2040 };
2041 
2042 static const struct tipd_data tps6598x_data = {
2043 	.irq_handler = tps6598x_interrupt,
2044 	.irq_mask1 = TPS_REG_INT_POWER_STATUS_UPDATE |
2045 		     TPS_REG_INT_DATA_STATUS_UPDATE |
2046 		     TPS_REG_INT_PLUG_EVENT,
2047 	.tps_struct_size = sizeof(struct tps6598x),
2048 	.register_port = tps6598x_register_port,
2049 	.unregister_port = tps6598x_unregister_port,
2050 	.trace_data_status = trace_tps6598x_data_status,
2051 	.trace_power_status = trace_tps6598x_power_status,
2052 	.trace_status = trace_tps6598x_status,
2053 	.apply_patch = tps6598x_apply_patch,
2054 	.init = tps6598x_init,
2055 	.read_data_status = tps6598x_read_data_status,
2056 	.read_power_status = tps6598x_read_power_status,
2057 	.reset = tps6598x_reset,
2058 	.connect = tps6598x_connect,
2059 };
2060 
2061 static const struct tipd_data tps66993_data = {
2062 	.irq_handler = tps6598x_interrupt,
2063 	.irq_mask1 = TPS_REG_INT_DATA_STATUS_UPDATE |
2064 		     TPS_REG_INT_PLUG_EVENT,
2065 	.tps_struct_size = sizeof(struct tps6598x),
2066 	.register_port = tps6598x_register_port,
2067 	.unregister_port = tps6598x_unregister_port,
2068 	.trace_data_status = trace_tps6598x_data_status,
2069 	.trace_power_status = trace_tps6598x_power_status,
2070 	.trace_status = trace_tps6598x_status,
2071 	.apply_patch = tps6598x_apply_patch,
2072 	.init = tps6598x_init,
2073 	.read_data_status = tps6598x_read_data_status,
2074 	.read_power_status = tps66993_read_power_status,
2075 	.reset = tps6598x_reset,
2076 	.connect = tps6598x_connect,
2077 };
2078 
2079 static const struct tipd_data tps25750_data = {
2080 	.irq_handler = tps25750_interrupt,
2081 	.irq_mask1 = TPS_REG_INT_POWER_STATUS_UPDATE |
2082 		     TPS_REG_INT_DATA_STATUS_UPDATE |
2083 		     TPS_REG_INT_PLUG_EVENT,
2084 	.tps_struct_size = sizeof(struct tps6598x),
2085 	.register_port = tps25750_register_port,
2086 	.unregister_port = tps6598x_unregister_port,
2087 	.trace_data_status = trace_tps6598x_data_status,
2088 	.trace_power_status = trace_tps25750_power_status,
2089 	.trace_status = trace_tps25750_status,
2090 	.apply_patch = tps25750_apply_patch,
2091 	.init = tps25750_init,
2092 	.read_data_status = tps6598x_read_data_status,
2093 	.read_power_status = tps6598x_read_power_status,
2094 	.reset = tps25750_reset,
2095 	.connect = tps6598x_connect,
2096 };
2097 
2098 static const struct of_device_id tps6598x_of_match[] = {
2099 	{ .compatible = "ti,tps6598x", &tps6598x_data},
2100 	{ .compatible = "ti,tps66993", &tps66993_data},
2101 	{ .compatible = "apple,cd321x", &cd321x_data},
2102 	{ .compatible = "ti,tps25750", &tps25750_data},
2103 	{}
2104 };
2105 MODULE_DEVICE_TABLE(of, tps6598x_of_match);
2106 
2107 static const struct i2c_device_id tps6598x_id[] = {
2108 	{ .name = "tps6598x", .driver_data = (kernel_ulong_t)&tps6598x_data },
2109 	{ }
2110 };
2111 MODULE_DEVICE_TABLE(i2c, tps6598x_id);
2112 
2113 static struct i2c_driver tps6598x_i2c_driver = {
2114 	.driver = {
2115 		.name = "tps6598x",
2116 		.pm = &tps6598x_pm_ops,
2117 		.of_match_table = tps6598x_of_match,
2118 	},
2119 	.probe = tps6598x_probe,
2120 	.remove = tps6598x_remove,
2121 	.id_table = tps6598x_id,
2122 };
2123 module_i2c_driver(tps6598x_i2c_driver);
2124 
2125 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
2126 MODULE_LICENSE("GPL v2");
2127 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");
2128