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