usb: typec: tps6598x: Add trace event for IRQ events
[linux-2.6-microblaze.git] / drivers / usb / typec / tps6598x.c
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 "tps6598x.h"
10
11 #include <linux/i2c.h>
12 #include <linux/acpi.h>
13 #include <linux/module.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/role.h>
19
20 #define CREATE_TRACE_POINTS
21 #include "tps6598x_trace.h"
22
23 /* Register offsets */
24 #define TPS_REG_VID                     0x00
25 #define TPS_REG_MODE                    0x03
26 #define TPS_REG_CMD1                    0x08
27 #define TPS_REG_DATA1                   0x09
28 #define TPS_REG_INT_EVENT1              0x14
29 #define TPS_REG_INT_EVENT2              0x15
30 #define TPS_REG_INT_MASK1               0x16
31 #define TPS_REG_INT_MASK2               0x17
32 #define TPS_REG_INT_CLEAR1              0x18
33 #define TPS_REG_INT_CLEAR2              0x19
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
40 /* TPS_REG_STATUS bits */
41 #define TPS_STATUS_PLUG_PRESENT         BIT(0)
42 #define TPS_STATUS_ORIENTATION          BIT(4)
43 #define TPS_STATUS_PORTROLE(s)          (!!((s) & BIT(5)))
44 #define TPS_STATUS_DATAROLE(s)          (!!((s) & BIT(6)))
45 #define TPS_STATUS_VCONN(s)             (!!((s) & BIT(7)))
46
47 /* TPS_REG_SYSTEM_CONF bits */
48 #define TPS_SYSCONF_PORTINFO(c)         ((c) & 7)
49
50 enum {
51         TPS_PORTINFO_SINK,
52         TPS_PORTINFO_SINK_ACCESSORY,
53         TPS_PORTINFO_DRP_UFP,
54         TPS_PORTINFO_DRP_UFP_DRD,
55         TPS_PORTINFO_DRP_DFP,
56         TPS_PORTINFO_DRP_DFP_DRD,
57         TPS_PORTINFO_SOURCE,
58 };
59
60 /* TPS_REG_POWER_STATUS bits */
61 #define TPS_POWER_STATUS_CONNECTION     BIT(0)
62 #define TPS_POWER_STATUS_SOURCESINK     BIT(1)
63 #define TPS_POWER_STATUS_PWROPMODE(p)   (((p) & GENMASK(3, 2)) >> 2)
64
65 /* TPS_REG_RX_IDENTITY_SOP */
66 struct tps6598x_rx_identity_reg {
67         u8 status;
68         struct usb_pd_identity identity;
69         u32 vdo[3];
70 } __packed;
71
72 /* Standard Task return codes */
73 #define TPS_TASK_TIMEOUT                1
74 #define TPS_TASK_REJECTED               3
75
76 enum {
77         TPS_MODE_APP,
78         TPS_MODE_BOOT,
79         TPS_MODE_BIST,
80         TPS_MODE_DISC,
81 };
82
83 static const char *const modes[] = {
84         [TPS_MODE_APP]  = "APP ",
85         [TPS_MODE_BOOT] = "BOOT",
86         [TPS_MODE_BIST] = "BIST",
87         [TPS_MODE_DISC] = "DISC",
88 };
89
90 /* Unrecognized commands will be replaced with "!CMD" */
91 #define INVALID_CMD(_cmd_)              (_cmd_ == 0x444d4321)
92
93 struct tps6598x {
94         struct device *dev;
95         struct regmap *regmap;
96         struct mutex lock; /* device lock */
97         u8 i2c_protocol:1;
98
99         struct typec_port *port;
100         struct typec_partner *partner;
101         struct usb_pd_identity partner_identity;
102         struct usb_role_switch *role_sw;
103         struct typec_capability typec_cap;
104
105         struct power_supply *psy;
106         struct power_supply_desc psy_desc;
107         enum power_supply_usb_type usb_type;
108 };
109
110 static enum power_supply_property tps6598x_psy_props[] = {
111         POWER_SUPPLY_PROP_USB_TYPE,
112         POWER_SUPPLY_PROP_ONLINE,
113 };
114
115 static enum power_supply_usb_type tps6598x_psy_usb_types[] = {
116         POWER_SUPPLY_USB_TYPE_C,
117         POWER_SUPPLY_USB_TYPE_PD,
118 };
119
120 static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-";
121
122 /*
123  * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
124  * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
125  */
126 #define TPS_MAX_LEN     64
127
128 static int
129 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
130 {
131         u8 data[TPS_MAX_LEN + 1];
132         int ret;
133
134         if (WARN_ON(len + 1 > sizeof(data)))
135                 return -EINVAL;
136
137         if (!tps->i2c_protocol)
138                 return regmap_raw_read(tps->regmap, reg, val, len);
139
140         ret = regmap_raw_read(tps->regmap, reg, data, sizeof(data));
141         if (ret)
142                 return ret;
143
144         if (data[0] < len)
145                 return -EIO;
146
147         memcpy(val, &data[1], len);
148         return 0;
149 }
150
151 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
152                                 const void *val, size_t len)
153 {
154         u8 data[TPS_MAX_LEN + 1];
155
156         if (!tps->i2c_protocol)
157                 return regmap_raw_write(tps->regmap, reg, val, len);
158
159         data[0] = len;
160         memcpy(&data[1], val, len);
161
162         return regmap_raw_write(tps->regmap, reg, data, sizeof(data));
163 }
164
165 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
166 {
167         return tps6598x_block_read(tps, reg, val, sizeof(u16));
168 }
169
170 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
171 {
172         return tps6598x_block_read(tps, reg, val, sizeof(u32));
173 }
174
175 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
176 {
177         return tps6598x_block_read(tps, reg, val, sizeof(u64));
178 }
179
180 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
181 {
182         return tps6598x_block_write(tps, reg, &val, sizeof(u16));
183 }
184
185 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
186 {
187         return tps6598x_block_write(tps, reg, &val, sizeof(u32));
188 }
189
190 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
191 {
192         return tps6598x_block_write(tps, reg, &val, sizeof(u64));
193 }
194
195 static inline int
196 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
197 {
198         return tps6598x_block_write(tps, reg, val, 4);
199 }
200
201 static int tps6598x_read_partner_identity(struct tps6598x *tps)
202 {
203         struct tps6598x_rx_identity_reg id;
204         int ret;
205
206         ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
207                                   &id, sizeof(id));
208         if (ret)
209                 return ret;
210
211         tps->partner_identity = id.identity;
212
213         return 0;
214 }
215
216 static void tps6598x_set_data_role(struct tps6598x *tps,
217                                    enum typec_data_role role, bool connected)
218 {
219         enum usb_role role_val;
220
221         if (role == TYPEC_HOST)
222                 role_val = USB_ROLE_HOST;
223         else
224                 role_val = USB_ROLE_DEVICE;
225
226         if (!connected)
227                 role_val = USB_ROLE_NONE;
228
229         usb_role_switch_set_role(tps->role_sw, role_val);
230         typec_set_data_role(tps->port, role);
231 }
232
233 static int tps6598x_connect(struct tps6598x *tps, u32 status)
234 {
235         struct typec_partner_desc desc;
236         enum typec_pwr_opmode mode;
237         u16 pwr_status;
238         int ret;
239
240         if (tps->partner)
241                 return 0;
242
243         ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
244         if (ret < 0)
245                 return ret;
246
247         mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
248
249         desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
250         desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
251         desc.identity = NULL;
252
253         if (desc.usb_pd) {
254                 ret = tps6598x_read_partner_identity(tps);
255                 if (ret)
256                         return ret;
257                 desc.identity = &tps->partner_identity;
258         }
259
260         typec_set_pwr_opmode(tps->port, mode);
261         typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
262         typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
263         tps6598x_set_data_role(tps, TPS_STATUS_DATAROLE(status), true);
264
265         tps->partner = typec_register_partner(tps->port, &desc);
266         if (IS_ERR(tps->partner))
267                 return PTR_ERR(tps->partner);
268
269         if (desc.identity)
270                 typec_partner_set_identity(tps->partner);
271
272         power_supply_changed(tps->psy);
273
274         return 0;
275 }
276
277 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
278 {
279         if (!IS_ERR(tps->partner))
280                 typec_unregister_partner(tps->partner);
281         tps->partner = NULL;
282         typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
283         typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
284         typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
285         tps6598x_set_data_role(tps, TPS_STATUS_DATAROLE(status), false);
286         power_supply_changed(tps->psy);
287 }
288
289 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
290                              size_t in_len, u8 *in_data,
291                              size_t out_len, u8 *out_data)
292 {
293         unsigned long timeout;
294         u32 val;
295         int ret;
296
297         ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
298         if (ret)
299                 return ret;
300         if (val && !INVALID_CMD(val))
301                 return -EBUSY;
302
303         if (in_len) {
304                 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
305                                            in_data, in_len);
306                 if (ret)
307                         return ret;
308         }
309
310         ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
311         if (ret < 0)
312                 return ret;
313
314         /* XXX: Using 1s for now, but it may not be enough for every command. */
315         timeout = jiffies + msecs_to_jiffies(1000);
316
317         do {
318                 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
319                 if (ret)
320                         return ret;
321                 if (INVALID_CMD(val))
322                         return -EINVAL;
323
324                 if (time_is_before_jiffies(timeout))
325                         return -ETIMEDOUT;
326         } while (val);
327
328         if (out_len) {
329                 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
330                                           out_data, out_len);
331                 if (ret)
332                         return ret;
333                 val = out_data[0];
334         } else {
335                 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
336                 if (ret)
337                         return ret;
338         }
339
340         switch (val) {
341         case TPS_TASK_TIMEOUT:
342                 return -ETIMEDOUT;
343         case TPS_TASK_REJECTED:
344                 return -EPERM;
345         default:
346                 break;
347         }
348
349         return 0;
350 }
351
352 static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
353 {
354         const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
355         struct tps6598x *tps = typec_get_drvdata(port);
356         u32 status;
357         int ret;
358
359         mutex_lock(&tps->lock);
360
361         ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
362         if (ret)
363                 goto out_unlock;
364
365         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
366         if (ret)
367                 goto out_unlock;
368
369         if (role != TPS_STATUS_DATAROLE(status)) {
370                 ret = -EPROTO;
371                 goto out_unlock;
372         }
373
374         tps6598x_set_data_role(tps, role, true);
375
376 out_unlock:
377         mutex_unlock(&tps->lock);
378
379         return ret;
380 }
381
382 static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
383 {
384         const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
385         struct tps6598x *tps = typec_get_drvdata(port);
386         u32 status;
387         int ret;
388
389         mutex_lock(&tps->lock);
390
391         ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
392         if (ret)
393                 goto out_unlock;
394
395         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
396         if (ret)
397                 goto out_unlock;
398
399         if (role != TPS_STATUS_PORTROLE(status)) {
400                 ret = -EPROTO;
401                 goto out_unlock;
402         }
403
404         typec_set_pwr_role(tps->port, role);
405
406 out_unlock:
407         mutex_unlock(&tps->lock);
408
409         return ret;
410 }
411
412 static const struct typec_operations tps6598x_ops = {
413         .dr_set = tps6598x_dr_set,
414         .pr_set = tps6598x_pr_set,
415 };
416
417 static irqreturn_t tps6598x_interrupt(int irq, void *data)
418 {
419         struct tps6598x *tps = data;
420         u64 event1;
421         u64 event2;
422         u32 status;
423         int ret;
424
425         mutex_lock(&tps->lock);
426
427         ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
428         ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
429         if (ret) {
430                 dev_err(tps->dev, "%s: failed to read events\n", __func__);
431                 goto err_unlock;
432         }
433         trace_tps6598x_irq(event1, event2);
434
435         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
436         if (ret) {
437                 dev_err(tps->dev, "%s: failed to read status\n", __func__);
438                 goto err_clear_ints;
439         }
440
441         /* Handle plug insert or removal */
442         if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) {
443                 if (status & TPS_STATUS_PLUG_PRESENT) {
444                         ret = tps6598x_connect(tps, status);
445                         if (ret)
446                                 dev_err(tps->dev,
447                                         "failed to register partner\n");
448                 } else {
449                         tps6598x_disconnect(tps, status);
450                 }
451         }
452
453 err_clear_ints:
454         tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
455         tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
456
457 err_unlock:
458         mutex_unlock(&tps->lock);
459
460         return IRQ_HANDLED;
461 }
462
463 static int tps6598x_check_mode(struct tps6598x *tps)
464 {
465         char mode[5] = { };
466         int ret;
467
468         ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
469         if (ret)
470                 return ret;
471
472         switch (match_string(modes, ARRAY_SIZE(modes), mode)) {
473         case TPS_MODE_APP:
474                 return 0;
475         case TPS_MODE_BOOT:
476                 dev_warn(tps->dev, "dead-battery condition\n");
477                 return 0;
478         case TPS_MODE_BIST:
479         case TPS_MODE_DISC:
480         default:
481                 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
482                         mode);
483                 break;
484         }
485
486         return -ENODEV;
487 }
488
489 static const struct regmap_config tps6598x_regmap_config = {
490         .reg_bits = 8,
491         .val_bits = 8,
492         .max_register = 0x7F,
493 };
494
495 static int tps6598x_psy_get_online(struct tps6598x *tps,
496                                    union power_supply_propval *val)
497 {
498         int ret;
499         u16 pwr_status;
500
501         ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
502         if (ret < 0)
503                 return ret;
504
505         if ((pwr_status & TPS_POWER_STATUS_CONNECTION) &&
506             (pwr_status & TPS_POWER_STATUS_SOURCESINK)) {
507                 val->intval = 1;
508         } else {
509                 val->intval = 0;
510         }
511         return 0;
512 }
513
514 static int tps6598x_psy_get_prop(struct power_supply *psy,
515                                  enum power_supply_property psp,
516                                  union power_supply_propval *val)
517 {
518         struct tps6598x *tps = power_supply_get_drvdata(psy);
519         u16 pwr_status;
520         int ret = 0;
521
522         switch (psp) {
523         case POWER_SUPPLY_PROP_USB_TYPE:
524                 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
525                 if (ret < 0)
526                         return ret;
527                 if (TPS_POWER_STATUS_PWROPMODE(pwr_status) == TYPEC_PWR_MODE_PD)
528                         val->intval = POWER_SUPPLY_USB_TYPE_PD;
529                 else
530                         val->intval = POWER_SUPPLY_USB_TYPE_C;
531                 break;
532         case POWER_SUPPLY_PROP_ONLINE:
533                 ret = tps6598x_psy_get_online(tps, val);
534                 break;
535         default:
536                 ret = -EINVAL;
537                 break;
538         }
539
540         return ret;
541 }
542
543 static int devm_tps6598_psy_register(struct tps6598x *tps)
544 {
545         struct power_supply_config psy_cfg = {};
546         const char *port_dev_name = dev_name(tps->dev);
547         char *psy_name;
548
549         psy_cfg.drv_data = tps;
550         psy_cfg.fwnode = dev_fwnode(tps->dev);
551
552         psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
553                                   port_dev_name);
554         if (!psy_name)
555                 return -ENOMEM;
556
557         tps->psy_desc.name = psy_name;
558         tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
559         tps->psy_desc.usb_types = tps6598x_psy_usb_types;
560         tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types);
561         tps->psy_desc.properties = tps6598x_psy_props;
562         tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
563         tps->psy_desc.get_property = tps6598x_psy_get_prop;
564
565         tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
566
567         tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
568                                                &psy_cfg);
569         return PTR_ERR_OR_ZERO(tps->psy);
570 }
571
572 static int tps6598x_probe(struct i2c_client *client)
573 {
574         struct typec_capability typec_cap = { };
575         struct tps6598x *tps;
576         struct fwnode_handle *fwnode;
577         u32 status;
578         u32 conf;
579         u32 vid;
580         int ret;
581
582         tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
583         if (!tps)
584                 return -ENOMEM;
585
586         mutex_init(&tps->lock);
587         tps->dev = &client->dev;
588
589         tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
590         if (IS_ERR(tps->regmap))
591                 return PTR_ERR(tps->regmap);
592
593         ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
594         if (ret < 0 || !vid)
595                 return -ENODEV;
596
597         /*
598          * Checking can the adapter handle SMBus protocol. If it can not, the
599          * driver needs to take care of block reads separately.
600          *
601          * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
602          * unconditionally if the adapter has I2C_FUNC_I2C set.
603          */
604         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
605                 tps->i2c_protocol = true;
606
607         /* Make sure the controller has application firmware running */
608         ret = tps6598x_check_mode(tps);
609         if (ret)
610                 return ret;
611
612         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
613         if (ret < 0)
614                 return ret;
615
616         ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
617         if (ret < 0)
618                 return ret;
619
620         fwnode = device_get_named_child_node(&client->dev, "connector");
621         if (IS_ERR(fwnode))
622                 return PTR_ERR(fwnode);
623
624         tps->role_sw = fwnode_usb_role_switch_get(fwnode);
625         if (IS_ERR(tps->role_sw)) {
626                 ret = PTR_ERR(tps->role_sw);
627                 goto err_fwnode_put;
628         }
629
630         typec_cap.revision = USB_TYPEC_REV_1_2;
631         typec_cap.pd_revision = 0x200;
632         typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
633         typec_cap.driver_data = tps;
634         typec_cap.ops = &tps6598x_ops;
635         typec_cap.fwnode = fwnode;
636
637         switch (TPS_SYSCONF_PORTINFO(conf)) {
638         case TPS_PORTINFO_SINK_ACCESSORY:
639         case TPS_PORTINFO_SINK:
640                 typec_cap.type = TYPEC_PORT_SNK;
641                 typec_cap.data = TYPEC_PORT_UFP;
642                 break;
643         case TPS_PORTINFO_DRP_UFP_DRD:
644         case TPS_PORTINFO_DRP_DFP_DRD:
645                 typec_cap.type = TYPEC_PORT_DRP;
646                 typec_cap.data = TYPEC_PORT_DRD;
647                 break;
648         case TPS_PORTINFO_DRP_UFP:
649                 typec_cap.type = TYPEC_PORT_DRP;
650                 typec_cap.data = TYPEC_PORT_UFP;
651                 break;
652         case TPS_PORTINFO_DRP_DFP:
653                 typec_cap.type = TYPEC_PORT_DRP;
654                 typec_cap.data = TYPEC_PORT_DFP;
655                 break;
656         case TPS_PORTINFO_SOURCE:
657                 typec_cap.type = TYPEC_PORT_SRC;
658                 typec_cap.data = TYPEC_PORT_DFP;
659                 break;
660         default:
661                 ret = -ENODEV;
662                 goto err_role_put;
663         }
664
665         ret = devm_tps6598_psy_register(tps);
666         if (ret)
667                 return ret;
668
669         tps->port = typec_register_port(&client->dev, &typec_cap);
670         if (IS_ERR(tps->port)) {
671                 ret = PTR_ERR(tps->port);
672                 goto err_role_put;
673         }
674         fwnode_handle_put(fwnode);
675
676         if (status & TPS_STATUS_PLUG_PRESENT) {
677                 ret = tps6598x_connect(tps, status);
678                 if (ret)
679                         dev_err(&client->dev, "failed to register partner\n");
680         }
681
682         ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
683                                         tps6598x_interrupt,
684                                         IRQF_SHARED | IRQF_ONESHOT,
685                                         dev_name(&client->dev), tps);
686         if (ret) {
687                 tps6598x_disconnect(tps, 0);
688                 typec_unregister_port(tps->port);
689                 goto err_role_put;
690         }
691
692         i2c_set_clientdata(client, tps);
693
694         return 0;
695
696 err_role_put:
697         usb_role_switch_put(tps->role_sw);
698 err_fwnode_put:
699         fwnode_handle_put(fwnode);
700
701         return ret;
702 }
703
704 static int tps6598x_remove(struct i2c_client *client)
705 {
706         struct tps6598x *tps = i2c_get_clientdata(client);
707
708         tps6598x_disconnect(tps, 0);
709         typec_unregister_port(tps->port);
710         usb_role_switch_put(tps->role_sw);
711
712         return 0;
713 }
714
715 static const struct of_device_id tps6598x_of_match[] = {
716         { .compatible = "ti,tps6598x", },
717         {}
718 };
719 MODULE_DEVICE_TABLE(of, tps6598x_of_match);
720
721 static const struct i2c_device_id tps6598x_id[] = {
722         { "tps6598x" },
723         { }
724 };
725 MODULE_DEVICE_TABLE(i2c, tps6598x_id);
726
727 static struct i2c_driver tps6598x_i2c_driver = {
728         .driver = {
729                 .name = "tps6598x",
730                 .of_match_table = tps6598x_of_match,
731         },
732         .probe_new = tps6598x_probe,
733         .remove = tps6598x_remove,
734         .id_table = tps6598x_id,
735 };
736 module_i2c_driver(tps6598x_i2c_driver);
737
738 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
739 MODULE_LICENSE("GPL v2");
740 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");