xref: /openbmc/linux/drivers/usb/typec/tipd/core.c (revision 83946783)
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/module.h>
12 #include <linux/of.h>
13 #include <linux/power_supply.h>
14 #include <linux/regmap.h>
15 #include <linux/interrupt.h>
16 #include <linux/usb/typec.h>
17 #include <linux/usb/role.h>
18 
19 #include "tps6598x.h"
20 #include "trace.h"
21 
22 /* Register offsets */
23 #define TPS_REG_VID			0x00
24 #define TPS_REG_MODE			0x03
25 #define TPS_REG_CMD1			0x08
26 #define TPS_REG_DATA1			0x09
27 #define TPS_REG_INT_EVENT1		0x14
28 #define TPS_REG_INT_EVENT2		0x15
29 #define TPS_REG_INT_MASK1		0x16
30 #define TPS_REG_INT_MASK2		0x17
31 #define TPS_REG_INT_CLEAR1		0x18
32 #define TPS_REG_INT_CLEAR2		0x19
33 #define TPS_REG_SYSTEM_POWER_STATE	0x20
34 #define TPS_REG_STATUS			0x1a
35 #define TPS_REG_SYSTEM_CONF		0x28
36 #define TPS_REG_CTRL_CONF		0x29
37 #define TPS_REG_POWER_STATUS		0x3f
38 #define TPS_REG_RX_IDENTITY_SOP		0x48
39 #define TPS_REG_DATA_STATUS		0x5f
40 
41 /* TPS_REG_SYSTEM_CONF bits */
42 #define TPS_SYSCONF_PORTINFO(c)		((c) & 7)
43 
44 enum {
45 	TPS_PORTINFO_SINK,
46 	TPS_PORTINFO_SINK_ACCESSORY,
47 	TPS_PORTINFO_DRP_UFP,
48 	TPS_PORTINFO_DRP_UFP_DRD,
49 	TPS_PORTINFO_DRP_DFP,
50 	TPS_PORTINFO_DRP_DFP_DRD,
51 	TPS_PORTINFO_SOURCE,
52 };
53 
54 /* TPS_REG_RX_IDENTITY_SOP */
55 struct tps6598x_rx_identity_reg {
56 	u8 status;
57 	struct usb_pd_identity identity;
58 } __packed;
59 
60 /* Standard Task return codes */
61 #define TPS_TASK_TIMEOUT		1
62 #define TPS_TASK_REJECTED		3
63 
64 enum {
65 	TPS_MODE_APP,
66 	TPS_MODE_BOOT,
67 	TPS_MODE_BIST,
68 	TPS_MODE_DISC,
69 };
70 
71 static const char *const modes[] = {
72 	[TPS_MODE_APP]	= "APP ",
73 	[TPS_MODE_BOOT]	= "BOOT",
74 	[TPS_MODE_BIST]	= "BIST",
75 	[TPS_MODE_DISC]	= "DISC",
76 };
77 
78 /* Unrecognized commands will be replaced with "!CMD" */
79 #define INVALID_CMD(_cmd_)		(_cmd_ == 0x444d4321)
80 
81 struct tps6598x {
82 	struct device *dev;
83 	struct regmap *regmap;
84 	struct mutex lock; /* device lock */
85 	u8 i2c_protocol:1;
86 
87 	struct typec_port *port;
88 	struct typec_partner *partner;
89 	struct usb_pd_identity partner_identity;
90 	struct usb_role_switch *role_sw;
91 	struct typec_capability typec_cap;
92 
93 	struct power_supply *psy;
94 	struct power_supply_desc psy_desc;
95 	enum power_supply_usb_type usb_type;
96 };
97 
98 static enum power_supply_property tps6598x_psy_props[] = {
99 	POWER_SUPPLY_PROP_USB_TYPE,
100 	POWER_SUPPLY_PROP_ONLINE,
101 };
102 
103 static enum power_supply_usb_type tps6598x_psy_usb_types[] = {
104 	POWER_SUPPLY_USB_TYPE_C,
105 	POWER_SUPPLY_USB_TYPE_PD,
106 };
107 
108 static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-";
109 
110 /*
111  * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
112  * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
113  */
114 #define TPS_MAX_LEN	64
115 
116 static int
117 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
118 {
119 	u8 data[TPS_MAX_LEN + 1];
120 	int ret;
121 
122 	if (len + 1 > sizeof(data))
123 		return -EINVAL;
124 
125 	if (!tps->i2c_protocol)
126 		return regmap_raw_read(tps->regmap, reg, val, len);
127 
128 	ret = regmap_raw_read(tps->regmap, reg, data, len + 1);
129 	if (ret)
130 		return ret;
131 
132 	if (data[0] < len)
133 		return -EIO;
134 
135 	memcpy(val, &data[1], len);
136 	return 0;
137 }
138 
139 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
140 				const void *val, size_t len)
141 {
142 	u8 data[TPS_MAX_LEN + 1];
143 
144 	if (len + 1 > sizeof(data))
145 		return -EINVAL;
146 
147 	if (!tps->i2c_protocol)
148 		return regmap_raw_write(tps->regmap, reg, val, len);
149 
150 	data[0] = len;
151 	memcpy(&data[1], val, len);
152 
153 	return regmap_raw_write(tps->regmap, reg, data, len + 1);
154 }
155 
156 static inline int tps6598x_read8(struct tps6598x *tps, u8 reg, u8 *val)
157 {
158 	return tps6598x_block_read(tps, reg, val, sizeof(u8));
159 }
160 
161 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
162 {
163 	return tps6598x_block_read(tps, reg, val, sizeof(u16));
164 }
165 
166 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
167 {
168 	return tps6598x_block_read(tps, reg, val, sizeof(u32));
169 }
170 
171 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
172 {
173 	return tps6598x_block_read(tps, reg, val, sizeof(u64));
174 }
175 
176 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
177 {
178 	return tps6598x_block_write(tps, reg, &val, sizeof(u16));
179 }
180 
181 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
182 {
183 	return tps6598x_block_write(tps, reg, &val, sizeof(u32));
184 }
185 
186 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
187 {
188 	return tps6598x_block_write(tps, reg, &val, sizeof(u64));
189 }
190 
191 static inline int
192 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
193 {
194 	return tps6598x_block_write(tps, reg, val, 4);
195 }
196 
197 static int tps6598x_read_partner_identity(struct tps6598x *tps)
198 {
199 	struct tps6598x_rx_identity_reg id;
200 	int ret;
201 
202 	ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
203 				  &id, sizeof(id));
204 	if (ret)
205 		return ret;
206 
207 	tps->partner_identity = id.identity;
208 
209 	return 0;
210 }
211 
212 static void tps6598x_set_data_role(struct tps6598x *tps,
213 				   enum typec_data_role role, bool connected)
214 {
215 	enum usb_role role_val;
216 
217 	if (role == TYPEC_HOST)
218 		role_val = USB_ROLE_HOST;
219 	else
220 		role_val = USB_ROLE_DEVICE;
221 
222 	if (!connected)
223 		role_val = USB_ROLE_NONE;
224 
225 	usb_role_switch_set_role(tps->role_sw, role_val);
226 	typec_set_data_role(tps->port, role);
227 }
228 
229 static int tps6598x_connect(struct tps6598x *tps, u32 status)
230 {
231 	struct typec_partner_desc desc;
232 	enum typec_pwr_opmode mode;
233 	u16 pwr_status;
234 	int ret;
235 
236 	if (tps->partner)
237 		return 0;
238 
239 	ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
240 	if (ret < 0)
241 		return ret;
242 
243 	mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
244 
245 	desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
246 	desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
247 	desc.identity = NULL;
248 
249 	if (desc.usb_pd) {
250 		ret = tps6598x_read_partner_identity(tps);
251 		if (ret)
252 			return ret;
253 		desc.identity = &tps->partner_identity;
254 	}
255 
256 	typec_set_pwr_opmode(tps->port, mode);
257 	typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
258 	typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
259 	tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), true);
260 
261 	tps->partner = typec_register_partner(tps->port, &desc);
262 	if (IS_ERR(tps->partner))
263 		return PTR_ERR(tps->partner);
264 
265 	if (desc.identity)
266 		typec_partner_set_identity(tps->partner);
267 
268 	power_supply_changed(tps->psy);
269 
270 	return 0;
271 }
272 
273 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
274 {
275 	if (!IS_ERR(tps->partner))
276 		typec_unregister_partner(tps->partner);
277 	tps->partner = NULL;
278 	typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
279 	typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
280 	typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
281 	tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), false);
282 
283 	power_supply_changed(tps->psy);
284 }
285 
286 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
287 			     size_t in_len, u8 *in_data,
288 			     size_t out_len, u8 *out_data)
289 {
290 	unsigned long timeout;
291 	u32 val;
292 	int ret;
293 
294 	ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
295 	if (ret)
296 		return ret;
297 	if (val && !INVALID_CMD(val))
298 		return -EBUSY;
299 
300 	if (in_len) {
301 		ret = tps6598x_block_write(tps, TPS_REG_DATA1,
302 					   in_data, in_len);
303 		if (ret)
304 			return ret;
305 	}
306 
307 	ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
308 	if (ret < 0)
309 		return ret;
310 
311 	/* XXX: Using 1s for now, but it may not be enough for every command. */
312 	timeout = jiffies + msecs_to_jiffies(1000);
313 
314 	do {
315 		ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
316 		if (ret)
317 			return ret;
318 		if (INVALID_CMD(val))
319 			return -EINVAL;
320 
321 		if (time_is_before_jiffies(timeout))
322 			return -ETIMEDOUT;
323 	} while (val);
324 
325 	if (out_len) {
326 		ret = tps6598x_block_read(tps, TPS_REG_DATA1,
327 					  out_data, out_len);
328 		if (ret)
329 			return ret;
330 		val = out_data[0];
331 	} else {
332 		ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
333 		if (ret)
334 			return ret;
335 	}
336 
337 	switch (val) {
338 	case TPS_TASK_TIMEOUT:
339 		return -ETIMEDOUT;
340 	case TPS_TASK_REJECTED:
341 		return -EPERM;
342 	default:
343 		break;
344 	}
345 
346 	return 0;
347 }
348 
349 static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
350 {
351 	const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
352 	struct tps6598x *tps = typec_get_drvdata(port);
353 	u32 status;
354 	int ret;
355 
356 	mutex_lock(&tps->lock);
357 
358 	ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
359 	if (ret)
360 		goto out_unlock;
361 
362 	ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
363 	if (ret)
364 		goto out_unlock;
365 
366 	if (role != TPS_STATUS_TO_TYPEC_DATAROLE(status)) {
367 		ret = -EPROTO;
368 		goto out_unlock;
369 	}
370 
371 	tps6598x_set_data_role(tps, role, true);
372 
373 out_unlock:
374 	mutex_unlock(&tps->lock);
375 
376 	return ret;
377 }
378 
379 static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
380 {
381 	const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
382 	struct tps6598x *tps = typec_get_drvdata(port);
383 	u32 status;
384 	int ret;
385 
386 	mutex_lock(&tps->lock);
387 
388 	ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
389 	if (ret)
390 		goto out_unlock;
391 
392 	ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
393 	if (ret)
394 		goto out_unlock;
395 
396 	if (role != TPS_STATUS_TO_TYPEC_PORTROLE(status)) {
397 		ret = -EPROTO;
398 		goto out_unlock;
399 	}
400 
401 	typec_set_pwr_role(tps->port, role);
402 
403 out_unlock:
404 	mutex_unlock(&tps->lock);
405 
406 	return ret;
407 }
408 
409 static const struct typec_operations tps6598x_ops = {
410 	.dr_set = tps6598x_dr_set,
411 	.pr_set = tps6598x_pr_set,
412 };
413 
414 static bool tps6598x_read_status(struct tps6598x *tps, u32 *status)
415 {
416 	int ret;
417 
418 	ret = tps6598x_read32(tps, TPS_REG_STATUS, status);
419 	if (ret) {
420 		dev_err(tps->dev, "%s: failed to read status\n", __func__);
421 		return false;
422 	}
423 	trace_tps6598x_status(*status);
424 
425 	return true;
426 }
427 
428 static bool tps6598x_read_data_status(struct tps6598x *tps)
429 {
430 	u32 data_status;
431 	int ret;
432 
433 	ret = tps6598x_read32(tps, TPS_REG_DATA_STATUS, &data_status);
434 	if (ret < 0) {
435 		dev_err(tps->dev, "failed to read data status: %d\n", ret);
436 		return false;
437 	}
438 	trace_tps6598x_data_status(data_status);
439 
440 	return true;
441 }
442 
443 static bool tps6598x_read_power_status(struct tps6598x *tps)
444 {
445 	u16 pwr_status;
446 	int ret;
447 
448 	ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
449 	if (ret < 0) {
450 		dev_err(tps->dev, "failed to read power status: %d\n", ret);
451 		return false;
452 	}
453 	trace_tps6598x_power_status(pwr_status);
454 
455 	return true;
456 }
457 
458 static void tps6598x_handle_plug_event(struct tps6598x *tps, u32 status)
459 {
460 	int ret;
461 
462 	if (status & TPS_STATUS_PLUG_PRESENT) {
463 		ret = tps6598x_connect(tps, status);
464 		if (ret)
465 			dev_err(tps->dev, "failed to register partner\n");
466 	} else {
467 		tps6598x_disconnect(tps, status);
468 	}
469 }
470 
471 static irqreturn_t cd321x_interrupt(int irq, void *data)
472 {
473 	struct tps6598x *tps = data;
474 	u64 event;
475 	u32 status;
476 	int ret;
477 
478 	mutex_lock(&tps->lock);
479 
480 	ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event);
481 	if (ret) {
482 		dev_err(tps->dev, "%s: failed to read events\n", __func__);
483 		goto err_unlock;
484 	}
485 	trace_cd321x_irq(event);
486 
487 	if (!event)
488 		goto err_unlock;
489 
490 	if (!tps6598x_read_status(tps, &status))
491 		goto err_clear_ints;
492 
493 	if (event & APPLE_CD_REG_INT_POWER_STATUS_UPDATE)
494 		if (!tps6598x_read_power_status(tps))
495 			goto err_clear_ints;
496 
497 	if (event & APPLE_CD_REG_INT_DATA_STATUS_UPDATE)
498 		if (!tps6598x_read_data_status(tps))
499 			goto err_clear_ints;
500 
501 	/* Handle plug insert or removal */
502 	if (event & APPLE_CD_REG_INT_PLUG_EVENT)
503 		tps6598x_handle_plug_event(tps, status);
504 
505 err_clear_ints:
506 	tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event);
507 
508 err_unlock:
509 	mutex_unlock(&tps->lock);
510 
511 	if (event)
512 		return IRQ_HANDLED;
513 	return IRQ_NONE;
514 }
515 
516 static irqreturn_t tps6598x_interrupt(int irq, void *data)
517 {
518 	struct tps6598x *tps = data;
519 	u64 event1;
520 	u64 event2;
521 	u32 status;
522 	int ret;
523 
524 	mutex_lock(&tps->lock);
525 
526 	ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
527 	ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
528 	if (ret) {
529 		dev_err(tps->dev, "%s: failed to read events\n", __func__);
530 		goto err_unlock;
531 	}
532 	trace_tps6598x_irq(event1, event2);
533 
534 	if (!(event1 | event2))
535 		goto err_unlock;
536 
537 	if (!tps6598x_read_status(tps, &status))
538 		goto err_clear_ints;
539 
540 	if ((event1 | event2) & TPS_REG_INT_POWER_STATUS_UPDATE)
541 		if (!tps6598x_read_power_status(tps))
542 			goto err_clear_ints;
543 
544 	if ((event1 | event2) & TPS_REG_INT_DATA_STATUS_UPDATE)
545 		if (!tps6598x_read_data_status(tps))
546 			goto err_clear_ints;
547 
548 	/* Handle plug insert or removal */
549 	if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT)
550 		tps6598x_handle_plug_event(tps, status);
551 
552 err_clear_ints:
553 	tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
554 	tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
555 
556 err_unlock:
557 	mutex_unlock(&tps->lock);
558 
559 	if (event1 | event2)
560 		return IRQ_HANDLED;
561 	return IRQ_NONE;
562 }
563 
564 static int tps6598x_check_mode(struct tps6598x *tps)
565 {
566 	char mode[5] = { };
567 	int ret;
568 
569 	ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
570 	if (ret)
571 		return ret;
572 
573 	switch (match_string(modes, ARRAY_SIZE(modes), mode)) {
574 	case TPS_MODE_APP:
575 		return 0;
576 	case TPS_MODE_BOOT:
577 		dev_warn(tps->dev, "dead-battery condition\n");
578 		return 0;
579 	case TPS_MODE_BIST:
580 	case TPS_MODE_DISC:
581 	default:
582 		dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
583 			mode);
584 		break;
585 	}
586 
587 	return -ENODEV;
588 }
589 
590 static const struct regmap_config tps6598x_regmap_config = {
591 	.reg_bits = 8,
592 	.val_bits = 8,
593 	.max_register = 0x7F,
594 };
595 
596 static int tps6598x_psy_get_online(struct tps6598x *tps,
597 				   union power_supply_propval *val)
598 {
599 	int ret;
600 	u16 pwr_status;
601 
602 	ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
603 	if (ret < 0)
604 		return ret;
605 
606 	if (TPS_POWER_STATUS_CONNECTION(pwr_status) &&
607 	    TPS_POWER_STATUS_SOURCESINK(pwr_status)) {
608 		val->intval = 1;
609 	} else {
610 		val->intval = 0;
611 	}
612 	return 0;
613 }
614 
615 static int tps6598x_psy_get_prop(struct power_supply *psy,
616 				 enum power_supply_property psp,
617 				 union power_supply_propval *val)
618 {
619 	struct tps6598x *tps = power_supply_get_drvdata(psy);
620 	u16 pwr_status;
621 	int ret = 0;
622 
623 	switch (psp) {
624 	case POWER_SUPPLY_PROP_USB_TYPE:
625 		ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
626 		if (ret < 0)
627 			return ret;
628 		if (TPS_POWER_STATUS_PWROPMODE(pwr_status) == TYPEC_PWR_MODE_PD)
629 			val->intval = POWER_SUPPLY_USB_TYPE_PD;
630 		else
631 			val->intval = POWER_SUPPLY_USB_TYPE_C;
632 		break;
633 	case POWER_SUPPLY_PROP_ONLINE:
634 		ret = tps6598x_psy_get_online(tps, val);
635 		break;
636 	default:
637 		ret = -EINVAL;
638 		break;
639 	}
640 
641 	return ret;
642 }
643 
644 static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state)
645 {
646 	u8 state;
647 	int ret;
648 
649 	ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
650 	if (ret)
651 		return ret;
652 
653 	if (state == target_state)
654 		return 0;
655 
656 	ret = tps6598x_exec_cmd(tps, "SPSS", sizeof(u8), &target_state, 0, NULL);
657 	if (ret)
658 		return ret;
659 
660 	ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
661 	if (ret)
662 		return ret;
663 
664 	if (state != target_state)
665 		return -EINVAL;
666 
667 	return 0;
668 }
669 
670 static int devm_tps6598_psy_register(struct tps6598x *tps)
671 {
672 	struct power_supply_config psy_cfg = {};
673 	const char *port_dev_name = dev_name(tps->dev);
674 	char *psy_name;
675 
676 	psy_cfg.drv_data = tps;
677 	psy_cfg.fwnode = dev_fwnode(tps->dev);
678 
679 	psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
680 				  port_dev_name);
681 	if (!psy_name)
682 		return -ENOMEM;
683 
684 	tps->psy_desc.name = psy_name;
685 	tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
686 	tps->psy_desc.usb_types = tps6598x_psy_usb_types;
687 	tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types);
688 	tps->psy_desc.properties = tps6598x_psy_props;
689 	tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
690 	tps->psy_desc.get_property = tps6598x_psy_get_prop;
691 
692 	tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
693 
694 	tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
695 					       &psy_cfg);
696 	return PTR_ERR_OR_ZERO(tps->psy);
697 }
698 
699 static int tps6598x_probe(struct i2c_client *client)
700 {
701 	irq_handler_t irq_handler = tps6598x_interrupt;
702 	struct device_node *np = client->dev.of_node;
703 	struct typec_capability typec_cap = { };
704 	struct tps6598x *tps;
705 	struct fwnode_handle *fwnode;
706 	u32 status;
707 	u32 conf;
708 	u32 vid;
709 	int ret;
710 
711 	tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
712 	if (!tps)
713 		return -ENOMEM;
714 
715 	mutex_init(&tps->lock);
716 	tps->dev = &client->dev;
717 
718 	tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
719 	if (IS_ERR(tps->regmap))
720 		return PTR_ERR(tps->regmap);
721 
722 	ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
723 	if (ret < 0 || !vid)
724 		return -ENODEV;
725 
726 	/*
727 	 * Checking can the adapter handle SMBus protocol. If it can not, the
728 	 * driver needs to take care of block reads separately.
729 	 */
730 	if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
731 		tps->i2c_protocol = true;
732 
733 	/* Make sure the controller has application firmware running */
734 	ret = tps6598x_check_mode(tps);
735 	if (ret)
736 		return ret;
737 
738 	if (np && of_device_is_compatible(np, "apple,cd321x")) {
739 		/* Switch CD321X chips to the correct system power state */
740 		ret = cd321x_switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0);
741 		if (ret)
742 			return ret;
743 
744 		/* CD321X chips have all interrupts masked initially */
745 		ret = tps6598x_write64(tps, TPS_REG_INT_MASK1,
746 					APPLE_CD_REG_INT_POWER_STATUS_UPDATE |
747 					APPLE_CD_REG_INT_DATA_STATUS_UPDATE |
748 					APPLE_CD_REG_INT_PLUG_EVENT);
749 		if (ret)
750 			return ret;
751 
752 		irq_handler = cd321x_interrupt;
753 	} else {
754 		/* Enable power status, data status and plug event interrupts */
755 		ret = tps6598x_write64(tps, TPS_REG_INT_MASK1,
756 				       TPS_REG_INT_POWER_STATUS_UPDATE |
757 				       TPS_REG_INT_DATA_STATUS_UPDATE |
758 				       TPS_REG_INT_PLUG_EVENT);
759 		if (ret)
760 			return ret;
761 	}
762 
763 	ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
764 	if (ret < 0)
765 		return ret;
766 	trace_tps6598x_status(status);
767 
768 	ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
769 	if (ret < 0)
770 		return ret;
771 
772 	/*
773 	 * This fwnode has a "compatible" property, but is never populated as a
774 	 * struct device. Instead we simply parse it to read the properties.
775 	 * This breaks fw_devlink=on. To maintain backward compatibility
776 	 * with existing DT files, we work around this by deleting any
777 	 * fwnode_links to/from this fwnode.
778 	 */
779 	fwnode = device_get_named_child_node(&client->dev, "connector");
780 	if (fwnode)
781 		fw_devlink_purge_absent_suppliers(fwnode);
782 
783 	tps->role_sw = fwnode_usb_role_switch_get(fwnode);
784 	if (IS_ERR(tps->role_sw)) {
785 		ret = PTR_ERR(tps->role_sw);
786 		goto err_fwnode_put;
787 	}
788 
789 	typec_cap.revision = USB_TYPEC_REV_1_2;
790 	typec_cap.pd_revision = 0x200;
791 	typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
792 	typec_cap.driver_data = tps;
793 	typec_cap.ops = &tps6598x_ops;
794 	typec_cap.fwnode = fwnode;
795 
796 	switch (TPS_SYSCONF_PORTINFO(conf)) {
797 	case TPS_PORTINFO_SINK_ACCESSORY:
798 	case TPS_PORTINFO_SINK:
799 		typec_cap.type = TYPEC_PORT_SNK;
800 		typec_cap.data = TYPEC_PORT_UFP;
801 		break;
802 	case TPS_PORTINFO_DRP_UFP_DRD:
803 	case TPS_PORTINFO_DRP_DFP_DRD:
804 		typec_cap.type = TYPEC_PORT_DRP;
805 		typec_cap.data = TYPEC_PORT_DRD;
806 		break;
807 	case TPS_PORTINFO_DRP_UFP:
808 		typec_cap.type = TYPEC_PORT_DRP;
809 		typec_cap.data = TYPEC_PORT_UFP;
810 		break;
811 	case TPS_PORTINFO_DRP_DFP:
812 		typec_cap.type = TYPEC_PORT_DRP;
813 		typec_cap.data = TYPEC_PORT_DFP;
814 		break;
815 	case TPS_PORTINFO_SOURCE:
816 		typec_cap.type = TYPEC_PORT_SRC;
817 		typec_cap.data = TYPEC_PORT_DFP;
818 		break;
819 	default:
820 		ret = -ENODEV;
821 		goto err_role_put;
822 	}
823 
824 	ret = devm_tps6598_psy_register(tps);
825 	if (ret)
826 		return ret;
827 
828 	tps->port = typec_register_port(&client->dev, &typec_cap);
829 	if (IS_ERR(tps->port)) {
830 		ret = PTR_ERR(tps->port);
831 		goto err_role_put;
832 	}
833 	fwnode_handle_put(fwnode);
834 
835 	if (status & TPS_STATUS_PLUG_PRESENT) {
836 		ret = tps6598x_connect(tps, status);
837 		if (ret)
838 			dev_err(&client->dev, "failed to register partner\n");
839 	}
840 
841 	ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
842 					irq_handler,
843 					IRQF_SHARED | IRQF_ONESHOT,
844 					dev_name(&client->dev), tps);
845 	if (ret) {
846 		tps6598x_disconnect(tps, 0);
847 		typec_unregister_port(tps->port);
848 		goto err_role_put;
849 	}
850 
851 	i2c_set_clientdata(client, tps);
852 
853 	return 0;
854 
855 err_role_put:
856 	usb_role_switch_put(tps->role_sw);
857 err_fwnode_put:
858 	fwnode_handle_put(fwnode);
859 
860 	return ret;
861 }
862 
863 static int tps6598x_remove(struct i2c_client *client)
864 {
865 	struct tps6598x *tps = i2c_get_clientdata(client);
866 
867 	tps6598x_disconnect(tps, 0);
868 	typec_unregister_port(tps->port);
869 	usb_role_switch_put(tps->role_sw);
870 
871 	return 0;
872 }
873 
874 static const struct of_device_id tps6598x_of_match[] = {
875 	{ .compatible = "ti,tps6598x", },
876 	{ .compatible = "apple,cd321x", },
877 	{}
878 };
879 MODULE_DEVICE_TABLE(of, tps6598x_of_match);
880 
881 static const struct i2c_device_id tps6598x_id[] = {
882 	{ "tps6598x" },
883 	{ }
884 };
885 MODULE_DEVICE_TABLE(i2c, tps6598x_id);
886 
887 static struct i2c_driver tps6598x_i2c_driver = {
888 	.driver = {
889 		.name = "tps6598x",
890 		.of_match_table = tps6598x_of_match,
891 	},
892 	.probe_new = tps6598x_probe,
893 	.remove = tps6598x_remove,
894 	.id_table = tps6598x_id,
895 };
896 module_i2c_driver(tps6598x_i2c_driver);
897 
898 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
899 MODULE_LICENSE("GPL v2");
900 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");
901