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