GNU Linux-libre 5.10.153-gnu1
[releases.git] / drivers / usb / typec / tcpm / fusb302.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2016-2017 Google, Inc
4  *
5  * Fairchild FUSB302 Type-C Chip Driver
6  */
7
8 #include <linux/debugfs.h>
9 #include <linux/delay.h>
10 #include <linux/errno.h>
11 #include <linux/extcon.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of_device.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/proc_fs.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/sched/clock.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
28 #include <linux/usb.h>
29 #include <linux/usb/typec.h>
30 #include <linux/usb/tcpm.h>
31 #include <linux/usb/pd.h>
32 #include <linux/workqueue.h>
33
34 #include "fusb302_reg.h"
35
36 /*
37  * When the device is SNK, BC_LVL interrupt is used to monitor cc pins
38  * for the current capability offered by the SRC. As FUSB302 chip fires
39  * the BC_LVL interrupt on PD signalings, cc lvl should be handled after
40  * a delay to avoid measuring on PD activities. The delay is slightly
41  * longer than PD_T_PD_DEBPUNCE (10-20ms).
42  */
43 #define T_BC_LVL_DEBOUNCE_DELAY_MS 30
44
45 enum toggling_mode {
46         TOGGLING_MODE_OFF,
47         TOGGLING_MODE_DRP,
48         TOGGLING_MODE_SNK,
49         TOGGLING_MODE_SRC,
50 };
51
52 enum src_current_status {
53         SRC_CURRENT_DEFAULT,
54         SRC_CURRENT_MEDIUM,
55         SRC_CURRENT_HIGH,
56 };
57
58 static const u8 ra_mda_value[] = {
59         [SRC_CURRENT_DEFAULT] = 4,      /* 210mV */
60         [SRC_CURRENT_MEDIUM] = 9,       /* 420mV */
61         [SRC_CURRENT_HIGH] = 18,        /* 798mV */
62 };
63
64 static const u8 rd_mda_value[] = {
65         [SRC_CURRENT_DEFAULT] = 38,     /* 1638mV */
66         [SRC_CURRENT_MEDIUM] = 38,      /* 1638mV */
67         [SRC_CURRENT_HIGH] = 61,        /* 2604mV */
68 };
69
70 #define LOG_BUFFER_ENTRIES      1024
71 #define LOG_BUFFER_ENTRY_SIZE   128
72
73 struct fusb302_chip {
74         struct device *dev;
75         struct i2c_client *i2c_client;
76         struct tcpm_port *tcpm_port;
77         struct tcpc_dev tcpc_dev;
78
79         struct regulator *vbus;
80
81         spinlock_t irq_lock;
82         struct work_struct irq_work;
83         bool irq_suspended;
84         bool irq_while_suspended;
85         struct gpio_desc *gpio_int_n;
86         int gpio_int_n_irq;
87         struct extcon_dev *extcon;
88
89         struct workqueue_struct *wq;
90         struct delayed_work bc_lvl_handler;
91
92         /* lock for sharing chip states */
93         struct mutex lock;
94
95         /* chip status */
96         enum toggling_mode toggling_mode;
97         enum src_current_status src_current_status;
98         bool intr_togdone;
99         bool intr_bc_lvl;
100         bool intr_comp_chng;
101
102         /* port status */
103         bool vconn_on;
104         bool vbus_on;
105         bool charge_on;
106         bool vbus_present;
107         enum typec_cc_polarity cc_polarity;
108         enum typec_cc_status cc1;
109         enum typec_cc_status cc2;
110         u32 snk_pdo[PDO_MAX_OBJECTS];
111
112 #ifdef CONFIG_DEBUG_FS
113         struct dentry *dentry;
114         /* lock for log buffer access */
115         struct mutex logbuffer_lock;
116         int logbuffer_head;
117         int logbuffer_tail;
118         u8 *logbuffer[LOG_BUFFER_ENTRIES];
119 #endif
120 };
121
122 /*
123  * Logging
124  */
125
126 #ifdef CONFIG_DEBUG_FS
127 static bool fusb302_log_full(struct fusb302_chip *chip)
128 {
129         return chip->logbuffer_tail ==
130                 (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
131 }
132
133 __printf(2, 0)
134 static void _fusb302_log(struct fusb302_chip *chip, const char *fmt,
135                          va_list args)
136 {
137         char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
138         u64 ts_nsec = local_clock();
139         unsigned long rem_nsec;
140
141         if (!chip->logbuffer[chip->logbuffer_head]) {
142                 chip->logbuffer[chip->logbuffer_head] =
143                                 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
144                 if (!chip->logbuffer[chip->logbuffer_head])
145                         return;
146         }
147
148         vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
149
150         mutex_lock(&chip->logbuffer_lock);
151
152         if (fusb302_log_full(chip)) {
153                 chip->logbuffer_head = max(chip->logbuffer_head - 1, 0);
154                 strlcpy(tmpbuffer, "overflow", sizeof(tmpbuffer));
155         }
156
157         if (chip->logbuffer_head < 0 ||
158             chip->logbuffer_head >= LOG_BUFFER_ENTRIES) {
159                 dev_warn(chip->dev,
160                          "Bad log buffer index %d\n", chip->logbuffer_head);
161                 goto abort;
162         }
163
164         if (!chip->logbuffer[chip->logbuffer_head]) {
165                 dev_warn(chip->dev,
166                          "Log buffer index %d is NULL\n", chip->logbuffer_head);
167                 goto abort;
168         }
169
170         rem_nsec = do_div(ts_nsec, 1000000000);
171         scnprintf(chip->logbuffer[chip->logbuffer_head],
172                   LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
173                   (unsigned long)ts_nsec, rem_nsec / 1000,
174                   tmpbuffer);
175         chip->logbuffer_head = (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
176
177 abort:
178         mutex_unlock(&chip->logbuffer_lock);
179 }
180
181 __printf(2, 3)
182 static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
183 {
184         va_list args;
185
186         va_start(args, fmt);
187         _fusb302_log(chip, fmt, args);
188         va_end(args);
189 }
190
191 static int fusb302_debug_show(struct seq_file *s, void *v)
192 {
193         struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
194         int tail;
195
196         mutex_lock(&chip->logbuffer_lock);
197         tail = chip->logbuffer_tail;
198         while (tail != chip->logbuffer_head) {
199                 seq_printf(s, "%s\n", chip->logbuffer[tail]);
200                 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
201         }
202         if (!seq_has_overflowed(s))
203                 chip->logbuffer_tail = tail;
204         mutex_unlock(&chip->logbuffer_lock);
205
206         return 0;
207 }
208 DEFINE_SHOW_ATTRIBUTE(fusb302_debug);
209
210 static void fusb302_debugfs_init(struct fusb302_chip *chip)
211 {
212         char name[NAME_MAX];
213
214         mutex_init(&chip->logbuffer_lock);
215         snprintf(name, NAME_MAX, "fusb302-%s", dev_name(chip->dev));
216         chip->dentry = debugfs_create_file(name, S_IFREG | 0444, usb_debug_root,
217                                            chip, &fusb302_debug_fops);
218 }
219
220 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
221 {
222         debugfs_remove(chip->dentry);
223 }
224
225 #else
226
227 static void fusb302_log(const struct fusb302_chip *chip,
228                         const char *fmt, ...) { }
229 static void fusb302_debugfs_init(const struct fusb302_chip *chip) { }
230 static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
231
232 #endif
233
234 static int fusb302_i2c_write(struct fusb302_chip *chip,
235                              u8 address, u8 data)
236 {
237         int ret = 0;
238
239         ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
240         if (ret < 0)
241                 fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
242                             data, address, ret);
243
244         return ret;
245 }
246
247 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
248                                    u8 length, const u8 *data)
249 {
250         int ret = 0;
251
252         if (length <= 0)
253                 return ret;
254
255         ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
256                                              length, data);
257         if (ret < 0)
258                 fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
259                             address, length, ret);
260
261         return ret;
262 }
263
264 static int fusb302_i2c_read(struct fusb302_chip *chip,
265                             u8 address, u8 *data)
266 {
267         int ret = 0;
268
269         ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
270         *data = (u8)ret;
271         if (ret < 0)
272                 fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
273
274         return ret;
275 }
276
277 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
278                                   u8 length, u8 *data)
279 {
280         int ret = 0;
281
282         if (length <= 0)
283                 return ret;
284
285         ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
286                                             length, data);
287         if (ret < 0) {
288                 fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
289                             address, length, ret);
290                 goto done;
291         }
292         if (ret != length) {
293                 fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
294                             ret, length, address);
295                 ret = -EIO;
296         }
297
298 done:
299         return ret;
300 }
301
302 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
303                                   u8 mask, u8 value)
304 {
305         int ret = 0;
306         u8 data;
307
308         ret = fusb302_i2c_read(chip, address, &data);
309         if (ret < 0)
310                 return ret;
311         data &= ~mask;
312         data |= value;
313         ret = fusb302_i2c_write(chip, address, data);
314         if (ret < 0)
315                 return ret;
316
317         return ret;
318 }
319
320 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
321                                 u8 set_bits)
322 {
323         return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
324 }
325
326 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
327                                   u8 clear_bits)
328 {
329         return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
330 }
331
332 static int fusb302_sw_reset(struct fusb302_chip *chip)
333 {
334         int ret = 0;
335
336         ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
337                                 FUSB_REG_RESET_SW_RESET);
338         if (ret < 0)
339                 fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
340         else
341                 fusb302_log(chip, "sw reset");
342
343         return ret;
344 }
345
346 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip)
347 {
348         int ret = 0;
349
350         ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
351                                    FUSB_REG_CONTROL3_N_RETRIES_3 |
352                                    FUSB_REG_CONTROL3_AUTO_RETRY);
353
354         return ret;
355 }
356
357 /*
358  * initialize interrupt on the chip
359  * - unmasked interrupt: VBUS_OK
360  */
361 static int fusb302_init_interrupt(struct fusb302_chip *chip)
362 {
363         int ret = 0;
364
365         ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
366                                 0xFF & ~FUSB_REG_MASK_VBUSOK);
367         if (ret < 0)
368                 return ret;
369         ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
370         if (ret < 0)
371                 return ret;
372         ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
373         if (ret < 0)
374                 return ret;
375         ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
376                                      FUSB_REG_CONTROL0_INT_MASK);
377         if (ret < 0)
378                 return ret;
379
380         return ret;
381 }
382
383 static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
384 {
385         int ret = 0;
386
387         ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
388
389         return ret;
390 }
391
392 static int tcpm_init(struct tcpc_dev *dev)
393 {
394         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
395                                                  tcpc_dev);
396         int ret = 0;
397         u8 data;
398
399         ret = fusb302_sw_reset(chip);
400         if (ret < 0)
401                 return ret;
402         ret = fusb302_enable_tx_auto_retries(chip);
403         if (ret < 0)
404                 return ret;
405         ret = fusb302_init_interrupt(chip);
406         if (ret < 0)
407                 return ret;
408         ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
409         if (ret < 0)
410                 return ret;
411         ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
412         if (ret < 0)
413                 return ret;
414         chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
415         ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
416         if (ret < 0)
417                 return ret;
418         fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
419
420         return ret;
421 }
422
423 static int tcpm_get_vbus(struct tcpc_dev *dev)
424 {
425         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
426                                                  tcpc_dev);
427         int ret = 0;
428
429         mutex_lock(&chip->lock);
430         ret = chip->vbus_present ? 1 : 0;
431         mutex_unlock(&chip->lock);
432
433         return ret;
434 }
435
436 static int tcpm_get_current_limit(struct tcpc_dev *dev)
437 {
438         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
439                                                  tcpc_dev);
440         int current_limit = 0;
441         unsigned long timeout;
442
443         if (!chip->extcon)
444                 return 0;
445
446         /*
447          * USB2 Charger detection may still be in progress when we get here,
448          * this can take upto 600ms, wait 800ms max.
449          */
450         timeout = jiffies + msecs_to_jiffies(800);
451         do {
452                 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_SDP) == 1)
453                         current_limit = 500;
454
455                 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_CDP) == 1 ||
456                     extcon_get_state(chip->extcon, EXTCON_CHG_USB_ACA) == 1)
457                         current_limit = 1500;
458
459                 if (extcon_get_state(chip->extcon, EXTCON_CHG_USB_DCP) == 1)
460                         current_limit = 2000;
461
462                 msleep(50);
463         } while (current_limit == 0 && time_before(jiffies, timeout));
464
465         return current_limit;
466 }
467
468 static int fusb302_set_src_current(struct fusb302_chip *chip,
469                                    enum src_current_status status)
470 {
471         int ret = 0;
472
473         chip->src_current_status = status;
474         switch (status) {
475         case SRC_CURRENT_DEFAULT:
476                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
477                                              FUSB_REG_CONTROL0_HOST_CUR_MASK,
478                                              FUSB_REG_CONTROL0_HOST_CUR_DEF);
479                 break;
480         case SRC_CURRENT_MEDIUM:
481                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
482                                              FUSB_REG_CONTROL0_HOST_CUR_MASK,
483                                              FUSB_REG_CONTROL0_HOST_CUR_MED);
484                 break;
485         case SRC_CURRENT_HIGH:
486                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
487                                              FUSB_REG_CONTROL0_HOST_CUR_MASK,
488                                              FUSB_REG_CONTROL0_HOST_CUR_HIGH);
489                 break;
490         default:
491                 break;
492         }
493
494         return ret;
495 }
496
497 static int fusb302_set_toggling(struct fusb302_chip *chip,
498                                 enum toggling_mode mode)
499 {
500         int ret = 0;
501
502         /* first disable toggling */
503         ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
504                                      FUSB_REG_CONTROL2_TOGGLE);
505         if (ret < 0)
506                 return ret;
507         /* mask interrupts for SRC or SNK */
508         ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
509                                    FUSB_REG_MASK_BC_LVL |
510                                    FUSB_REG_MASK_COMP_CHNG);
511         if (ret < 0)
512                 return ret;
513         chip->intr_bc_lvl = false;
514         chip->intr_comp_chng = false;
515         /* configure toggling mode: none/snk/src/drp */
516         switch (mode) {
517         case TOGGLING_MODE_OFF:
518                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
519                                              FUSB_REG_CONTROL2_MODE_MASK,
520                                              FUSB_REG_CONTROL2_MODE_NONE);
521                 if (ret < 0)
522                         return ret;
523                 break;
524         case TOGGLING_MODE_SNK:
525                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
526                                              FUSB_REG_CONTROL2_MODE_MASK,
527                                              FUSB_REG_CONTROL2_MODE_UFP);
528                 if (ret < 0)
529                         return ret;
530                 break;
531         case TOGGLING_MODE_SRC:
532                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
533                                              FUSB_REG_CONTROL2_MODE_MASK,
534                                              FUSB_REG_CONTROL2_MODE_DFP);
535                 if (ret < 0)
536                         return ret;
537                 break;
538         case TOGGLING_MODE_DRP:
539                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
540                                              FUSB_REG_CONTROL2_MODE_MASK,
541                                              FUSB_REG_CONTROL2_MODE_DRP);
542                 if (ret < 0)
543                         return ret;
544                 break;
545         default:
546                 break;
547         }
548
549         if (mode == TOGGLING_MODE_OFF) {
550                 /* mask TOGDONE interrupt */
551                 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
552                                            FUSB_REG_MASKA_TOGDONE);
553                 if (ret < 0)
554                         return ret;
555                 chip->intr_togdone = false;
556         } else {
557                 /* Datasheet says vconn MUST be off when toggling */
558                 WARN(chip->vconn_on, "Vconn is on during toggle start");
559                 /* unmask TOGDONE interrupt */
560                 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
561                                              FUSB_REG_MASKA_TOGDONE);
562                 if (ret < 0)
563                         return ret;
564                 chip->intr_togdone = true;
565                 /* start toggling */
566                 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
567                                            FUSB_REG_CONTROL2_TOGGLE);
568                 if (ret < 0)
569                         return ret;
570                 /* during toggling, consider cc as Open */
571                 chip->cc1 = TYPEC_CC_OPEN;
572                 chip->cc2 = TYPEC_CC_OPEN;
573         }
574         chip->toggling_mode = mode;
575
576         return ret;
577 }
578
579 static const char * const typec_cc_status_name[] = {
580         [TYPEC_CC_OPEN]         = "Open",
581         [TYPEC_CC_RA]           = "Ra",
582         [TYPEC_CC_RD]           = "Rd",
583         [TYPEC_CC_RP_DEF]       = "Rp-def",
584         [TYPEC_CC_RP_1_5]       = "Rp-1.5",
585         [TYPEC_CC_RP_3_0]       = "Rp-3.0",
586 };
587
588 static const enum src_current_status cc_src_current[] = {
589         [TYPEC_CC_OPEN]         = SRC_CURRENT_DEFAULT,
590         [TYPEC_CC_RA]           = SRC_CURRENT_DEFAULT,
591         [TYPEC_CC_RD]           = SRC_CURRENT_DEFAULT,
592         [TYPEC_CC_RP_DEF]       = SRC_CURRENT_DEFAULT,
593         [TYPEC_CC_RP_1_5]       = SRC_CURRENT_MEDIUM,
594         [TYPEC_CC_RP_3_0]       = SRC_CURRENT_HIGH,
595 };
596
597 static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
598 {
599         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
600                                                  tcpc_dev);
601         u8 switches0_mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
602                             FUSB_REG_SWITCHES0_CC2_PU_EN |
603                             FUSB_REG_SWITCHES0_CC1_PD_EN |
604                             FUSB_REG_SWITCHES0_CC2_PD_EN;
605         u8 rd_mda, switches0_data = 0x00;
606         int ret = 0;
607
608         mutex_lock(&chip->lock);
609         switch (cc) {
610         case TYPEC_CC_OPEN:
611                 break;
612         case TYPEC_CC_RD:
613                 switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
614                                   FUSB_REG_SWITCHES0_CC2_PD_EN;
615                 break;
616         case TYPEC_CC_RP_DEF:
617         case TYPEC_CC_RP_1_5:
618         case TYPEC_CC_RP_3_0:
619                 switches0_data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
620                                   FUSB_REG_SWITCHES0_CC1_PU_EN :
621                                   FUSB_REG_SWITCHES0_CC2_PU_EN;
622                 break;
623         default:
624                 fusb302_log(chip, "unsupported cc value %s",
625                             typec_cc_status_name[cc]);
626                 ret = -EINVAL;
627                 goto done;
628         }
629
630         fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
631
632         ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
633         if (ret < 0) {
634                 fusb302_log(chip, "cannot set toggling mode, ret=%d", ret);
635                 goto done;
636         }
637
638         ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
639                                      switches0_mask, switches0_data);
640         if (ret < 0) {
641                 fusb302_log(chip, "cannot set pull-up/-down, ret = %d", ret);
642                 goto done;
643         }
644         /* reset the cc status */
645         chip->cc1 = TYPEC_CC_OPEN;
646         chip->cc2 = TYPEC_CC_OPEN;
647
648         /* adjust current for SRC */
649         ret = fusb302_set_src_current(chip, cc_src_current[cc]);
650         if (ret < 0) {
651                 fusb302_log(chip, "cannot set src current %s, ret=%d",
652                             typec_cc_status_name[cc], ret);
653                 goto done;
654         }
655
656         /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
657         switch (cc) {
658         case TYPEC_CC_RP_DEF:
659         case TYPEC_CC_RP_1_5:
660         case TYPEC_CC_RP_3_0:
661                 rd_mda = rd_mda_value[cc_src_current[cc]];
662                 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
663                 if (ret < 0) {
664                         fusb302_log(chip,
665                                     "cannot set SRC measure value, ret=%d",
666                                     ret);
667                         goto done;
668                 }
669                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
670                                              FUSB_REG_MASK_BC_LVL |
671                                              FUSB_REG_MASK_COMP_CHNG,
672                                              FUSB_REG_MASK_BC_LVL);
673                 if (ret < 0) {
674                         fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
675                                     ret);
676                         goto done;
677                 }
678                 chip->intr_comp_chng = true;
679                 chip->intr_bc_lvl = false;
680                 break;
681         case TYPEC_CC_RD:
682                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
683                                              FUSB_REG_MASK_BC_LVL |
684                                              FUSB_REG_MASK_COMP_CHNG,
685                                              FUSB_REG_MASK_COMP_CHNG);
686                 if (ret < 0) {
687                         fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
688                                     ret);
689                         goto done;
690                 }
691                 chip->intr_bc_lvl = true;
692                 chip->intr_comp_chng = false;
693                 break;
694         default:
695                 break;
696         }
697 done:
698         mutex_unlock(&chip->lock);
699
700         return ret;
701 }
702
703 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
704                        enum typec_cc_status *cc2)
705 {
706         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
707                                                  tcpc_dev);
708
709         mutex_lock(&chip->lock);
710         *cc1 = chip->cc1;
711         *cc2 = chip->cc2;
712         fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
713                     typec_cc_status_name[*cc2]);
714         mutex_unlock(&chip->lock);
715
716         return 0;
717 }
718
719 static int tcpm_set_polarity(struct tcpc_dev *dev,
720                              enum typec_cc_polarity polarity)
721 {
722         return 0;
723 }
724
725 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
726 {
727         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
728                                                  tcpc_dev);
729         int ret = 0;
730         u8 switches0_data = 0x00;
731         u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
732                             FUSB_REG_SWITCHES0_VCONN_CC2;
733
734         mutex_lock(&chip->lock);
735         if (chip->vconn_on == on) {
736                 fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
737                 goto done;
738         }
739         if (on) {
740                 switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
741                                  FUSB_REG_SWITCHES0_VCONN_CC2 :
742                                  FUSB_REG_SWITCHES0_VCONN_CC1;
743         }
744         ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
745                                      switches0_mask, switches0_data);
746         if (ret < 0)
747                 goto done;
748         chip->vconn_on = on;
749         fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
750 done:
751         mutex_unlock(&chip->lock);
752
753         return ret;
754 }
755
756 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
757 {
758         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
759                                                  tcpc_dev);
760         int ret = 0;
761
762         mutex_lock(&chip->lock);
763         if (chip->vbus_on == on) {
764                 fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
765         } else {
766                 if (on)
767                         ret = regulator_enable(chip->vbus);
768                 else
769                         ret = regulator_disable(chip->vbus);
770                 if (ret < 0) {
771                         fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
772                                     on ? "enable" : "disable", ret);
773                         goto done;
774                 }
775                 chip->vbus_on = on;
776                 fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
777         }
778         if (chip->charge_on == charge)
779                 fusb302_log(chip, "charge is already %s",
780                             charge ? "On" : "Off");
781         else
782                 chip->charge_on = charge;
783
784 done:
785         mutex_unlock(&chip->lock);
786
787         return ret;
788 }
789
790 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
791 {
792         return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
793                                     FUSB_REG_CONTROL0_TX_FLUSH);
794 }
795
796 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
797 {
798         return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
799                                     FUSB_REG_CONTROL1_RX_FLUSH);
800 }
801
802 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
803 {
804         if (on)
805                 return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
806                                             FUSB_REG_SWITCHES1_AUTO_GCRC);
807         return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
808                                             FUSB_REG_SWITCHES1_AUTO_GCRC);
809 }
810
811 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
812 {
813         int ret = 0;
814         u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
815         u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
816                               FUSB_REG_MASKA_HARDSENT |
817                               FUSB_REG_MASKA_TX_SUCCESS |
818                               FUSB_REG_MASKA_HARDRESET;
819         u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
820
821         ret = on ?
822                 fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
823                 fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
824         if (ret < 0)
825                 return ret;
826         ret = on ?
827                 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
828                 fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
829         if (ret < 0)
830                 return ret;
831         ret = on ?
832                 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
833                 fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
834         return ret;
835 }
836
837 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
838 {
839         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
840                                                  tcpc_dev);
841         int ret = 0;
842
843         mutex_lock(&chip->lock);
844         ret = fusb302_pd_rx_flush(chip);
845         if (ret < 0) {
846                 fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
847                 goto done;
848         }
849         ret = fusb302_pd_tx_flush(chip);
850         if (ret < 0) {
851                 fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
852                 goto done;
853         }
854         ret = fusb302_pd_set_auto_goodcrc(chip, on);
855         if (ret < 0) {
856                 fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
857                             on ? "on" : "off", ret);
858                 goto done;
859         }
860         ret = fusb302_pd_set_interrupts(chip, on);
861         if (ret < 0) {
862                 fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
863                             on ? "on" : "off", ret);
864                 goto done;
865         }
866         fusb302_log(chip, "pd := %s", on ? "on" : "off");
867 done:
868         mutex_unlock(&chip->lock);
869
870         return ret;
871 }
872
873 static const char * const typec_role_name[] = {
874         [TYPEC_SINK]            = "Sink",
875         [TYPEC_SOURCE]          = "Source",
876 };
877
878 static const char * const typec_data_role_name[] = {
879         [TYPEC_DEVICE]          = "Device",
880         [TYPEC_HOST]            = "Host",
881 };
882
883 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
884                           enum typec_role pwr, enum typec_data_role data)
885 {
886         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
887                                                  tcpc_dev);
888         int ret = 0;
889         u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
890                             FUSB_REG_SWITCHES1_DATAROLE;
891         u8 switches1_data = 0x00;
892
893         mutex_lock(&chip->lock);
894         if (pwr == TYPEC_SOURCE)
895                 switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
896         if (data == TYPEC_HOST)
897                 switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
898         ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
899                                      switches1_mask, switches1_data);
900         if (ret < 0) {
901                 fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
902                             typec_role_name[pwr], typec_data_role_name[data],
903                             ret);
904                 goto done;
905         }
906         fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
907                     typec_data_role_name[data]);
908 done:
909         mutex_unlock(&chip->lock);
910
911         return ret;
912 }
913
914 static int tcpm_start_toggling(struct tcpc_dev *dev,
915                                enum typec_port_type port_type,
916                                enum typec_cc_status cc)
917 {
918         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
919                                                  tcpc_dev);
920         enum toggling_mode mode = TOGGLING_MODE_OFF;
921         int ret = 0;
922
923         switch (port_type) {
924         case TYPEC_PORT_SRC:
925                 mode = TOGGLING_MODE_SRC;
926                 break;
927         case TYPEC_PORT_SNK:
928                 mode = TOGGLING_MODE_SNK;
929                 break;
930         case TYPEC_PORT_DRP:
931                 mode = TOGGLING_MODE_DRP;
932                 break;
933         }
934
935         mutex_lock(&chip->lock);
936         ret = fusb302_set_src_current(chip, cc_src_current[cc]);
937         if (ret < 0) {
938                 fusb302_log(chip, "unable to set src current %s, ret=%d",
939                             typec_cc_status_name[cc], ret);
940                 goto done;
941         }
942         ret = fusb302_set_toggling(chip, mode);
943         if (ret < 0) {
944                 fusb302_log(chip,
945                             "unable to start drp toggling, ret=%d", ret);
946                 goto done;
947         }
948         fusb302_log(chip, "start drp toggling");
949 done:
950         mutex_unlock(&chip->lock);
951
952         return ret;
953 }
954
955 static int fusb302_pd_send_message(struct fusb302_chip *chip,
956                                    const struct pd_message *msg)
957 {
958         int ret = 0;
959         u8 buf[40];
960         u8 pos = 0;
961         int len;
962
963         /* SOP tokens */
964         buf[pos++] = FUSB302_TKN_SYNC1;
965         buf[pos++] = FUSB302_TKN_SYNC1;
966         buf[pos++] = FUSB302_TKN_SYNC1;
967         buf[pos++] = FUSB302_TKN_SYNC2;
968
969         len = pd_header_cnt_le(msg->header) * 4;
970         /* plug 2 for header */
971         len += 2;
972         if (len > 0x1F) {
973                 fusb302_log(chip,
974                             "PD message too long %d (incl. header)", len);
975                 return -EINVAL;
976         }
977         /* packsym tells the FUSB302 chip that the next X bytes are payload */
978         buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
979         memcpy(&buf[pos], &msg->header, sizeof(msg->header));
980         pos += sizeof(msg->header);
981
982         len -= 2;
983         memcpy(&buf[pos], msg->payload, len);
984         pos += len;
985
986         /* CRC */
987         buf[pos++] = FUSB302_TKN_JAMCRC;
988         /* EOP */
989         buf[pos++] = FUSB302_TKN_EOP;
990         /* turn tx off after sending message */
991         buf[pos++] = FUSB302_TKN_TXOFF;
992         /* start transmission */
993         buf[pos++] = FUSB302_TKN_TXON;
994
995         ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
996         if (ret < 0)
997                 return ret;
998         fusb302_log(chip, "sending PD message header: %x", msg->header);
999         fusb302_log(chip, "sending PD message len: %d", len);
1000
1001         return ret;
1002 }
1003
1004 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1005 {
1006         return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1007                                     FUSB_REG_CONTROL3_SEND_HARDRESET);
1008 }
1009
1010 static const char * const transmit_type_name[] = {
1011         [TCPC_TX_SOP]                   = "SOP",
1012         [TCPC_TX_SOP_PRIME]             = "SOP'",
1013         [TCPC_TX_SOP_PRIME_PRIME]       = "SOP''",
1014         [TCPC_TX_SOP_DEBUG_PRIME]       = "DEBUG'",
1015         [TCPC_TX_SOP_DEBUG_PRIME_PRIME] = "DEBUG''",
1016         [TCPC_TX_HARD_RESET]            = "HARD_RESET",
1017         [TCPC_TX_CABLE_RESET]           = "CABLE_RESET",
1018         [TCPC_TX_BIST_MODE_2]           = "BIST_MODE_2",
1019 };
1020
1021 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1022                             const struct pd_message *msg)
1023 {
1024         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1025                                                  tcpc_dev);
1026         int ret = 0;
1027
1028         mutex_lock(&chip->lock);
1029         switch (type) {
1030         case TCPC_TX_SOP:
1031                 ret = fusb302_pd_send_message(chip, msg);
1032                 if (ret < 0)
1033                         fusb302_log(chip,
1034                                     "cannot send PD message, ret=%d", ret);
1035                 break;
1036         case TCPC_TX_HARD_RESET:
1037                 ret = fusb302_pd_send_hardreset(chip);
1038                 if (ret < 0)
1039                         fusb302_log(chip,
1040                                     "cannot send hardreset, ret=%d", ret);
1041                 break;
1042         default:
1043                 fusb302_log(chip, "type %s not supported",
1044                             transmit_type_name[type]);
1045                 ret = -EINVAL;
1046         }
1047         mutex_unlock(&chip->lock);
1048
1049         return ret;
1050 }
1051
1052 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1053 {
1054         if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1055                 return TYPEC_CC_RP_3_0;
1056         if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1057                 return TYPEC_CC_RP_1_5;
1058         if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1059                 return TYPEC_CC_RP_DEF;
1060         return TYPEC_CC_OPEN;
1061 }
1062
1063 static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1064 {
1065         struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1066                                                  bc_lvl_handler.work);
1067         int ret = 0;
1068         u8 status0;
1069         u8 bc_lvl;
1070         enum typec_cc_status cc_status;
1071
1072         mutex_lock(&chip->lock);
1073         if (!chip->intr_bc_lvl) {
1074                 fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1075                 goto done;
1076         }
1077         ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1078         if (ret < 0)
1079                 goto done;
1080         fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1081         if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1082                 fusb302_log(chip, "CC activities detected, delay handling");
1083                 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1084                                  msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1085                 goto done;
1086         }
1087         bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1088         cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1089         if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1090                 if (chip->cc1 != cc_status) {
1091                         fusb302_log(chip, "cc1: %s -> %s",
1092                                     typec_cc_status_name[chip->cc1],
1093                                     typec_cc_status_name[cc_status]);
1094                         chip->cc1 = cc_status;
1095                         tcpm_cc_change(chip->tcpm_port);
1096                 }
1097         } else {
1098                 if (chip->cc2 != cc_status) {
1099                         fusb302_log(chip, "cc2: %s -> %s",
1100                                     typec_cc_status_name[chip->cc2],
1101                                     typec_cc_status_name[cc_status]);
1102                         chip->cc2 = cc_status;
1103                         tcpm_cc_change(chip->tcpm_port);
1104                 }
1105         }
1106
1107 done:
1108         mutex_unlock(&chip->lock);
1109 }
1110
1111 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1112 {
1113         fusb302_tcpc_dev->init = tcpm_init;
1114         fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1115         fusb302_tcpc_dev->get_current_limit = tcpm_get_current_limit;
1116         fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1117         fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1118         fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1119         fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1120         fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1121         fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1122         fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1123         fusb302_tcpc_dev->start_toggling = tcpm_start_toggling;
1124         fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1125 }
1126
1127 static const char * const cc_polarity_name[] = {
1128         [TYPEC_POLARITY_CC1]    = "Polarity_CC1",
1129         [TYPEC_POLARITY_CC2]    = "Polarity_CC2",
1130 };
1131
1132 static int fusb302_set_cc_polarity_and_pull(struct fusb302_chip *chip,
1133                                             enum typec_cc_polarity cc_polarity,
1134                                             bool pull_up, bool pull_down)
1135 {
1136         int ret = 0;
1137         u8 switches0_data = 0x00;
1138         u8 switches1_mask = FUSB_REG_SWITCHES1_TXCC1_EN |
1139                             FUSB_REG_SWITCHES1_TXCC2_EN;
1140         u8 switches1_data = 0x00;
1141
1142         if (pull_down)
1143                 switches0_data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
1144                                   FUSB_REG_SWITCHES0_CC2_PD_EN;
1145
1146         if (cc_polarity == TYPEC_POLARITY_CC1) {
1147                 switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC1;
1148                 if (chip->vconn_on)
1149                         switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC2;
1150                 if (pull_up)
1151                         switches0_data |= FUSB_REG_SWITCHES0_CC1_PU_EN;
1152                 switches1_data = FUSB_REG_SWITCHES1_TXCC1_EN;
1153         } else {
1154                 switches0_data |= FUSB_REG_SWITCHES0_MEAS_CC2;
1155                 if (chip->vconn_on)
1156                         switches0_data |= FUSB_REG_SWITCHES0_VCONN_CC1;
1157                 if (pull_up)
1158                         switches0_data |= FUSB_REG_SWITCHES0_CC2_PU_EN;
1159                 switches1_data = FUSB_REG_SWITCHES1_TXCC2_EN;
1160         }
1161         ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1162         if (ret < 0)
1163                 return ret;
1164         ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
1165                                      switches1_mask, switches1_data);
1166         if (ret < 0)
1167                 return ret;
1168         chip->cc_polarity = cc_polarity;
1169
1170         return ret;
1171 }
1172
1173 static int fusb302_handle_togdone_snk(struct fusb302_chip *chip,
1174                                       u8 togdone_result)
1175 {
1176         int ret = 0;
1177         u8 status0;
1178         u8 bc_lvl;
1179         enum typec_cc_polarity cc_polarity;
1180         enum typec_cc_status cc_status_active, cc1, cc2;
1181
1182         /* set polarity and pull_up, pull_down */
1183         cc_polarity = (togdone_result == FUSB_REG_STATUS1A_TOGSS_SNK1) ?
1184                       TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
1185         ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, false, true);
1186         if (ret < 0) {
1187                 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1188                             cc_polarity_name[cc_polarity], ret);
1189                 return ret;
1190         }
1191         /* fusb302_set_cc_polarity() has set the correct measure block */
1192         ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1193         if (ret < 0)
1194                 return ret;
1195         bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1196         cc_status_active = fusb302_bc_lvl_to_cc(bc_lvl);
1197         /* restart toggling if the cc status on the active line is OPEN */
1198         if (cc_status_active == TYPEC_CC_OPEN) {
1199                 fusb302_log(chip, "restart toggling as CC_OPEN detected");
1200                 ret = fusb302_set_toggling(chip, chip->toggling_mode);
1201                 return ret;
1202         }
1203         /* update tcpm with the new cc value */
1204         cc1 = (cc_polarity == TYPEC_POLARITY_CC1) ?
1205               cc_status_active : TYPEC_CC_OPEN;
1206         cc2 = (cc_polarity == TYPEC_POLARITY_CC2) ?
1207               cc_status_active : TYPEC_CC_OPEN;
1208         if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1209                 chip->cc1 = cc1;
1210                 chip->cc2 = cc2;
1211                 tcpm_cc_change(chip->tcpm_port);
1212         }
1213         /* turn off toggling */
1214         ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1215         if (ret < 0) {
1216                 fusb302_log(chip,
1217                             "cannot set toggling mode off, ret=%d", ret);
1218                 return ret;
1219         }
1220         /* unmask bc_lvl interrupt */
1221         ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, FUSB_REG_MASK_BC_LVL);
1222         if (ret < 0) {
1223                 fusb302_log(chip,
1224                             "cannot unmask bc_lcl interrupt, ret=%d", ret);
1225                 return ret;
1226         }
1227         chip->intr_bc_lvl = true;
1228         fusb302_log(chip, "detected cc1=%s, cc2=%s",
1229                     typec_cc_status_name[cc1],
1230                     typec_cc_status_name[cc2]);
1231
1232         return ret;
1233 }
1234
1235 /* On error returns < 0, otherwise a typec_cc_status value */
1236 static int fusb302_get_src_cc_status(struct fusb302_chip *chip,
1237                                      enum typec_cc_polarity cc_polarity,
1238                                      enum typec_cc_status *cc)
1239 {
1240         u8 ra_mda = ra_mda_value[chip->src_current_status];
1241         u8 rd_mda = rd_mda_value[chip->src_current_status];
1242         u8 switches0_data, status0;
1243         int ret;
1244
1245         /* Step 1: Set switches so that we measure the right CC pin */
1246         switches0_data = (cc_polarity == TYPEC_POLARITY_CC1) ?
1247                 FUSB_REG_SWITCHES0_CC1_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC1 :
1248                 FUSB_REG_SWITCHES0_CC2_PU_EN | FUSB_REG_SWITCHES0_MEAS_CC2;
1249         ret = fusb302_i2c_write(chip, FUSB_REG_SWITCHES0, switches0_data);
1250         if (ret < 0)
1251                 return ret;
1252
1253         fusb302_i2c_read(chip, FUSB_REG_SWITCHES0, &status0);
1254         fusb302_log(chip, "get_src_cc_status switches: 0x%0x", status0);
1255
1256         /* Step 2: Set compararator volt to differentiate between Open and Rd */
1257         ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1258         if (ret < 0)
1259                 return ret;
1260
1261         usleep_range(50, 100);
1262         ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1263         if (ret < 0)
1264                 return ret;
1265
1266         fusb302_log(chip, "get_src_cc_status rd_mda status0: 0x%0x", status0);
1267         if (status0 & FUSB_REG_STATUS0_COMP) {
1268                 *cc = TYPEC_CC_OPEN;
1269                 return 0;
1270         }
1271
1272         /* Step 3: Set compararator input to differentiate between Rd and Ra. */
1273         ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, ra_mda);
1274         if (ret < 0)
1275                 return ret;
1276
1277         usleep_range(50, 100);
1278         ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1279         if (ret < 0)
1280                 return ret;
1281
1282         fusb302_log(chip, "get_src_cc_status ra_mda status0: 0x%0x", status0);
1283         if (status0 & FUSB_REG_STATUS0_COMP)
1284                 *cc = TYPEC_CC_RD;
1285         else
1286                 *cc = TYPEC_CC_RA;
1287
1288         return 0;
1289 }
1290
1291 static int fusb302_handle_togdone_src(struct fusb302_chip *chip,
1292                                       u8 togdone_result)
1293 {
1294         /*
1295          * - set polarity (measure cc, vconn, tx)
1296          * - set pull_up, pull_down
1297          * - set cc1, cc2, and update to tcpm_port
1298          * - set I_COMP interrupt on
1299          */
1300         int ret = 0;
1301         u8 rd_mda = rd_mda_value[chip->src_current_status];
1302         enum toggling_mode toggling_mode = chip->toggling_mode;
1303         enum typec_cc_polarity cc_polarity;
1304         enum typec_cc_status cc1, cc2;
1305
1306         /*
1307          * The toggle-engine will stop in a src state if it sees either Ra or
1308          * Rd. Determine the status for both CC pins, starting with the one
1309          * where toggling stopped, as that is where the switches point now.
1310          */
1311         if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1312                 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1313         else
1314                 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1315         if (ret < 0)
1316                 return ret;
1317         /* we must turn off toggling before we can measure the other pin */
1318         ret = fusb302_set_toggling(chip, TOGGLING_MODE_OFF);
1319         if (ret < 0) {
1320                 fusb302_log(chip, "cannot set toggling mode off, ret=%d", ret);
1321                 return ret;
1322         }
1323         /* get the status of the other pin */
1324         if (togdone_result == FUSB_REG_STATUS1A_TOGSS_SRC1)
1325                 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC2, &cc2);
1326         else
1327                 ret = fusb302_get_src_cc_status(chip, TYPEC_POLARITY_CC1, &cc1);
1328         if (ret < 0)
1329                 return ret;
1330
1331         /* determine polarity based on the status of both pins */
1332         if (cc1 == TYPEC_CC_RD &&
1333                         (cc2 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_RA)) {
1334                 cc_polarity = TYPEC_POLARITY_CC1;
1335         } else if (cc2 == TYPEC_CC_RD &&
1336                     (cc1 == TYPEC_CC_OPEN || cc1 == TYPEC_CC_RA)) {
1337                 cc_polarity = TYPEC_POLARITY_CC2;
1338         } else {
1339                 fusb302_log(chip, "unexpected CC status cc1=%s, cc2=%s, restarting toggling",
1340                             typec_cc_status_name[cc1],
1341                             typec_cc_status_name[cc2]);
1342                 return fusb302_set_toggling(chip, toggling_mode);
1343         }
1344         /* set polarity and pull_up, pull_down */
1345         ret = fusb302_set_cc_polarity_and_pull(chip, cc_polarity, true, false);
1346         if (ret < 0) {
1347                 fusb302_log(chip, "cannot set cc polarity %s, ret=%d",
1348                             cc_polarity_name[cc_polarity], ret);
1349                 return ret;
1350         }
1351         /* update tcpm with the new cc value */
1352         if ((chip->cc1 != cc1) || (chip->cc2 != cc2)) {
1353                 chip->cc1 = cc1;
1354                 chip->cc2 = cc2;
1355                 tcpm_cc_change(chip->tcpm_port);
1356         }
1357         /* set MDAC to Rd threshold, and unmask I_COMP for unplug detection */
1358         ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
1359         if (ret < 0)
1360                 return ret;
1361         /* unmask comp_chng interrupt */
1362         ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASK,
1363                                      FUSB_REG_MASK_COMP_CHNG);
1364         if (ret < 0) {
1365                 fusb302_log(chip,
1366                             "cannot unmask comp_chng interrupt, ret=%d", ret);
1367                 return ret;
1368         }
1369         chip->intr_comp_chng = true;
1370         fusb302_log(chip, "detected cc1=%s, cc2=%s",
1371                     typec_cc_status_name[cc1],
1372                     typec_cc_status_name[cc2]);
1373
1374         return ret;
1375 }
1376
1377 static int fusb302_handle_togdone(struct fusb302_chip *chip)
1378 {
1379         int ret = 0;
1380         u8 status1a;
1381         u8 togdone_result;
1382
1383         ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1A, &status1a);
1384         if (ret < 0)
1385                 return ret;
1386         togdone_result = (status1a >> FUSB_REG_STATUS1A_TOGSS_POS) &
1387                          FUSB_REG_STATUS1A_TOGSS_MASK;
1388         switch (togdone_result) {
1389         case FUSB_REG_STATUS1A_TOGSS_SNK1:
1390         case FUSB_REG_STATUS1A_TOGSS_SNK2:
1391                 return fusb302_handle_togdone_snk(chip, togdone_result);
1392         case FUSB_REG_STATUS1A_TOGSS_SRC1:
1393         case FUSB_REG_STATUS1A_TOGSS_SRC2:
1394                 return fusb302_handle_togdone_src(chip, togdone_result);
1395         case FUSB_REG_STATUS1A_TOGSS_AA:
1396                 /* doesn't support */
1397                 fusb302_log(chip, "AudioAccessory not supported");
1398                 fusb302_set_toggling(chip, chip->toggling_mode);
1399                 break;
1400         default:
1401                 fusb302_log(chip, "TOGDONE with an invalid state: %d",
1402                             togdone_result);
1403                 fusb302_set_toggling(chip, chip->toggling_mode);
1404                 break;
1405         }
1406         return ret;
1407 }
1408
1409 static int fusb302_pd_reset(struct fusb302_chip *chip)
1410 {
1411         return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
1412                                     FUSB_REG_RESET_PD_RESET);
1413 }
1414
1415 static int fusb302_pd_read_message(struct fusb302_chip *chip,
1416                                    struct pd_message *msg)
1417 {
1418         int ret = 0;
1419         u8 token;
1420         u8 crc[4];
1421         int len;
1422
1423         /* first SOP token */
1424         ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
1425         if (ret < 0)
1426                 return ret;
1427         ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
1428                                      (u8 *)&msg->header);
1429         if (ret < 0)
1430                 return ret;
1431         len = pd_header_cnt_le(msg->header) * 4;
1432         /* add 4 to length to include the CRC */
1433         if (len > PD_MAX_PAYLOAD * 4) {
1434                 fusb302_log(chip, "PD message too long %d", len);
1435                 return -EINVAL;
1436         }
1437         if (len > 0) {
1438                 ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
1439                                              (u8 *)msg->payload);
1440                 if (ret < 0)
1441                         return ret;
1442         }
1443         /* another 4 bytes to read CRC out */
1444         ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
1445         if (ret < 0)
1446                 return ret;
1447         fusb302_log(chip, "PD message header: %x", msg->header);
1448         fusb302_log(chip, "PD message len: %d", len);
1449
1450         /*
1451          * Check if we've read off a GoodCRC message. If so then indicate to
1452          * TCPM that the previous transmission has completed. Otherwise we pass
1453          * the received message over to TCPM for processing.
1454          *
1455          * We make this check here instead of basing the reporting decision on
1456          * the IRQ event type, as it's possible for the chip to report the
1457          * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
1458          * to check the message type to ensure correct reporting to TCPM.
1459          */
1460         if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
1461                 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1462         else
1463                 tcpm_pd_receive(chip->tcpm_port, msg);
1464
1465         return ret;
1466 }
1467
1468 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1469 {
1470         struct fusb302_chip *chip = dev_id;
1471         unsigned long flags;
1472
1473         /* Disable our level triggered IRQ until our irq_work has cleared it */
1474         disable_irq_nosync(chip->gpio_int_n_irq);
1475
1476         spin_lock_irqsave(&chip->irq_lock, flags);
1477         if (chip->irq_suspended)
1478                 chip->irq_while_suspended = true;
1479         else
1480                 schedule_work(&chip->irq_work);
1481         spin_unlock_irqrestore(&chip->irq_lock, flags);
1482
1483         return IRQ_HANDLED;
1484 }
1485
1486 static void fusb302_irq_work(struct work_struct *work)
1487 {
1488         struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1489                                                  irq_work);
1490         int ret = 0;
1491         u8 interrupt;
1492         u8 interrupta;
1493         u8 interruptb;
1494         u8 status0;
1495         bool vbus_present;
1496         bool comp_result;
1497         bool intr_togdone;
1498         bool intr_bc_lvl;
1499         bool intr_comp_chng;
1500         struct pd_message pd_msg;
1501
1502         mutex_lock(&chip->lock);
1503         /* grab a snapshot of intr flags */
1504         intr_togdone = chip->intr_togdone;
1505         intr_bc_lvl = chip->intr_bc_lvl;
1506         intr_comp_chng = chip->intr_comp_chng;
1507
1508         ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1509         if (ret < 0)
1510                 goto done;
1511         ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1512         if (ret < 0)
1513                 goto done;
1514         ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1515         if (ret < 0)
1516                 goto done;
1517         ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1518         if (ret < 0)
1519                 goto done;
1520         fusb302_log(chip,
1521                     "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1522                     interrupt, interrupta, interruptb, status0);
1523
1524         if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1525                 vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1526                 fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1527                             vbus_present ? "On" : "Off");
1528                 if (vbus_present != chip->vbus_present) {
1529                         chip->vbus_present = vbus_present;
1530                         tcpm_vbus_change(chip->tcpm_port);
1531                 }
1532         }
1533
1534         if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1535                 fusb302_log(chip, "IRQ: TOGDONE");
1536                 ret = fusb302_handle_togdone(chip);
1537                 if (ret < 0) {
1538                         fusb302_log(chip,
1539                                     "handle togdone error, ret=%d", ret);
1540                         goto done;
1541                 }
1542         }
1543
1544         if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1545                 fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1546                 /*
1547                  * as BC_LVL interrupt can be affected by PD activity,
1548                  * apply delay to for the handler to wait for the PD
1549                  * signaling to finish.
1550                  */
1551                 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1552                                  msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1553         }
1554
1555         if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1556                 comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1557                 fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1558                             comp_result ? "true" : "false");
1559                 if (comp_result) {
1560                         /* cc level > Rd_threshold, detach */
1561                         chip->cc1 = TYPEC_CC_OPEN;
1562                         chip->cc2 = TYPEC_CC_OPEN;
1563                         tcpm_cc_change(chip->tcpm_port);
1564                 }
1565         }
1566
1567         if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1568                 fusb302_log(chip, "IRQ: PD collision");
1569                 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1570         }
1571
1572         if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1573                 fusb302_log(chip, "IRQ: PD retry failed");
1574                 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1575         }
1576
1577         if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1578                 fusb302_log(chip, "IRQ: PD hardreset sent");
1579                 ret = fusb302_pd_reset(chip);
1580                 if (ret < 0) {
1581                         fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1582                         goto done;
1583                 }
1584                 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1585         }
1586
1587         if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1588                 fusb302_log(chip, "IRQ: PD tx success");
1589                 ret = fusb302_pd_read_message(chip, &pd_msg);
1590                 if (ret < 0) {
1591                         fusb302_log(chip,
1592                                     "cannot read in PD message, ret=%d", ret);
1593                         goto done;
1594                 }
1595         }
1596
1597         if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1598                 fusb302_log(chip, "IRQ: PD received hardreset");
1599                 ret = fusb302_pd_reset(chip);
1600                 if (ret < 0) {
1601                         fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1602                         goto done;
1603                 }
1604                 tcpm_pd_hard_reset(chip->tcpm_port);
1605         }
1606
1607         if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1608                 fusb302_log(chip, "IRQ: PD sent good CRC");
1609                 ret = fusb302_pd_read_message(chip, &pd_msg);
1610                 if (ret < 0) {
1611                         fusb302_log(chip,
1612                                     "cannot read in PD message, ret=%d", ret);
1613                         goto done;
1614                 }
1615         }
1616 done:
1617         mutex_unlock(&chip->lock);
1618         enable_irq(chip->gpio_int_n_irq);
1619 }
1620
1621 static int init_gpio(struct fusb302_chip *chip)
1622 {
1623         struct device *dev = chip->dev;
1624         int ret = 0;
1625
1626         chip->gpio_int_n = devm_gpiod_get(dev, "fcs,int_n", GPIOD_IN);
1627         if (IS_ERR(chip->gpio_int_n)) {
1628                 dev_err(dev, "failed to request gpio_int_n\n");
1629                 return PTR_ERR(chip->gpio_int_n);
1630         }
1631         ret = gpiod_to_irq(chip->gpio_int_n);
1632         if (ret < 0) {
1633                 dev_err(dev,
1634                         "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1635                 return ret;
1636         }
1637         chip->gpio_int_n_irq = ret;
1638         return 0;
1639 }
1640
1641 #define PDO_FIXED_FLAGS \
1642         (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1643
1644 static const u32 src_pdo[] = {
1645         PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1646 };
1647
1648 static const u32 snk_pdo[] = {
1649         PDO_FIXED(5000, 400, PDO_FIXED_FLAGS)
1650 };
1651
1652 static const struct property_entry port_props[] = {
1653         PROPERTY_ENTRY_STRING("data-role", "dual"),
1654         PROPERTY_ENTRY_STRING("power-role", "dual"),
1655         PROPERTY_ENTRY_STRING("try-power-role", "sink"),
1656         PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
1657         PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
1658         PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
1659         { }
1660 };
1661
1662 static struct fwnode_handle *fusb302_fwnode_get(struct device *dev)
1663 {
1664         struct fwnode_handle *fwnode;
1665
1666         fwnode = device_get_named_child_node(dev, "connector");
1667         if (!fwnode)
1668                 fwnode = fwnode_create_software_node(port_props, NULL);
1669
1670         return fwnode;
1671 }
1672
1673 static int fusb302_probe(struct i2c_client *client,
1674                          const struct i2c_device_id *id)
1675 {
1676         struct fusb302_chip *chip;
1677         struct i2c_adapter *adapter = client->adapter;
1678         struct device *dev = &client->dev;
1679         const char *name;
1680         int ret = 0;
1681
1682         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1683                 dev_err(&client->dev,
1684                         "I2C/SMBus block functionality not supported!\n");
1685                 return -ENODEV;
1686         }
1687         chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1688         if (!chip)
1689                 return -ENOMEM;
1690
1691         chip->i2c_client = client;
1692         chip->dev = &client->dev;
1693         mutex_init(&chip->lock);
1694
1695         /*
1696          * Devicetree platforms should get extcon via phandle (not yet
1697          * supported). On ACPI platforms, we get the name from a device prop.
1698          * This device prop is for kernel internal use only and is expected
1699          * to be set by the platform code which also registers the i2c client
1700          * for the fusb302.
1701          */
1702         if (device_property_read_string(dev, "linux,extcon-name", &name) == 0) {
1703                 chip->extcon = extcon_get_extcon_dev(name);
1704                 if (!chip->extcon)
1705                         return -EPROBE_DEFER;
1706         }
1707
1708         chip->vbus = devm_regulator_get(chip->dev, "vbus");
1709         if (IS_ERR(chip->vbus))
1710                 return PTR_ERR(chip->vbus);
1711
1712         chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1713         if (!chip->wq)
1714                 return -ENOMEM;
1715
1716         spin_lock_init(&chip->irq_lock);
1717         INIT_WORK(&chip->irq_work, fusb302_irq_work);
1718         INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1719         init_tcpc_dev(&chip->tcpc_dev);
1720         fusb302_debugfs_init(chip);
1721
1722         if (client->irq) {
1723                 chip->gpio_int_n_irq = client->irq;
1724         } else {
1725                 ret = init_gpio(chip);
1726                 if (ret < 0)
1727                         goto destroy_workqueue;
1728         }
1729
1730         chip->tcpc_dev.fwnode = fusb302_fwnode_get(dev);
1731         if (IS_ERR(chip->tcpc_dev.fwnode)) {
1732                 ret = PTR_ERR(chip->tcpc_dev.fwnode);
1733                 goto destroy_workqueue;
1734         }
1735
1736         chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1737         if (IS_ERR(chip->tcpm_port)) {
1738                 fwnode_handle_put(chip->tcpc_dev.fwnode);
1739                 ret = PTR_ERR(chip->tcpm_port);
1740                 if (ret != -EPROBE_DEFER)
1741                         dev_err(dev, "cannot register tcpm port, ret=%d", ret);
1742                 goto destroy_workqueue;
1743         }
1744
1745         ret = request_irq(chip->gpio_int_n_irq, fusb302_irq_intn,
1746                           IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1747                           "fsc_interrupt_int_n", chip);
1748         if (ret < 0) {
1749                 dev_err(dev, "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1750                 goto tcpm_unregister_port;
1751         }
1752         enable_irq_wake(chip->gpio_int_n_irq);
1753         i2c_set_clientdata(client, chip);
1754
1755         return ret;
1756
1757 tcpm_unregister_port:
1758         tcpm_unregister_port(chip->tcpm_port);
1759         fwnode_handle_put(chip->tcpc_dev.fwnode);
1760 destroy_workqueue:
1761         fusb302_debugfs_exit(chip);
1762         destroy_workqueue(chip->wq);
1763
1764         return ret;
1765 }
1766
1767 static int fusb302_remove(struct i2c_client *client)
1768 {
1769         struct fusb302_chip *chip = i2c_get_clientdata(client);
1770
1771         disable_irq_wake(chip->gpio_int_n_irq);
1772         free_irq(chip->gpio_int_n_irq, chip);
1773         cancel_work_sync(&chip->irq_work);
1774         cancel_delayed_work_sync(&chip->bc_lvl_handler);
1775         tcpm_unregister_port(chip->tcpm_port);
1776         fwnode_handle_put(chip->tcpc_dev.fwnode);
1777         destroy_workqueue(chip->wq);
1778         fusb302_debugfs_exit(chip);
1779
1780         return 0;
1781 }
1782
1783 static int fusb302_pm_suspend(struct device *dev)
1784 {
1785         struct fusb302_chip *chip = dev->driver_data;
1786         unsigned long flags;
1787
1788         spin_lock_irqsave(&chip->irq_lock, flags);
1789         chip->irq_suspended = true;
1790         spin_unlock_irqrestore(&chip->irq_lock, flags);
1791
1792         /* Make sure any pending irq_work is finished before the bus suspends */
1793         flush_work(&chip->irq_work);
1794         return 0;
1795 }
1796
1797 static int fusb302_pm_resume(struct device *dev)
1798 {
1799         struct fusb302_chip *chip = dev->driver_data;
1800         unsigned long flags;
1801
1802         spin_lock_irqsave(&chip->irq_lock, flags);
1803         if (chip->irq_while_suspended) {
1804                 schedule_work(&chip->irq_work);
1805                 chip->irq_while_suspended = false;
1806         }
1807         chip->irq_suspended = false;
1808         spin_unlock_irqrestore(&chip->irq_lock, flags);
1809
1810         return 0;
1811 }
1812
1813 static const struct of_device_id fusb302_dt_match[] = {
1814         {.compatible = "fcs,fusb302"},
1815         {},
1816 };
1817 MODULE_DEVICE_TABLE(of, fusb302_dt_match);
1818
1819 static const struct i2c_device_id fusb302_i2c_device_id[] = {
1820         {"typec_fusb302", 0},
1821         {},
1822 };
1823 MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
1824
1825 static const struct dev_pm_ops fusb302_pm_ops = {
1826         .suspend = fusb302_pm_suspend,
1827         .resume = fusb302_pm_resume,
1828 };
1829
1830 static struct i2c_driver fusb302_driver = {
1831         .driver = {
1832                    .name = "typec_fusb302",
1833                    .pm = &fusb302_pm_ops,
1834                    .of_match_table = of_match_ptr(fusb302_dt_match),
1835                    },
1836         .probe = fusb302_probe,
1837         .remove = fusb302_remove,
1838         .id_table = fusb302_i2c_device_id,
1839 };
1840 module_i2c_driver(fusb302_driver);
1841
1842 MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1843 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1844 MODULE_LICENSE("GPL");