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