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 partner\n", __func__);
825 return;
826 }
827
828 if (desc.identity) {
829 typec_partner_set_identity(tps->partner);
830 cd321x->cur_partner_identity = st.partner_identity;
831 }
832 }
833
834 /* Update the TypeC MUX/PHY state */
835 cd321x_typec_update_mode(tps, &st);
836
837 /* Launch the USB role switch */
838 usb_role_switch_set_role(tps->role_sw, new_role);
839
840 power_supply_changed(tps->psy);
841 }
842
cd321x_queue_status(struct cd321x * cd321x)843 static void cd321x_queue_status(struct cd321x *cd321x)
844 {
845 cd321x->update_status.status_changed |= cd321x->update_status.status ^ cd321x->tps.status;
846
847 cd321x->update_status.status = cd321x->tps.status;
848 cd321x->update_status.pwr_status = cd321x->tps.pwr_status;
849 cd321x->update_status.data_status = cd321x->tps.data_status;
850
851 cd321x->update_status.partner_identity = cd321x->tps.partner_identity;
852 cd321x->update_status.dp_sid_status = cd321x->dp_sid_status;
853 cd321x->update_status.intel_vid_status = cd321x->intel_vid_status;
854 cd321x->update_status.usb4_status = cd321x->usb4_status;
855 }
856
cd321x_connect(struct tps6598x * tps,u32 status)857 static int cd321x_connect(struct tps6598x *tps, u32 status)
858 {
859 struct cd321x *cd321x = container_of(tps, struct cd321x, tps);
860
861 tps->status = status;
862 cd321x_queue_status(cd321x);
863
864 /*
865 * Cancel pending work if not already running, then requeue after CD321X_DEBOUNCE_DELAY_MS
866 * regardless since the work function will check for any plug or altmodes changes since
867 * its last run anyway.
868 */
869 cancel_delayed_work(&cd321x->update_work);
870 schedule_delayed_work(&cd321x->update_work, msecs_to_jiffies(CD321X_DEBOUNCE_DELAY_MS));
871
872 return 0;
873 }
874
cd321x_interrupt(int irq,void * data)875 static irqreturn_t cd321x_interrupt(int irq, void *data)
876 {
877 struct tps6598x *tps = data;
878 u64 event = 0;
879 u32 status;
880 int ret;
881
882 mutex_lock(&tps->lock);
883
884 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event);
885 if (ret) {
886 dev_err(tps->dev, "%s: failed to read events\n", __func__);
887 goto err_unlock;
888 }
889 trace_cd321x_irq(event);
890
891 if (!event)
892 goto err_unlock;
893
894 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event);
895
896 if (!tps6598x_read_status(tps, &status))
897 goto err_unlock;
898
899 if (event & APPLE_CD_REG_INT_POWER_STATUS_UPDATE) {
900 if (!tps6598x_read_power_status(tps))
901 goto err_unlock;
902 if (TPS_POWER_STATUS_PWROPMODE(tps->pwr_status) == TYPEC_PWR_MODE_PD) {
903 if (tps6598x_read_partner_identity(tps)) {
904 dev_err(tps->dev, "failed to read partner identity\n");
905 tps->partner_identity = (struct usb_pd_identity) {0};
906 }
907 }
908 }
909
910 if (event & APPLE_CD_REG_INT_DATA_STATUS_UPDATE)
911 if (!tps->data->read_data_status(tps))
912 goto err_unlock;
913
914 /* Can be called uncondtionally since it will check for any changes itself */
915 cd321x_connect(tps, status);
916
917 err_unlock:
918 mutex_unlock(&tps->lock);
919
920 if (event)
921 return IRQ_HANDLED;
922 return IRQ_NONE;
923 }
924
tps6598x_has_role_changed(struct tps6598x * tps,u32 status)925 static bool tps6598x_has_role_changed(struct tps6598x *tps, u32 status)
926 {
927 status ^= tps->status;
928
929 return status & (TPS_STATUS_PORTROLE | TPS_STATUS_DATAROLE);
930 }
931
tps25750_interrupt(int irq,void * data)932 static irqreturn_t tps25750_interrupt(int irq, void *data)
933 {
934 struct tps6598x *tps = data;
935 u64 event[2] = { };
936 u32 status;
937 int ret;
938
939 mutex_lock(&tps->lock);
940
941 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event, 11);
942 if (ret) {
943 dev_err(tps->dev, "%s: failed to read events\n", __func__);
944 goto err_unlock;
945 }
946 trace_tps25750_irq(event[0]);
947
948 if (!(event[0] | event[1]))
949 goto err_unlock;
950
951 if (!tps6598x_read_status(tps, &status))
952 goto err_clear_ints;
953
954 if (event[0] & TPS_REG_INT_POWER_STATUS_UPDATE)
955 if (!tps6598x_read_power_status(tps))
956 goto err_clear_ints;
957
958 if (event[0] & TPS_REG_INT_DATA_STATUS_UPDATE)
959 if (!tps->data->read_data_status(tps))
960 goto err_clear_ints;
961
962 /*
963 * data/port roles could be updated independently after
964 * a plug event. Therefore, we need to check
965 * for pr/dr status change to set TypeC dr/pr accordingly.
966 */
967 if (event[0] & TPS_REG_INT_PLUG_EVENT ||
968 tps6598x_has_role_changed(tps, status))
969 tps6598x_handle_plug_event(tps, status);
970
971 tps->status = status;
972
973 err_clear_ints:
974 tps6598x_block_write(tps, TPS_REG_INT_CLEAR1, event, 11);
975
976 err_unlock:
977 mutex_unlock(&tps->lock);
978
979 if (event[0] | event[1])
980 return IRQ_HANDLED;
981 return IRQ_NONE;
982 }
983
tps6598x_interrupt(int irq,void * data)984 static irqreturn_t tps6598x_interrupt(int irq, void *data)
985 {
986 int intev_len = TPS_65981_2_6_INTEVENT_LEN;
987 struct tps6598x *tps = data;
988 u64 event1[2] = { };
989 u64 event2[2] = { };
990 u32 version;
991 u32 status;
992 int ret;
993
994 mutex_lock(&tps->lock);
995
996 ret = tps6598x_read32(tps, TPS_REG_VERSION, &version);
997 if (ret)
998 dev_warn(tps->dev, "%s: failed to read version (%d)\n",
999 __func__, ret);
1000
1001 if (TPS_VERSION_HW_VERSION(version) == TPS_VERSION_HW_65987_8_DH ||
1002 TPS_VERSION_HW_VERSION(version) == TPS_VERSION_HW_65987_8_DK)
1003 intev_len = TPS_65987_8_INTEVENT_LEN;
1004
1005 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event1, intev_len);
1006
1007 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event1, intev_len);
1008 if (ret) {
1009 dev_err(tps->dev, "%s: failed to read event1\n", __func__);
1010 goto err_unlock;
1011 }
1012 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT2, event2, intev_len);
1013 if (ret) {
1014 dev_err(tps->dev, "%s: failed to read event2\n", __func__);
1015 goto err_unlock;
1016 }
1017 trace_tps6598x_irq(event1[0], event2[0]);
1018
1019 if (!(event1[0] | event1[1] | event2[0] | event2[1]))
1020 goto err_unlock;
1021
1022 tps6598x_block_write(tps, TPS_REG_INT_CLEAR1, event1, intev_len);
1023 tps6598x_block_write(tps, TPS_REG_INT_CLEAR2, event2, intev_len);
1024
1025 if (!tps6598x_read_status(tps, &status))
1026 goto err_unlock;
1027
1028 if ((event1[0] | event2[0]) & TPS_REG_INT_POWER_STATUS_UPDATE)
1029 if (!tps6598x_read_power_status(tps))
1030 goto err_unlock;
1031
1032 if ((event1[0] | event2[0]) & TPS_REG_INT_DATA_STATUS_UPDATE)
1033 if (!tps->data->read_data_status(tps))
1034 goto err_unlock;
1035
1036 /* Handle plug insert or removal */
1037 if ((event1[0] | event2[0]) & TPS_REG_INT_PLUG_EVENT)
1038 tps6598x_handle_plug_event(tps, status);
1039
1040 err_unlock:
1041 mutex_unlock(&tps->lock);
1042
1043 if (event1[0] | event1[1] | event2[0] | event2[1])
1044 return IRQ_HANDLED;
1045
1046 return IRQ_NONE;
1047 }
1048
1049 /* Time interval for Polling */
1050 #define POLL_INTERVAL 500 /* msecs */
tps6598x_poll_work(struct work_struct * work)1051 static void tps6598x_poll_work(struct work_struct *work)
1052 {
1053 struct tps6598x *tps = container_of(to_delayed_work(work),
1054 struct tps6598x, wq_poll);
1055
1056 tps->data->irq_handler(0, tps);
1057 queue_delayed_work(system_power_efficient_wq,
1058 &tps->wq_poll, msecs_to_jiffies(POLL_INTERVAL));
1059 }
1060
tps6598x_check_mode(struct tps6598x * tps)1061 static int tps6598x_check_mode(struct tps6598x *tps)
1062 {
1063 char mode[5] = { };
1064 int ret;
1065
1066 ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
1067 if (ret)
1068 return ret;
1069
1070 ret = match_string(modes, ARRAY_SIZE(modes), mode);
1071
1072 switch (ret) {
1073 case TPS_MODE_APP:
1074 case TPS_MODE_PTCH:
1075 return ret;
1076 case TPS_MODE_BOOT:
1077 dev_warn(tps->dev, "dead-battery condition\n");
1078 return ret;
1079 case TPS_MODE_BIST:
1080 case TPS_MODE_DISC:
1081 default:
1082 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
1083 mode);
1084 break;
1085 }
1086
1087 return -ENODEV;
1088 }
1089
1090 static const struct regmap_config tps6598x_regmap_config = {
1091 .reg_bits = 8,
1092 .val_bits = 8,
1093 .max_register = 0x7F,
1094 };
1095
tps6598x_psy_get_online(struct tps6598x * tps,union power_supply_propval * val)1096 static int tps6598x_psy_get_online(struct tps6598x *tps,
1097 union power_supply_propval *val)
1098 {
1099 if (TPS_POWER_STATUS_CONNECTION(tps->pwr_status) &&
1100 TPS_POWER_STATUS_SOURCESINK(tps->pwr_status)) {
1101 val->intval = 1;
1102 } else {
1103 val->intval = 0;
1104 }
1105 return 0;
1106 }
1107
tps6598x_psy_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)1108 static int tps6598x_psy_get_prop(struct power_supply *psy,
1109 enum power_supply_property psp,
1110 union power_supply_propval *val)
1111 {
1112 struct tps6598x *tps = power_supply_get_drvdata(psy);
1113 int ret = 0;
1114
1115 switch (psp) {
1116 case POWER_SUPPLY_PROP_USB_TYPE:
1117 if (TPS_POWER_STATUS_PWROPMODE(tps->pwr_status) == TYPEC_PWR_MODE_PD)
1118 val->intval = POWER_SUPPLY_USB_TYPE_PD;
1119 else
1120 val->intval = POWER_SUPPLY_USB_TYPE_C;
1121 break;
1122 case POWER_SUPPLY_PROP_ONLINE:
1123 ret = tps6598x_psy_get_online(tps, val);
1124 break;
1125 default:
1126 ret = -EINVAL;
1127 break;
1128 }
1129
1130 return ret;
1131 }
1132
cd321x_switch_power_state(struct tps6598x * tps,u8 target_state)1133 static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state)
1134 {
1135 u8 state;
1136 int ret;
1137
1138 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
1139 if (ret)
1140 return ret;
1141
1142 if (state == target_state)
1143 return 0;
1144
1145 ret = tps6598x_exec_cmd(tps, "SSPS", sizeof(u8), &target_state, 0, NULL);
1146 if (ret)
1147 return ret;
1148
1149 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
1150 if (ret)
1151 return ret;
1152
1153 if (state != target_state)
1154 return -EINVAL;
1155
1156 return 0;
1157 }
1158
devm_tps6598_psy_register(struct tps6598x * tps)1159 static int devm_tps6598_psy_register(struct tps6598x *tps)
1160 {
1161 struct power_supply_config psy_cfg = {};
1162 const char *port_dev_name = dev_name(tps->dev);
1163 char *psy_name;
1164
1165 psy_cfg.drv_data = tps;
1166 psy_cfg.fwnode = dev_fwnode(tps->dev);
1167
1168 psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
1169 port_dev_name);
1170 if (!psy_name)
1171 return -ENOMEM;
1172
1173 tps->psy_desc.name = psy_name;
1174 tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
1175 tps->psy_desc.usb_types = BIT(POWER_SUPPLY_USB_TYPE_C) |
1176 BIT(POWER_SUPPLY_USB_TYPE_PD);
1177 tps->psy_desc.properties = tps6598x_psy_props;
1178 tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
1179 tps->psy_desc.get_property = tps6598x_psy_get_prop;
1180
1181 tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
1182
1183 tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
1184 &psy_cfg);
1185 return PTR_ERR_OR_ZERO(tps->psy);
1186 }
1187
1188 static int
tps6598x_register_port(struct tps6598x * tps,struct fwnode_handle * fwnode)1189 tps6598x_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
1190 {
1191 int ret;
1192 u32 conf;
1193 struct typec_capability typec_cap = { };
1194
1195 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
1196 if (ret)
1197 return ret;
1198
1199 typec_cap.revision = USB_TYPEC_REV_1_2;
1200 typec_cap.pd_revision = 0x200;
1201 typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
1202 typec_cap.driver_data = tps;
1203 typec_cap.ops = &tps6598x_ops;
1204 typec_cap.fwnode = fwnode;
1205
1206 switch (TPS_SYSCONF_PORTINFO(conf)) {
1207 case TPS_PORTINFO_SINK_ACCESSORY:
1208 case TPS_PORTINFO_SINK:
1209 typec_cap.type = TYPEC_PORT_SNK;
1210 typec_cap.data = TYPEC_PORT_UFP;
1211 break;
1212 case TPS_PORTINFO_DRP_UFP_DRD:
1213 case TPS_PORTINFO_DRP_DFP_DRD:
1214 typec_cap.type = TYPEC_PORT_DRP;
1215 typec_cap.data = TYPEC_PORT_DRD;
1216 break;
1217 case TPS_PORTINFO_DRP_UFP:
1218 typec_cap.type = TYPEC_PORT_DRP;
1219 typec_cap.data = TYPEC_PORT_UFP;
1220 break;
1221 case TPS_PORTINFO_DRP_DFP:
1222 typec_cap.type = TYPEC_PORT_DRP;
1223 typec_cap.data = TYPEC_PORT_DFP;
1224 break;
1225 case TPS_PORTINFO_SOURCE:
1226 typec_cap.type = TYPEC_PORT_SRC;
1227 typec_cap.data = TYPEC_PORT_DFP;
1228 break;
1229 default:
1230 return -ENODEV;
1231 }
1232
1233 tps->port = typec_register_port(tps->dev, &typec_cap);
1234 if (IS_ERR(tps->port))
1235 return PTR_ERR(tps->port);
1236
1237 return 0;
1238 }
1239
cd321x_register_port_altmodes(struct cd321x * cd321x)1240 static int cd321x_register_port_altmodes(struct cd321x *cd321x)
1241 {
1242 struct typec_altmode_desc desc;
1243 struct typec_altmode *amode;
1244
1245 memset(&desc, 0, sizeof(desc));
1246 desc.svid = USB_TYPEC_DP_SID;
1247 desc.mode = USB_TYPEC_DP_MODE;
1248 desc.vdo = DP_CONF_SET_PIN_ASSIGN(BIT(DP_PIN_ASSIGN_C) | BIT(DP_PIN_ASSIGN_D));
1249 desc.vdo |= DP_CAP_DFP_D;
1250 amode = typec_port_register_altmode(cd321x->tps.port, &desc);
1251 if (IS_ERR(amode))
1252 return PTR_ERR(amode);
1253 cd321x->port_altmode_dp = amode;
1254
1255 memset(&desc, 0, sizeof(desc));
1256 desc.svid = USB_TYPEC_TBT_SID;
1257 desc.mode = TYPEC_ANY_MODE;
1258 amode = typec_port_register_altmode(cd321x->tps.port, &desc);
1259 if (IS_ERR(amode)) {
1260 typec_unregister_altmode(cd321x->port_altmode_dp);
1261 cd321x->port_altmode_dp = NULL;
1262 return PTR_ERR(amode);
1263 }
1264 cd321x->port_altmode_tbt = amode;
1265
1266 return 0;
1267 }
1268
1269 static int
cd321x_register_port(struct tps6598x * tps,struct fwnode_handle * fwnode)1270 cd321x_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
1271 {
1272 struct cd321x *cd321x = container_of(tps, struct cd321x, tps);
1273 int ret;
1274
1275 INIT_DELAYED_WORK(&cd321x->update_work, cd321x_update_work);
1276
1277 ret = tps6598x_register_port(tps, fwnode);
1278 if (ret)
1279 return ret;
1280
1281 ret = cd321x_register_port_altmodes(cd321x);
1282 if (ret)
1283 goto err_unregister_port;
1284
1285 cd321x->mux = fwnode_typec_mux_get(fwnode);
1286 if (IS_ERR(cd321x->mux)) {
1287 ret = PTR_ERR(cd321x->mux);
1288 goto err_unregister_altmodes;
1289 }
1290
1291 cd321x->state.alt = NULL;
1292 cd321x->state.mode = TYPEC_STATE_SAFE;
1293 cd321x->state.data = NULL;
1294 typec_set_mode(tps->port, TYPEC_STATE_SAFE);
1295
1296 return 0;
1297
1298 err_unregister_altmodes:
1299 typec_unregister_altmode(cd321x->port_altmode_dp);
1300 typec_unregister_altmode(cd321x->port_altmode_tbt);
1301 cd321x->port_altmode_dp = NULL;
1302 cd321x->port_altmode_tbt = NULL;
1303 err_unregister_port:
1304 typec_unregister_port(tps->port);
1305 return ret;
1306 }
1307
1308 static void
tps6598x_unregister_port(struct tps6598x * tps)1309 tps6598x_unregister_port(struct tps6598x *tps)
1310 {
1311 typec_unregister_port(tps->port);
1312 }
1313
1314 static void
cd321x_unregister_port(struct tps6598x * tps)1315 cd321x_unregister_port(struct tps6598x *tps)
1316 {
1317 struct cd321x *cd321x = container_of(tps, struct cd321x, tps);
1318
1319 typec_mux_put(cd321x->mux);
1320 cd321x->mux = NULL;
1321 typec_unregister_altmode(cd321x->port_altmode_dp);
1322 cd321x->port_altmode_dp = NULL;
1323 typec_unregister_altmode(cd321x->port_altmode_tbt);
1324 cd321x->port_altmode_tbt = NULL;
1325 typec_unregister_port(tps->port);
1326 }
1327
tps_request_firmware(struct tps6598x * tps,const struct firmware ** fw,const char ** firmware_name)1328 static int tps_request_firmware(struct tps6598x *tps, const struct firmware **fw,
1329 const char **firmware_name)
1330 {
1331 int ret;
1332
1333 ret = device_property_read_string(tps->dev, "firmware-name",
1334 firmware_name);
1335 if (ret)
1336 return ret;
1337
1338 ret = request_firmware(fw, *firmware_name, tps->dev);
1339 if (ret) {
1340 dev_err(tps->dev, "failed to retrieve \"%s\"\n", *firmware_name);
1341 return ret;
1342 }
1343
1344 if ((*fw)->size == 0) {
1345 release_firmware(*fw);
1346 ret = -EINVAL;
1347 }
1348
1349 return ret;
1350 }
1351
1352 static int
tps25750_write_firmware(struct tps6598x * tps,u8 bpms_addr,const u8 * data,size_t len)1353 tps25750_write_firmware(struct tps6598x *tps,
1354 u8 bpms_addr, const u8 *data, size_t len)
1355 {
1356 struct i2c_client *client = to_i2c_client(tps->dev);
1357 int ret;
1358 u8 slave_addr;
1359 int timeout;
1360
1361 slave_addr = client->addr;
1362 timeout = client->adapter->timeout;
1363
1364 /*
1365 * binary configuration size is around ~16Kbytes
1366 * which might take some time to finish writing it
1367 */
1368 client->adapter->timeout = msecs_to_jiffies(5000);
1369 client->addr = bpms_addr;
1370
1371 ret = regmap_raw_write(tps->regmap, data[0], &data[1], len - 1);
1372
1373 client->addr = slave_addr;
1374 client->adapter->timeout = timeout;
1375
1376 return ret;
1377 }
1378
1379 static int
tps25750_exec_pbms(struct tps6598x * tps,u8 * in_data,size_t in_len)1380 tps25750_exec_pbms(struct tps6598x *tps, u8 *in_data, size_t in_len)
1381 {
1382 int ret;
1383 u8 rc;
1384
1385 ret = tps6598x_exec_cmd_tmo(tps, "PBMs", in_len, in_data,
1386 sizeof(rc), &rc, 4000, 0);
1387 if (ret)
1388 return ret;
1389
1390 switch (rc) {
1391 case TPS_TASK_BPMS_INVALID_BUNDLE_SIZE:
1392 dev_err(tps->dev, "%s: invalid fw size\n", __func__);
1393 return -EINVAL;
1394 case TPS_TASK_BPMS_INVALID_SLAVE_ADDR:
1395 dev_err(tps->dev, "%s: invalid slave address\n", __func__);
1396 return -EINVAL;
1397 case TPS_TASK_BPMS_INVALID_TIMEOUT:
1398 dev_err(tps->dev, "%s: timed out\n", __func__);
1399 return -ETIMEDOUT;
1400 default:
1401 break;
1402 }
1403
1404 return 0;
1405 }
1406
tps25750_abort_patch_process(struct tps6598x * tps)1407 static int tps25750_abort_patch_process(struct tps6598x *tps)
1408 {
1409 int ret;
1410
1411 ret = tps6598x_exec_cmd(tps, "PBMe", 0, NULL, 0, NULL);
1412 if (ret)
1413 return ret;
1414
1415 ret = tps6598x_check_mode(tps);
1416 if (ret != TPS_MODE_PTCH)
1417 dev_err(tps->dev, "failed to switch to \"PTCH\" mode\n");
1418
1419 return ret;
1420 }
1421
tps25750_start_patch_burst_mode(struct tps6598x * tps)1422 static int tps25750_start_patch_burst_mode(struct tps6598x *tps)
1423 {
1424 int ret;
1425 const struct firmware *fw;
1426 const char *firmware_name;
1427 struct {
1428 u32 fw_size;
1429 u8 addr;
1430 u8 timeout;
1431 } __packed bpms_data;
1432 u32 addr;
1433 struct device_node *np = tps->dev->of_node;
1434
1435 ret = tps_request_firmware(tps, &fw, &firmware_name);
1436 if (ret)
1437 return ret;
1438
1439 ret = of_property_match_string(np, "reg-names", "patch-address");
1440 if (ret < 0) {
1441 dev_err(tps->dev, "failed to get patch-address %d\n", ret);
1442 goto release_fw;
1443 }
1444
1445 ret = of_property_read_u32_index(np, "reg", ret, &addr);
1446 if (ret)
1447 goto release_fw;
1448
1449 if (addr == 0 || (addr >= 0x20 && addr <= 0x23)) {
1450 dev_err(tps->dev, "wrong patch address %u\n", addr);
1451 ret = -EINVAL;
1452 goto release_fw;
1453 }
1454
1455 bpms_data.addr = (u8)addr;
1456 bpms_data.fw_size = fw->size;
1457 bpms_data.timeout = TPS_BUNDLE_TIMEOUT;
1458
1459 ret = tps25750_exec_pbms(tps, (u8 *)&bpms_data, sizeof(bpms_data));
1460 if (ret)
1461 goto release_fw;
1462
1463 ret = tps25750_write_firmware(tps, bpms_data.addr, fw->data, fw->size);
1464 if (ret) {
1465 dev_err(tps->dev, "Failed to write patch %s of %zu bytes\n",
1466 firmware_name, fw->size);
1467 goto release_fw;
1468 }
1469
1470 /*
1471 * A delay of 500us is required after the firmware is written
1472 * based on pg.62 in tps6598x Host Interface Technical
1473 * Reference Manual
1474 * https://www.ti.com/lit/ug/slvuc05a/slvuc05a.pdf
1475 */
1476 udelay(500);
1477
1478 release_fw:
1479 release_firmware(fw);
1480
1481 return ret;
1482 }
1483
tps25750_complete_patch_process(struct tps6598x * tps)1484 static int tps25750_complete_patch_process(struct tps6598x *tps)
1485 {
1486 int ret;
1487 u8 out_data[40];
1488 u8 dummy[2] = { };
1489
1490 /*
1491 * Without writing something to DATA_IN, this command would
1492 * return an error
1493 */
1494 ret = tps6598x_exec_cmd_tmo(tps, "PBMc", sizeof(dummy), dummy,
1495 sizeof(out_data), out_data, 2000, 20);
1496 if (ret)
1497 return ret;
1498
1499 if (out_data[TPS_PBMC_RC]) {
1500 dev_err(tps->dev,
1501 "%s: pbmc failed: %u\n", __func__,
1502 out_data[TPS_PBMC_RC]);
1503 return -EIO;
1504 }
1505
1506 if (out_data[TPS_PBMC_DPCS]) {
1507 dev_err(tps->dev,
1508 "%s: failed device patch complete status: %u\n",
1509 __func__, out_data[TPS_PBMC_DPCS]);
1510 return -EIO;
1511 }
1512
1513 return 0;
1514 }
1515
tps25750_apply_patch(struct tps6598x * tps)1516 static int tps25750_apply_patch(struct tps6598x *tps)
1517 {
1518 int ret;
1519 unsigned long timeout;
1520 u64 status = 0;
1521
1522 ret = tps6598x_block_read(tps, TPS_REG_BOOT_STATUS, &status, 5);
1523 if (ret)
1524 return ret;
1525 /*
1526 * Nothing to be done if the configuration
1527 * is being loaded from EERPOM
1528 */
1529 if (status & TPS_BOOT_STATUS_I2C_EEPROM_PRESENT)
1530 goto wait_for_app;
1531
1532 ret = tps25750_start_patch_burst_mode(tps);
1533 if (ret) {
1534 tps25750_abort_patch_process(tps);
1535 return ret;
1536 }
1537
1538 ret = tps25750_complete_patch_process(tps);
1539 if (ret)
1540 return ret;
1541
1542 wait_for_app:
1543 timeout = jiffies + msecs_to_jiffies(1000);
1544
1545 do {
1546 ret = tps6598x_check_mode(tps);
1547 if (ret < 0)
1548 return ret;
1549
1550 if (time_is_before_jiffies(timeout))
1551 return -ETIMEDOUT;
1552
1553 } while (ret != TPS_MODE_APP);
1554
1555 /*
1556 * The dead battery flag may be triggered when the controller
1557 * port is connected to a device that can source power and
1558 * attempts to power up both the controller and the board it is on.
1559 * To restore controller functionality, it is necessary to clear
1560 * this flag
1561 */
1562 if (status & TPS_BOOT_STATUS_DEAD_BATTERY_FLAG) {
1563 ret = tps6598x_exec_cmd(tps, "DBfg", 0, NULL, 0, NULL);
1564 if (ret) {
1565 dev_err(tps->dev, "failed to clear dead battery %d\n", ret);
1566 return ret;
1567 }
1568 }
1569
1570 dev_info(tps->dev, "controller switched to \"APP\" mode\n");
1571
1572 return 0;
1573 };
1574
tps6598x_apply_patch(struct tps6598x * tps)1575 static int tps6598x_apply_patch(struct tps6598x *tps)
1576 {
1577 u8 in = TPS_PTCS_CONTENT_DEV | TPS_PTCS_CONTENT_APP;
1578 u8 out[TPS_MAX_LEN] = {0};
1579 size_t in_len = sizeof(in);
1580 size_t copied_bytes = 0;
1581 size_t bytes_left;
1582 const struct firmware *fw;
1583 const char *firmware_name;
1584 int ret;
1585
1586 ret = tps_request_firmware(tps, &fw, &firmware_name);
1587 if (ret)
1588 return ret;
1589
1590 ret = tps6598x_exec_cmd(tps, "PTCs", in_len, &in,
1591 TPS_PTCS_OUT_BYTES, out);
1592 if (ret || out[TPS_PTCS_STATUS] == TPS_PTCS_STATUS_FAIL) {
1593 if (!ret)
1594 ret = -EBUSY;
1595 dev_err(tps->dev, "Update start failed (%d)\n", ret);
1596 goto release_fw;
1597 }
1598
1599 bytes_left = fw->size;
1600 while (bytes_left) {
1601 in_len = min(bytes_left, TPS_MAX_LEN);
1602 ret = tps6598x_exec_cmd(tps, "PTCd", in_len,
1603 fw->data + copied_bytes,
1604 TPS_PTCD_OUT_BYTES, out);
1605 if (ret || out[TPS_PTCD_TRANSFER_STATUS] ||
1606 out[TPS_PTCD_LOADING_STATE] == TPS_PTCD_LOAD_ERR) {
1607 if (!ret)
1608 ret = -EBUSY;
1609 dev_err(tps->dev, "Patch download failed (%d)\n", ret);
1610 goto release_fw;
1611 }
1612 copied_bytes += in_len;
1613 bytes_left -= in_len;
1614 }
1615
1616 ret = tps6598x_exec_cmd(tps, "PTCc", 0, NULL, TPS_PTCC_OUT_BYTES, out);
1617 if (ret || out[TPS_PTCC_DEV] || out[TPS_PTCC_APP]) {
1618 if (!ret)
1619 ret = -EBUSY;
1620 dev_err(tps->dev, "Update completion failed (%d)\n", ret);
1621 goto release_fw;
1622 }
1623 msleep(TPS_SETUP_MS);
1624 dev_info(tps->dev, "Firmware update succeeded\n");
1625
1626 release_fw:
1627 if (ret) {
1628 dev_err(tps->dev, "Failed to write patch %s of %zu bytes\n",
1629 firmware_name, fw->size);
1630 }
1631 release_firmware(fw);
1632
1633 return ret;
1634 }
1635
cd321x_init(struct tps6598x * tps)1636 static int cd321x_init(struct tps6598x *tps)
1637 {
1638 return 0;
1639 }
1640
tps25750_init(struct tps6598x * tps)1641 static int tps25750_init(struct tps6598x *tps)
1642 {
1643 int ret;
1644
1645 ret = tps->data->apply_patch(tps);
1646 if (ret)
1647 return ret;
1648
1649 ret = tps6598x_write8(tps, TPS_REG_SLEEP_CONF,
1650 TPS_SLEEP_CONF_SLEEP_MODE_ALLOWED);
1651 if (ret)
1652 dev_warn(tps->dev,
1653 "%s: failed to enable sleep mode: %d\n",
1654 __func__, ret);
1655
1656 return 0;
1657 }
1658
tps6598x_init(struct tps6598x * tps)1659 static int tps6598x_init(struct tps6598x *tps)
1660 {
1661 return tps->data->apply_patch(tps);
1662 }
1663
cd321x_reset(struct tps6598x * tps)1664 static int cd321x_reset(struct tps6598x *tps)
1665 {
1666 return 0;
1667 }
1668
tps25750_reset(struct tps6598x * tps)1669 static int tps25750_reset(struct tps6598x *tps)
1670 {
1671 return tps6598x_exec_cmd_tmo(tps, "GAID", 0, NULL, 0, NULL, 2000, 0);
1672 }
1673
tps6598x_reset(struct tps6598x * tps)1674 static int tps6598x_reset(struct tps6598x *tps)
1675 {
1676 return 0;
1677 }
1678
1679 static int
tps25750_register_port(struct tps6598x * tps,struct fwnode_handle * fwnode)1680 tps25750_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
1681 {
1682 struct typec_capability typec_cap = { };
1683 const char *data_role;
1684 u8 pd_status;
1685 int ret;
1686
1687 ret = tps6598x_read8(tps, TPS_REG_PD_STATUS, &pd_status);
1688 if (ret)
1689 return ret;
1690
1691 ret = fwnode_property_read_string(fwnode, "data-role", &data_role);
1692 if (ret) {
1693 dev_err(tps->dev, "data-role not found: %d\n", ret);
1694 return ret;
1695 }
1696
1697 ret = typec_find_port_data_role(data_role);
1698 if (ret < 0) {
1699 dev_err(tps->dev, "unknown data-role: %s\n", data_role);
1700 return ret;
1701 }
1702
1703 typec_cap.data = ret;
1704 typec_cap.revision = USB_TYPEC_REV_1_3;
1705 typec_cap.pd_revision = 0x300;
1706 typec_cap.orientation_aware = true;
1707 typec_cap.driver_data = tps;
1708 typec_cap.ops = &tps6598x_ops;
1709 typec_cap.fwnode = fwnode;
1710 typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
1711
1712 switch (TPS_PD_STATUS_PORT_TYPE(pd_status)) {
1713 case TPS_PD_STATUS_PORT_TYPE_SINK_SOURCE:
1714 case TPS_PD_STATUS_PORT_TYPE_SOURCE_SINK:
1715 typec_cap.type = TYPEC_PORT_DRP;
1716 break;
1717 case TPS_PD_STATUS_PORT_TYPE_SINK:
1718 typec_cap.type = TYPEC_PORT_SNK;
1719 break;
1720 case TPS_PD_STATUS_PORT_TYPE_SOURCE:
1721 typec_cap.type = TYPEC_PORT_SRC;
1722 break;
1723 default:
1724 return -ENODEV;
1725 }
1726
1727 tps->port = typec_register_port(tps->dev, &typec_cap);
1728 if (IS_ERR(tps->port))
1729 return PTR_ERR(tps->port);
1730
1731 return 0;
1732 }
1733
cd321x_remove(struct tps6598x * tps)1734 static void cd321x_remove(struct tps6598x *tps)
1735 {
1736 struct cd321x *cd321x = container_of(tps, struct cd321x, tps);
1737
1738 cancel_delayed_work_sync(&cd321x->update_work);
1739 }
1740
tps6598x_probe(struct i2c_client * client)1741 static int tps6598x_probe(struct i2c_client *client)
1742 {
1743 const struct tipd_data *data;
1744 struct tps6598x *tps;
1745 struct fwnode_handle *fwnode;
1746 u32 status;
1747 u32 vid;
1748 int ret;
1749
1750 data = i2c_get_match_data(client);
1751 if (!data)
1752 return -EINVAL;
1753
1754 tps = devm_kzalloc(&client->dev, data->tps_struct_size, GFP_KERNEL);
1755 if (!tps)
1756 return -ENOMEM;
1757
1758 mutex_init(&tps->lock);
1759 tps->dev = &client->dev;
1760 tps->data = data;
1761
1762 tps->reset = devm_gpiod_get_optional(tps->dev, "reset", GPIOD_OUT_LOW);
1763 if (IS_ERR(tps->reset))
1764 return dev_err_probe(tps->dev, PTR_ERR(tps->reset),
1765 "failed to get reset GPIO\n");
1766 if (tps->reset)
1767 msleep(TPS_SETUP_MS);
1768
1769 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
1770 if (IS_ERR(tps->regmap))
1771 return PTR_ERR(tps->regmap);
1772
1773 if (!device_is_compatible(tps->dev, "ti,tps25750")) {
1774 ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
1775 if (ret < 0 || !vid)
1776 return -ENODEV;
1777 }
1778
1779 /*
1780 * Checking can the adapter handle SMBus protocol. If it can not, the
1781 * driver needs to take care of block reads separately.
1782 */
1783 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
1784 tps->i2c_protocol = true;
1785
1786 if (tps->data->switch_power_state) {
1787 ret = tps->data->switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0);
1788 if (ret)
1789 return ret;
1790 }
1791
1792 /* Make sure the controller has application firmware running */
1793 ret = tps6598x_check_mode(tps);
1794 if (ret < 0)
1795 return ret;
1796
1797 if (ret == TPS_MODE_PTCH) {
1798 ret = tps->data->init(tps);
1799 if (ret)
1800 return ret;
1801 }
1802
1803 ret = tps6598x_write64(tps, TPS_REG_INT_MASK1, tps->data->irq_mask1);
1804 if (ret)
1805 goto err_reset_controller;
1806
1807 if (!tps6598x_read_status(tps, &status)) {
1808 ret = -ENODEV;
1809 goto err_clear_mask;
1810 }
1811
1812 /*
1813 * This fwnode has a "compatible" property, but is never populated as a
1814 * struct device. Instead we simply parse it to read the properties.
1815 * This breaks fw_devlink=on. To maintain backward compatibility
1816 * with existing DT files, we work around this by deleting any
1817 * fwnode_links to/from this fwnode.
1818 */
1819 fwnode = device_get_named_child_node(&client->dev, "connector");
1820 if (fwnode)
1821 fw_devlink_purge_absent_suppliers(fwnode);
1822
1823 tps->role_sw = fwnode_usb_role_switch_get(fwnode);
1824 if (IS_ERR(tps->role_sw)) {
1825 ret = PTR_ERR(tps->role_sw);
1826 goto err_fwnode_put;
1827 }
1828
1829 ret = devm_tps6598_psy_register(tps);
1830 if (ret)
1831 goto err_role_put;
1832
1833 ret = tps->data->register_port(tps, fwnode);
1834 if (ret)
1835 goto err_role_put;
1836
1837 if (status & TPS_STATUS_PLUG_PRESENT) {
1838 ret = -EINVAL;
1839 if (!tps6598x_read_power_status(tps))
1840 goto err_unregister_port;
1841 if (!tps->data->read_data_status(tps))
1842 goto err_unregister_port;
1843 ret = tps->data->connect(tps, status);
1844 if (ret)
1845 dev_err(&client->dev, "failed to register partner\n");
1846 }
1847
1848 if (client->irq) {
1849 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
1850 tps->data->irq_handler,
1851 IRQF_SHARED | IRQF_ONESHOT,
1852 dev_name(&client->dev), tps);
1853 } else {
1854 dev_warn(tps->dev, "Unable to find the interrupt, switching to polling\n");
1855 INIT_DELAYED_WORK(&tps->wq_poll, tps6598x_poll_work);
1856 queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,
1857 msecs_to_jiffies(POLL_INTERVAL));
1858 }
1859
1860 if (ret)
1861 goto err_disconnect;
1862
1863 i2c_set_clientdata(client, tps);
1864 fwnode_handle_put(fwnode);
1865
1866 tps->wakeup = device_property_read_bool(tps->dev, "wakeup-source");
1867 if (tps->wakeup && client->irq) {
1868 devm_device_init_wakeup(&client->dev);
1869 enable_irq_wake(client->irq);
1870 }
1871
1872 return 0;
1873
1874 err_disconnect:
1875 tps6598x_disconnect(tps, 0);
1876 err_unregister_port:
1877 tps->data->unregister_port(tps);
1878 err_role_put:
1879 usb_role_switch_put(tps->role_sw);
1880 err_fwnode_put:
1881 fwnode_handle_put(fwnode);
1882 err_clear_mask:
1883 tps6598x_write64(tps, TPS_REG_INT_MASK1, 0);
1884 err_reset_controller:
1885 /* Reset PD controller to remove any applied patch */
1886 tps->data->reset(tps);
1887
1888 return ret;
1889 }
1890
tps6598x_remove(struct i2c_client * client)1891 static void tps6598x_remove(struct i2c_client *client)
1892 {
1893 struct tps6598x *tps = i2c_get_clientdata(client);
1894
1895 if (!client->irq)
1896 cancel_delayed_work_sync(&tps->wq_poll);
1897 else
1898 devm_free_irq(tps->dev, client->irq, tps);
1899
1900 if (tps->data->remove)
1901 tps->data->remove(tps);
1902
1903 tps6598x_disconnect(tps, 0);
1904 tps->data->unregister_port(tps);
1905 usb_role_switch_put(tps->role_sw);
1906
1907 /* Reset PD controller to remove any applied patch */
1908 tps->data->reset(tps);
1909
1910 if (tps->reset)
1911 gpiod_set_value_cansleep(tps->reset, 1);
1912 }
1913
tps6598x_suspend(struct device * dev)1914 static int __maybe_unused tps6598x_suspend(struct device *dev)
1915 {
1916 struct i2c_client *client = to_i2c_client(dev);
1917 struct tps6598x *tps = i2c_get_clientdata(client);
1918
1919 if (tps->wakeup) {
1920 disable_irq(client->irq);
1921 enable_irq_wake(client->irq);
1922 } else if (tps->reset) {
1923 gpiod_set_value_cansleep(tps->reset, 1);
1924 }
1925
1926 if (!client->irq)
1927 cancel_delayed_work_sync(&tps->wq_poll);
1928
1929 return 0;
1930 }
1931
tps6598x_resume(struct device * dev)1932 static int __maybe_unused tps6598x_resume(struct device *dev)
1933 {
1934 struct i2c_client *client = to_i2c_client(dev);
1935 struct tps6598x *tps = i2c_get_clientdata(client);
1936 int ret;
1937
1938 ret = tps6598x_check_mode(tps);
1939 if (ret < 0)
1940 return ret;
1941
1942 if (ret == TPS_MODE_PTCH) {
1943 ret = tps->data->init(tps);
1944 if (ret)
1945 return ret;
1946 }
1947
1948 if (tps->wakeup) {
1949 disable_irq_wake(client->irq);
1950 enable_irq(client->irq);
1951 } else if (tps->reset) {
1952 gpiod_set_value_cansleep(tps->reset, 0);
1953 msleep(TPS_SETUP_MS);
1954 }
1955
1956 if (!client->irq)
1957 queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,
1958 msecs_to_jiffies(POLL_INTERVAL));
1959
1960 return 0;
1961 }
1962
1963 static const struct dev_pm_ops tps6598x_pm_ops = {
1964 SET_SYSTEM_SLEEP_PM_OPS(tps6598x_suspend, tps6598x_resume)
1965 };
1966
1967 static const struct tipd_data cd321x_data = {
1968 .irq_handler = cd321x_interrupt,
1969 .irq_mask1 = APPLE_CD_REG_INT_POWER_STATUS_UPDATE |
1970 APPLE_CD_REG_INT_DATA_STATUS_UPDATE |
1971 APPLE_CD_REG_INT_PLUG_EVENT,
1972 .tps_struct_size = sizeof(struct cd321x),
1973 .remove = cd321x_remove,
1974 .register_port = cd321x_register_port,
1975 .unregister_port = cd321x_unregister_port,
1976 .trace_data_status = trace_cd321x_data_status,
1977 .trace_power_status = trace_tps6598x_power_status,
1978 .trace_status = trace_tps6598x_status,
1979 .init = cd321x_init,
1980 .read_data_status = cd321x_read_data_status,
1981 .reset = cd321x_reset,
1982 .switch_power_state = cd321x_switch_power_state,
1983 .connect = cd321x_connect,
1984 };
1985
1986 static const struct tipd_data tps6598x_data = {
1987 .irq_handler = tps6598x_interrupt,
1988 .irq_mask1 = TPS_REG_INT_POWER_STATUS_UPDATE |
1989 TPS_REG_INT_DATA_STATUS_UPDATE |
1990 TPS_REG_INT_PLUG_EVENT,
1991 .tps_struct_size = sizeof(struct tps6598x),
1992 .register_port = tps6598x_register_port,
1993 .unregister_port = tps6598x_unregister_port,
1994 .trace_data_status = trace_tps6598x_data_status,
1995 .trace_power_status = trace_tps6598x_power_status,
1996 .trace_status = trace_tps6598x_status,
1997 .apply_patch = tps6598x_apply_patch,
1998 .init = tps6598x_init,
1999 .read_data_status = tps6598x_read_data_status,
2000 .reset = tps6598x_reset,
2001 .connect = tps6598x_connect,
2002 };
2003
2004 static const struct tipd_data tps25750_data = {
2005 .irq_handler = tps25750_interrupt,
2006 .irq_mask1 = TPS_REG_INT_POWER_STATUS_UPDATE |
2007 TPS_REG_INT_DATA_STATUS_UPDATE |
2008 TPS_REG_INT_PLUG_EVENT,
2009 .tps_struct_size = sizeof(struct tps6598x),
2010 .register_port = tps25750_register_port,
2011 .unregister_port = tps6598x_unregister_port,
2012 .trace_data_status = trace_tps6598x_data_status,
2013 .trace_power_status = trace_tps25750_power_status,
2014 .trace_status = trace_tps25750_status,
2015 .apply_patch = tps25750_apply_patch,
2016 .init = tps25750_init,
2017 .read_data_status = tps6598x_read_data_status,
2018 .reset = tps25750_reset,
2019 .connect = tps6598x_connect,
2020 };
2021
2022 static const struct of_device_id tps6598x_of_match[] = {
2023 { .compatible = "ti,tps6598x", &tps6598x_data},
2024 { .compatible = "apple,cd321x", &cd321x_data},
2025 { .compatible = "ti,tps25750", &tps25750_data},
2026 {}
2027 };
2028 MODULE_DEVICE_TABLE(of, tps6598x_of_match);
2029
2030 static const struct i2c_device_id tps6598x_id[] = {
2031 { "tps6598x", (kernel_ulong_t)&tps6598x_data },
2032 { }
2033 };
2034 MODULE_DEVICE_TABLE(i2c, tps6598x_id);
2035
2036 static struct i2c_driver tps6598x_i2c_driver = {
2037 .driver = {
2038 .name = "tps6598x",
2039 .pm = &tps6598x_pm_ops,
2040 .of_match_table = tps6598x_of_match,
2041 },
2042 .probe = tps6598x_probe,
2043 .remove = tps6598x_remove,
2044 .id_table = tps6598x_id,
2045 };
2046 module_i2c_driver(tps6598x_i2c_driver);
2047
2048 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
2049 MODULE_LICENSE("GPL v2");
2050 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");
2051