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