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