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