]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/typec/fusb302/fusb302.c
Merge tag 'pci-v4.12-fixes-2' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[karo-tx-linux.git] / drivers / staging / typec / fusb302 / fusb302.c
1 /*
2  * Copyright 2016-2017 Google, Inc
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * Fairchild FUSB302 Type-C Chip Driver
15  */
16
17 #include <linux/debugfs.h>
18 #include <linux/delay.h>
19 #include <linux/errno.h>
20 #include <linux/gpio.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/of_device.h>
27 #include <linux/of_device.h>
28 #include <linux/of_gpio.h>
29 #include <linux/pinctrl/consumer.h>
30 #include <linux/proc_fs.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/sched/clock.h>
33 #include <linux/seq_file.h>
34 #include <linux/slab.h>
35 #include <linux/string.h>
36 #include <linux/types.h>
37 #include <linux/usb/typec.h>
38 #include <linux/workqueue.h>
39
40 #include "fusb302_reg.h"
41 #include "../tcpm.h"
42 #include "../pd.h"
43
44 /*
45  * When the device is SNK, BC_LVL interrupt is used to monitor cc pins
46  * for the current capability offered by the SRC. As FUSB302 chip fires
47  * the BC_LVL interrupt on PD signalings, cc lvl should be handled after
48  * a delay to avoid measuring on PD activities. The delay is slightly
49  * longer than PD_T_PD_DEBPUNCE (10-20ms).
50  */
51 #define T_BC_LVL_DEBOUNCE_DELAY_MS 30
52
53 enum toggling_mode {
54         TOGGLINE_MODE_OFF,
55         TOGGLING_MODE_DRP,
56         TOGGLING_MODE_SNK,
57         TOGGLING_MODE_SRC,
58 };
59
60 static const char * const toggling_mode_name[] = {
61         [TOGGLINE_MODE_OFF]     = "toggling_OFF",
62         [TOGGLING_MODE_DRP]     = "toggling_DRP",
63         [TOGGLING_MODE_SNK]     = "toggling_SNK",
64         [TOGGLING_MODE_SRC]     = "toggling_SRC",
65 };
66
67 enum src_current_status {
68         SRC_CURRENT_DEFAULT,
69         SRC_CURRENT_MEDIUM,
70         SRC_CURRENT_HIGH,
71 };
72
73 static const u8 ra_mda_value[] = {
74         [SRC_CURRENT_DEFAULT] = 4,      /* 210mV */
75         [SRC_CURRENT_MEDIUM] = 9,       /* 420mV */
76         [SRC_CURRENT_HIGH] = 18,        /* 798mV */
77 };
78
79 static const u8 rd_mda_value[] = {
80         [SRC_CURRENT_DEFAULT] = 38,     /* 1638mV */
81         [SRC_CURRENT_MEDIUM] = 38,      /* 1638mV */
82         [SRC_CURRENT_HIGH] = 61,        /* 2604mV */
83 };
84
85 #define LOG_BUFFER_ENTRIES      1024
86 #define LOG_BUFFER_ENTRY_SIZE   128
87
88 struct fusb302_chip {
89         struct device *dev;
90         struct i2c_client *i2c_client;
91         struct tcpm_port *tcpm_port;
92         struct tcpc_dev tcpc_dev;
93
94         struct regulator *vbus;
95
96         int gpio_int_n;
97         int gpio_int_n_irq;
98
99         struct workqueue_struct *wq;
100         struct delayed_work bc_lvl_handler;
101
102         atomic_t pm_suspend;
103         atomic_t i2c_busy;
104
105         /* lock for sharing chip states */
106         struct mutex lock;
107
108         /* chip status */
109         enum toggling_mode toggling_mode;
110         enum src_current_status src_current_status;
111         bool intr_togdone;
112         bool intr_bc_lvl;
113         bool intr_comp_chng;
114
115         /* port status */
116         bool pull_up;
117         bool vconn_on;
118         bool vbus_on;
119         bool charge_on;
120         bool vbus_present;
121         enum typec_cc_polarity cc_polarity;
122         enum typec_cc_status cc1;
123         enum typec_cc_status cc2;
124
125 #ifdef CONFIG_DEBUG_FS
126         struct dentry *dentry;
127         /* lock for log buffer access */
128         struct mutex logbuffer_lock;
129         int logbuffer_head;
130         int logbuffer_tail;
131         u8 *logbuffer[LOG_BUFFER_ENTRIES];
132 #endif
133 };
134
135 /*
136  * Logging
137  */
138
139 #ifdef CONFIG_DEBUG_FS
140
141 static bool fusb302_log_full(struct fusb302_chip *chip)
142 {
143         return chip->logbuffer_tail ==
144                 (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
145 }
146
147 static void _fusb302_log(struct fusb302_chip *chip, const char *fmt,
148                          va_list args)
149 {
150         char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
151         u64 ts_nsec = local_clock();
152         unsigned long rem_nsec;
153
154         if (!chip->logbuffer[chip->logbuffer_head]) {
155                 chip->logbuffer[chip->logbuffer_head] =
156                                 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
157                 if (!chip->logbuffer[chip->logbuffer_head])
158                         return;
159         }
160
161         vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
162
163         mutex_lock(&chip->logbuffer_lock);
164
165         if (fusb302_log_full(chip)) {
166                 chip->logbuffer_head = max(chip->logbuffer_head - 1, 0);
167                 strlcpy(tmpbuffer, "overflow", sizeof(tmpbuffer));
168         }
169
170         if (chip->logbuffer_head < 0 ||
171             chip->logbuffer_head >= LOG_BUFFER_ENTRIES) {
172                 dev_warn(chip->dev,
173                          "Bad log buffer index %d\n", chip->logbuffer_head);
174                 goto abort;
175         }
176
177         if (!chip->logbuffer[chip->logbuffer_head]) {
178                 dev_warn(chip->dev,
179                          "Log buffer index %d is NULL\n", chip->logbuffer_head);
180                 goto abort;
181         }
182
183         rem_nsec = do_div(ts_nsec, 1000000000);
184         scnprintf(chip->logbuffer[chip->logbuffer_head],
185                   LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
186                   (unsigned long)ts_nsec, rem_nsec / 1000,
187                   tmpbuffer);
188         chip->logbuffer_head = (chip->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
189
190 abort:
191         mutex_unlock(&chip->logbuffer_lock);
192 }
193
194 static void fusb302_log(struct fusb302_chip *chip, const char *fmt, ...)
195 {
196         va_list args;
197
198         va_start(args, fmt);
199         _fusb302_log(chip, fmt, args);
200         va_end(args);
201 }
202
203 static int fusb302_seq_show(struct seq_file *s, void *v)
204 {
205         struct fusb302_chip *chip = (struct fusb302_chip *)s->private;
206         int tail;
207
208         mutex_lock(&chip->logbuffer_lock);
209         tail = chip->logbuffer_tail;
210         while (tail != chip->logbuffer_head) {
211                 seq_printf(s, "%s\n", chip->logbuffer[tail]);
212                 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
213         }
214         if (!seq_has_overflowed(s))
215                 chip->logbuffer_tail = tail;
216         mutex_unlock(&chip->logbuffer_lock);
217
218         return 0;
219 }
220
221 static int fusb302_debug_open(struct inode *inode, struct file *file)
222 {
223         return single_open(file, fusb302_seq_show, inode->i_private);
224 }
225
226 static const struct file_operations fusb302_debug_operations = {
227         .open           = fusb302_debug_open,
228         .llseek         = seq_lseek,
229         .read           = seq_read,
230         .release        = single_release,
231 };
232
233 static struct dentry *rootdir;
234
235 static int fusb302_debugfs_init(struct fusb302_chip *chip)
236 {
237         mutex_init(&chip->logbuffer_lock);
238         if (!rootdir) {
239                 rootdir = debugfs_create_dir("fusb302", NULL);
240                 if (!rootdir)
241                         return -ENOMEM;
242         }
243
244         chip->dentry = debugfs_create_file(dev_name(chip->dev),
245                                            S_IFREG | 0444, rootdir,
246                                            chip, &fusb302_debug_operations);
247
248         return 0;
249 }
250
251 static void fusb302_debugfs_exit(struct fusb302_chip *chip)
252 {
253         debugfs_remove(chip->dentry);
254 }
255
256 #else
257
258 static void fusb302_log(const struct fusb302_chip *chip,
259                         const char *fmt, ...) { }
260 static int fusb302_debugfs_init(const struct fusb302_chip *chip) { return 0; }
261 static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
262
263 #endif
264
265 #define FUSB302_RESUME_RETRY 10
266 #define FUSB302_RESUME_RETRY_SLEEP 50
267
268 static bool fusb302_is_suspended(struct fusb302_chip *chip)
269 {
270         int retry_cnt;
271
272         for (retry_cnt = 0; retry_cnt < FUSB302_RESUME_RETRY; retry_cnt++) {
273                 if (atomic_read(&chip->pm_suspend)) {
274                         dev_err(chip->dev, "i2c: pm suspend, retry %d/%d\n",
275                                 retry_cnt + 1, FUSB302_RESUME_RETRY);
276                         msleep(FUSB302_RESUME_RETRY_SLEEP);
277                 } else {
278                         return false;
279                 }
280         }
281
282         return true;
283 }
284
285 static int fusb302_i2c_write(struct fusb302_chip *chip,
286                              u8 address, u8 data)
287 {
288         int ret = 0;
289
290         atomic_set(&chip->i2c_busy, 1);
291
292         if (fusb302_is_suspended(chip)) {
293                 atomic_set(&chip->i2c_busy, 0);
294                 return -ETIMEDOUT;
295         }
296
297         ret = i2c_smbus_write_byte_data(chip->i2c_client, address, data);
298         if (ret < 0)
299                 fusb302_log(chip, "cannot write 0x%02x to 0x%02x, ret=%d",
300                             data, address, ret);
301         atomic_set(&chip->i2c_busy, 0);
302
303         return ret;
304 }
305
306 static int fusb302_i2c_block_write(struct fusb302_chip *chip, u8 address,
307                                    u8 length, const u8 *data)
308 {
309         int ret = 0;
310
311         if (length <= 0)
312                 return ret;
313         atomic_set(&chip->i2c_busy, 1);
314
315         if (fusb302_is_suspended(chip)) {
316                 atomic_set(&chip->i2c_busy, 0);
317                 return -ETIMEDOUT;
318         }
319
320         ret = i2c_smbus_write_i2c_block_data(chip->i2c_client, address,
321                                              length, data);
322         if (ret < 0)
323                 fusb302_log(chip, "cannot block write 0x%02x, len=%d, ret=%d",
324                             address, length, ret);
325         atomic_set(&chip->i2c_busy, 0);
326
327         return ret;
328 }
329
330 static int fusb302_i2c_read(struct fusb302_chip *chip,
331                             u8 address, u8 *data)
332 {
333         int ret = 0;
334
335         atomic_set(&chip->i2c_busy, 1);
336
337         if (fusb302_is_suspended(chip)) {
338                 atomic_set(&chip->i2c_busy, 0);
339                 return -ETIMEDOUT;
340         }
341
342         ret = i2c_smbus_read_byte_data(chip->i2c_client, address);
343         *data = (u8)ret;
344         if (ret < 0)
345                 fusb302_log(chip, "cannot read %02x, ret=%d", address, ret);
346         atomic_set(&chip->i2c_busy, 0);
347
348         return ret;
349 }
350
351 static int fusb302_i2c_block_read(struct fusb302_chip *chip, u8 address,
352                                   u8 length, u8 *data)
353 {
354         int ret = 0;
355
356         if (length <= 0)
357                 return ret;
358         atomic_set(&chip->i2c_busy, 1);
359
360         if (fusb302_is_suspended(chip)) {
361                 atomic_set(&chip->i2c_busy, 0);
362                 return -ETIMEDOUT;
363         }
364
365         ret = i2c_smbus_read_i2c_block_data(chip->i2c_client, address,
366                                             length, data);
367         if (ret < 0) {
368                 fusb302_log(chip, "cannot block read 0x%02x, len=%d, ret=%d",
369                             address, length, ret);
370                 goto done;
371         }
372         if (ret != length) {
373                 fusb302_log(chip, "only read %d/%d bytes from 0x%02x",
374                             ret, length, address);
375                 ret = -EIO;
376         }
377
378 done:
379         atomic_set(&chip->i2c_busy, 0);
380
381         return ret;
382 }
383
384 static int fusb302_i2c_mask_write(struct fusb302_chip *chip, u8 address,
385                                   u8 mask, u8 value)
386 {
387         int ret = 0;
388         u8 data;
389
390         ret = fusb302_i2c_read(chip, address, &data);
391         if (ret < 0)
392                 return ret;
393         data &= ~mask;
394         data |= value;
395         ret = fusb302_i2c_write(chip, address, data);
396         if (ret < 0)
397                 return ret;
398
399         return ret;
400 }
401
402 static int fusb302_i2c_set_bits(struct fusb302_chip *chip, u8 address,
403                                 u8 set_bits)
404 {
405         return fusb302_i2c_mask_write(chip, address, 0x00, set_bits);
406 }
407
408 static int fusb302_i2c_clear_bits(struct fusb302_chip *chip, u8 address,
409                                   u8 clear_bits)
410 {
411         return fusb302_i2c_mask_write(chip, address, clear_bits, 0x00);
412 }
413
414 static int fusb302_sw_reset(struct fusb302_chip *chip)
415 {
416         int ret = 0;
417
418         ret = fusb302_i2c_write(chip, FUSB_REG_RESET,
419                                 FUSB_REG_RESET_SW_RESET);
420         if (ret < 0)
421                 fusb302_log(chip, "cannot sw reset the chip, ret=%d", ret);
422         else
423                 fusb302_log(chip, "sw reset");
424
425         return ret;
426 }
427
428 static int fusb302_enable_tx_auto_retries(struct fusb302_chip *chip)
429 {
430         int ret = 0;
431
432         ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
433                                    FUSB_REG_CONTROL3_N_RETRIES_3 |
434                                    FUSB_REG_CONTROL3_AUTO_RETRY);
435
436         return ret;
437 }
438
439 /*
440  * initialize interrupt on the chip
441  * - unmasked interrupt: VBUS_OK
442  */
443 static int fusb302_init_interrupt(struct fusb302_chip *chip)
444 {
445         int ret = 0;
446
447         ret = fusb302_i2c_write(chip, FUSB_REG_MASK,
448                                 0xFF & ~FUSB_REG_MASK_VBUSOK);
449         if (ret < 0)
450                 return ret;
451         ret = fusb302_i2c_write(chip, FUSB_REG_MASKA, 0xFF);
452         if (ret < 0)
453                 return ret;
454         ret = fusb302_i2c_write(chip, FUSB_REG_MASKB, 0xFF);
455         if (ret < 0)
456                 return ret;
457         ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL0,
458                                      FUSB_REG_CONTROL0_INT_MASK);
459         if (ret < 0)
460                 return ret;
461
462         return ret;
463 }
464
465 static int fusb302_set_power_mode(struct fusb302_chip *chip, u8 power_mode)
466 {
467         int ret = 0;
468
469         ret = fusb302_i2c_write(chip, FUSB_REG_POWER, power_mode);
470
471         return ret;
472 }
473
474 static int tcpm_init(struct tcpc_dev *dev)
475 {
476         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
477                                                  tcpc_dev);
478         int ret = 0;
479         u8 data;
480
481         ret = fusb302_sw_reset(chip);
482         if (ret < 0)
483                 return ret;
484         ret = fusb302_enable_tx_auto_retries(chip);
485         if (ret < 0)
486                 return ret;
487         ret = fusb302_init_interrupt(chip);
488         if (ret < 0)
489                 return ret;
490         ret = fusb302_set_power_mode(chip, FUSB_REG_POWER_PWR_ALL);
491         if (ret < 0)
492                 return ret;
493         ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &data);
494         if (ret < 0)
495                 return ret;
496         chip->vbus_present = !!(data & FUSB_REG_STATUS0_VBUSOK);
497         ret = fusb302_i2c_read(chip, FUSB_REG_DEVICE_ID, &data);
498         if (ret < 0)
499                 return ret;
500         fusb302_log(chip, "fusb302 device ID: 0x%02x", data);
501
502         return ret;
503 }
504
505 static int tcpm_get_vbus(struct tcpc_dev *dev)
506 {
507         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
508                                                  tcpc_dev);
509         int ret = 0;
510
511         mutex_lock(&chip->lock);
512         ret = chip->vbus_present ? 1 : 0;
513         mutex_unlock(&chip->lock);
514
515         return ret;
516 }
517
518 static int fusb302_set_cc_pull(struct fusb302_chip *chip,
519                                bool pull_up, bool pull_down)
520 {
521         int ret = 0;
522         u8 data = 0x00;
523         u8 mask = FUSB_REG_SWITCHES0_CC1_PU_EN |
524                   FUSB_REG_SWITCHES0_CC2_PU_EN |
525                   FUSB_REG_SWITCHES0_CC1_PD_EN |
526                   FUSB_REG_SWITCHES0_CC2_PD_EN;
527
528         if (pull_up)
529                 data |= (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
530                         FUSB_REG_SWITCHES0_CC1_PU_EN :
531                         FUSB_REG_SWITCHES0_CC2_PU_EN;
532         if (pull_down)
533                 data |= FUSB_REG_SWITCHES0_CC1_PD_EN |
534                         FUSB_REG_SWITCHES0_CC2_PD_EN;
535         ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
536                                      mask, data);
537         if (ret < 0)
538                 return ret;
539         chip->pull_up = pull_up;
540
541         return ret;
542 }
543
544 static int fusb302_set_src_current(struct fusb302_chip *chip,
545                                    enum src_current_status status)
546 {
547         int ret = 0;
548
549         chip->src_current_status = status;
550         switch (status) {
551         case SRC_CURRENT_DEFAULT:
552                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
553                                              FUSB_REG_CONTROL0_HOST_CUR_MASK,
554                                              FUSB_REG_CONTROL0_HOST_CUR_DEF);
555                 break;
556         case SRC_CURRENT_MEDIUM:
557                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
558                                              FUSB_REG_CONTROL0_HOST_CUR_MASK,
559                                              FUSB_REG_CONTROL0_HOST_CUR_MED);
560                 break;
561         case SRC_CURRENT_HIGH:
562                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL0,
563                                              FUSB_REG_CONTROL0_HOST_CUR_MASK,
564                                              FUSB_REG_CONTROL0_HOST_CUR_HIGH);
565                 break;
566         default:
567                 break;
568         }
569
570         return ret;
571 }
572
573 static int fusb302_set_toggling(struct fusb302_chip *chip,
574                                 enum toggling_mode mode)
575 {
576         int ret = 0;
577
578         /* first disable toggling */
579         ret = fusb302_i2c_clear_bits(chip, FUSB_REG_CONTROL2,
580                                      FUSB_REG_CONTROL2_TOGGLE);
581         if (ret < 0)
582                 return ret;
583         /* mask interrupts for SRC or SNK */
584         ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASK,
585                                    FUSB_REG_MASK_BC_LVL |
586                                    FUSB_REG_MASK_COMP_CHNG);
587         if (ret < 0)
588                 return ret;
589         chip->intr_bc_lvl = false;
590         chip->intr_comp_chng = false;
591         /* configure toggling mode: none/snk/src/drp */
592         switch (mode) {
593         case TOGGLINE_MODE_OFF:
594                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
595                                              FUSB_REG_CONTROL2_MODE_MASK,
596                                              FUSB_REG_CONTROL2_MODE_NONE);
597                 if (ret < 0)
598                         return ret;
599                 break;
600         case TOGGLING_MODE_SNK:
601                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
602                                              FUSB_REG_CONTROL2_MODE_MASK,
603                                              FUSB_REG_CONTROL2_MODE_UFP);
604                 if (ret < 0)
605                         return ret;
606                 break;
607         case TOGGLING_MODE_SRC:
608                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
609                                              FUSB_REG_CONTROL2_MODE_MASK,
610                                              FUSB_REG_CONTROL2_MODE_DFP);
611                 if (ret < 0)
612                         return ret;
613                 break;
614         case TOGGLING_MODE_DRP:
615                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_CONTROL2,
616                                              FUSB_REG_CONTROL2_MODE_MASK,
617                                              FUSB_REG_CONTROL2_MODE_DRP);
618                 if (ret < 0)
619                         return ret;
620                 break;
621         default:
622                 break;
623         }
624
625         if (mode == TOGGLINE_MODE_OFF) {
626                 /* mask TOGDONE interrupt */
627                 ret = fusb302_i2c_set_bits(chip, FUSB_REG_MASKA,
628                                            FUSB_REG_MASKA_TOGDONE);
629                 if (ret < 0)
630                         return ret;
631                 chip->intr_togdone = false;
632         } else {
633                 /* unmask TOGDONE interrupt */
634                 ret = fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA,
635                                              FUSB_REG_MASKA_TOGDONE);
636                 if (ret < 0)
637                         return ret;
638                 chip->intr_togdone = true;
639                 /* start toggling */
640                 ret = fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL2,
641                                            FUSB_REG_CONTROL2_TOGGLE);
642                 if (ret < 0)
643                         return ret;
644                 /* during toggling, consider cc as Open */
645                 chip->cc1 = TYPEC_CC_OPEN;
646                 chip->cc2 = TYPEC_CC_OPEN;
647         }
648         chip->toggling_mode = mode;
649
650         return ret;
651 }
652
653 static const char * const typec_cc_status_name[] = {
654         [TYPEC_CC_OPEN]         = "Open",
655         [TYPEC_CC_RA]           = "Ra",
656         [TYPEC_CC_RD]           = "Rd",
657         [TYPEC_CC_RP_DEF]       = "Rp-def",
658         [TYPEC_CC_RP_1_5]       = "Rp-1.5",
659         [TYPEC_CC_RP_3_0]       = "Rp-3.0",
660 };
661
662 static const enum src_current_status cc_src_current[] = {
663         [TYPEC_CC_OPEN]         = SRC_CURRENT_DEFAULT,
664         [TYPEC_CC_RA]           = SRC_CURRENT_DEFAULT,
665         [TYPEC_CC_RD]           = SRC_CURRENT_DEFAULT,
666         [TYPEC_CC_RP_DEF]       = SRC_CURRENT_DEFAULT,
667         [TYPEC_CC_RP_1_5]       = SRC_CURRENT_MEDIUM,
668         [TYPEC_CC_RP_3_0]       = SRC_CURRENT_HIGH,
669 };
670
671 static int tcpm_set_cc(struct tcpc_dev *dev, enum typec_cc_status cc)
672 {
673         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
674                                                  tcpc_dev);
675         int ret = 0;
676         bool pull_up, pull_down;
677         u8 rd_mda;
678
679         mutex_lock(&chip->lock);
680         switch (cc) {
681         case TYPEC_CC_OPEN:
682                 pull_up = false;
683                 pull_down = false;
684                 break;
685         case TYPEC_CC_RD:
686                 pull_up = false;
687                 pull_down = true;
688                 break;
689         case TYPEC_CC_RP_DEF:
690         case TYPEC_CC_RP_1_5:
691         case TYPEC_CC_RP_3_0:
692                 pull_up = true;
693                 pull_down = false;
694                 break;
695         default:
696                 fusb302_log(chip, "unsupported cc value %s",
697                             typec_cc_status_name[cc]);
698                 ret = -EINVAL;
699                 goto done;
700         }
701         ret = fusb302_set_toggling(chip, TOGGLINE_MODE_OFF);
702         if (ret < 0) {
703                 fusb302_log(chip, "cannot stop toggling, ret=%d", ret);
704                 goto done;
705         }
706         ret = fusb302_set_cc_pull(chip, pull_up, pull_down);
707         if (ret < 0) {
708                 fusb302_log(chip,
709                             "cannot set cc pulling up %s, down %s, ret = %d",
710                             pull_up ? "True" : "False",
711                             pull_down ? "True" : "False",
712                             ret);
713                 goto done;
714         }
715         /* reset the cc status */
716         chip->cc1 = TYPEC_CC_OPEN;
717         chip->cc2 = TYPEC_CC_OPEN;
718         /* adjust current for SRC */
719         if (pull_up) {
720                 ret = fusb302_set_src_current(chip, cc_src_current[cc]);
721                 if (ret < 0) {
722                         fusb302_log(chip, "cannot set src current %s, ret=%d",
723                                     typec_cc_status_name[cc], ret);
724                         goto done;
725                 }
726         }
727         /* enable/disable interrupts, BC_LVL for SNK and COMP_CHNG for SRC */
728         if (pull_up) {
729                 rd_mda = rd_mda_value[cc_src_current[cc]];
730                 ret = fusb302_i2c_write(chip, FUSB_REG_MEASURE, rd_mda);
731                 if (ret < 0) {
732                         fusb302_log(chip,
733                                     "cannot set SRC measure value, ret=%d",
734                                     ret);
735                         goto done;
736                 }
737                 ret = fusb302_i2c_mask_write(chip, FUSB_REG_MASK,
738                                              FUSB_REG_MASK_BC_LVL |
739                                              FUSB_REG_MASK_COMP_CHNG,
740                                              FUSB_REG_MASK_COMP_CHNG);
741                 if (ret < 0) {
742                         fusb302_log(chip, "cannot set SRC interrupt, ret=%d",
743                                     ret);
744                         goto done;
745                 }
746                 chip->intr_bc_lvl = false;
747                 chip->intr_comp_chng = true;
748         }
749         if (pull_down) {
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_BC_LVL);
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 = true;
760                 chip->intr_comp_chng = false;
761         }
762         fusb302_log(chip, "cc := %s", typec_cc_status_name[cc]);
763 done:
764         mutex_unlock(&chip->lock);
765
766         return ret;
767 }
768
769 static int tcpm_get_cc(struct tcpc_dev *dev, enum typec_cc_status *cc1,
770                        enum typec_cc_status *cc2)
771 {
772         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
773                                                  tcpc_dev);
774
775         mutex_lock(&chip->lock);
776         *cc1 = chip->cc1;
777         *cc2 = chip->cc2;
778         fusb302_log(chip, "cc1=%s, cc2=%s", typec_cc_status_name[*cc1],
779                     typec_cc_status_name[*cc2]);
780         mutex_unlock(&chip->lock);
781
782         return 0;
783 }
784
785 static int tcpm_set_polarity(struct tcpc_dev *dev,
786                              enum typec_cc_polarity polarity)
787 {
788         return 0;
789 }
790
791 static int tcpm_set_vconn(struct tcpc_dev *dev, bool on)
792 {
793         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
794                                                  tcpc_dev);
795         int ret = 0;
796         u8 switches0_data = 0x00;
797         u8 switches0_mask = FUSB_REG_SWITCHES0_VCONN_CC1 |
798                             FUSB_REG_SWITCHES0_VCONN_CC2;
799
800         mutex_lock(&chip->lock);
801         if (chip->vconn_on == on) {
802                 fusb302_log(chip, "vconn is already %s", on ? "On" : "Off");
803                 goto done;
804         }
805         if (on) {
806                 switches0_data = (chip->cc_polarity == TYPEC_POLARITY_CC1) ?
807                                  FUSB_REG_SWITCHES0_VCONN_CC2 :
808                                  FUSB_REG_SWITCHES0_VCONN_CC1;
809         }
810         ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES0,
811                                      switches0_mask, switches0_data);
812         if (ret < 0)
813                 goto done;
814         chip->vconn_on = on;
815         fusb302_log(chip, "vconn := %s", on ? "On" : "Off");
816 done:
817         mutex_unlock(&chip->lock);
818
819         return ret;
820 }
821
822 static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
823 {
824         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
825                                                  tcpc_dev);
826         int ret = 0;
827
828         mutex_lock(&chip->lock);
829         if (chip->vbus_on == on) {
830                 fusb302_log(chip, "vbus is already %s", on ? "On" : "Off");
831         } else {
832                 if (on)
833                         ret = regulator_enable(chip->vbus);
834                 else
835                         ret = regulator_disable(chip->vbus);
836                 if (ret < 0) {
837                         fusb302_log(chip, "cannot %s vbus regulator, ret=%d",
838                                     on ? "enable" : "disable", ret);
839                         goto done;
840                 }
841                 chip->vbus_on = on;
842                 fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
843         }
844         if (chip->charge_on == charge)
845                 fusb302_log(chip, "charge is already %s",
846                             charge ? "On" : "Off");
847         else
848                 chip->charge_on = charge;
849
850 done:
851         mutex_unlock(&chip->lock);
852
853         return ret;
854 }
855
856 static int tcpm_set_current_limit(struct tcpc_dev *dev, u32 max_ma, u32 mv)
857 {
858         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
859                                                  tcpc_dev);
860
861         fusb302_log(chip, "current limit: %d ma, %d mv (not implemented)",
862                     max_ma, mv);
863
864         return 0;
865 }
866
867 static int fusb302_pd_tx_flush(struct fusb302_chip *chip)
868 {
869         return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL0,
870                                     FUSB_REG_CONTROL0_TX_FLUSH);
871 }
872
873 static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
874 {
875         return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL1,
876                                     FUSB_REG_CONTROL1_RX_FLUSH);
877 }
878
879 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
880 {
881         if (on)
882                 return fusb302_i2c_set_bits(chip, FUSB_REG_SWITCHES1,
883                                             FUSB_REG_SWITCHES1_AUTO_GCRC);
884         return fusb302_i2c_clear_bits(chip, FUSB_REG_SWITCHES1,
885                                             FUSB_REG_SWITCHES1_AUTO_GCRC);
886 }
887
888 static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
889 {
890         int ret = 0;
891         u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
892         u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
893                               FUSB_REG_MASKA_HARDSENT |
894                               FUSB_REG_MASKA_TX_SUCCESS |
895                               FUSB_REG_MASKA_HARDRESET;
896         u8 maskb_interrupts = FUSB_REG_MASKB_GCRCSENT;
897
898         ret = on ?
899                 fusb302_i2c_clear_bits(chip, FUSB_REG_MASK, mask_interrupts) :
900                 fusb302_i2c_set_bits(chip, FUSB_REG_MASK, mask_interrupts);
901         if (ret < 0)
902                 return ret;
903         ret = on ?
904                 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKA, maska_interrupts) :
905                 fusb302_i2c_set_bits(chip, FUSB_REG_MASKA, maska_interrupts);
906         if (ret < 0)
907                 return ret;
908         ret = on ?
909                 fusb302_i2c_clear_bits(chip, FUSB_REG_MASKB, maskb_interrupts) :
910                 fusb302_i2c_set_bits(chip, FUSB_REG_MASKB, maskb_interrupts);
911         return ret;
912 }
913
914 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
915 {
916         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
917                                                  tcpc_dev);
918         int ret = 0;
919
920         mutex_lock(&chip->lock);
921         ret = fusb302_pd_rx_flush(chip);
922         if (ret < 0) {
923                 fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
924                 goto done;
925         }
926         ret = fusb302_pd_tx_flush(chip);
927         if (ret < 0) {
928                 fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
929                 goto done;
930         }
931         ret = fusb302_pd_set_auto_goodcrc(chip, on);
932         if (ret < 0) {
933                 fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
934                             on ? "on" : "off", ret);
935                 goto done;
936         }
937         ret = fusb302_pd_set_interrupts(chip, on);
938         if (ret < 0) {
939                 fusb302_log(chip, "cannot turn %s pd interrupts, ret=%d",
940                             on ? "on" : "off", ret);
941                 goto done;
942         }
943         fusb302_log(chip, "pd := %s", on ? "on" : "off");
944 done:
945         mutex_unlock(&chip->lock);
946
947         return ret;
948 }
949
950 static const char * const typec_role_name[] = {
951         [TYPEC_SINK]            = "Sink",
952         [TYPEC_SOURCE]          = "Source",
953 };
954
955 static const char * const typec_data_role_name[] = {
956         [TYPEC_DEVICE]          = "Device",
957         [TYPEC_HOST]            = "Host",
958 };
959
960 static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
961                           enum typec_role pwr, enum typec_data_role data)
962 {
963         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
964                                                  tcpc_dev);
965         int ret = 0;
966         u8 switches1_mask = FUSB_REG_SWITCHES1_POWERROLE |
967                             FUSB_REG_SWITCHES1_DATAROLE;
968         u8 switches1_data = 0x00;
969
970         mutex_lock(&chip->lock);
971         if (pwr == TYPEC_SOURCE)
972                 switches1_data |= FUSB_REG_SWITCHES1_POWERROLE;
973         if (data == TYPEC_HOST)
974                 switches1_data |= FUSB_REG_SWITCHES1_DATAROLE;
975         ret = fusb302_i2c_mask_write(chip, FUSB_REG_SWITCHES1,
976                                      switches1_mask, switches1_data);
977         if (ret < 0) {
978                 fusb302_log(chip, "unable to set pd header %s, %s, ret=%d",
979                             typec_role_name[pwr], typec_data_role_name[data],
980                             ret);
981                 goto done;
982         }
983         fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
984                     typec_data_role_name[data]);
985 done:
986         mutex_unlock(&chip->lock);
987
988         return ret;
989 }
990
991 static int tcpm_start_drp_toggling(struct tcpc_dev *dev,
992                                    enum typec_cc_status cc)
993 {
994         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
995                                                  tcpc_dev);
996         int ret = 0;
997
998         mutex_lock(&chip->lock);
999         ret = fusb302_set_src_current(chip, cc_src_current[cc]);
1000         if (ret < 0) {
1001                 fusb302_log(chip, "unable to set src current %s, ret=%d",
1002                             typec_cc_status_name[cc], ret);
1003                 goto done;
1004         }
1005         ret = fusb302_set_toggling(chip, TOGGLING_MODE_DRP);
1006         if (ret < 0) {
1007                 fusb302_log(chip,
1008                             "unable to start drp toggling, ret=%d", ret);
1009                 goto done;
1010         }
1011         fusb302_log(chip, "start drp toggling");
1012 done:
1013         mutex_unlock(&chip->lock);
1014
1015         return ret;
1016 }
1017
1018 static int fusb302_pd_send_message(struct fusb302_chip *chip,
1019                                    const struct pd_message *msg)
1020 {
1021         int ret = 0;
1022         u8 buf[40];
1023         u8 pos = 0;
1024         int len;
1025
1026         /* SOP tokens */
1027         buf[pos++] = FUSB302_TKN_SYNC1;
1028         buf[pos++] = FUSB302_TKN_SYNC1;
1029         buf[pos++] = FUSB302_TKN_SYNC1;
1030         buf[pos++] = FUSB302_TKN_SYNC2;
1031
1032         len = pd_header_cnt_le(msg->header) * 4;
1033         /* plug 2 for header */
1034         len += 2;
1035         if (len > 0x1F) {
1036                 fusb302_log(chip,
1037                             "PD message too long %d (incl. header)", len);
1038                 return -EINVAL;
1039         }
1040         /* packsym tells the FUSB302 chip that the next X bytes are payload */
1041         buf[pos++] = FUSB302_TKN_PACKSYM | (len & 0x1F);
1042         buf[pos++] = msg->header & 0xFF;
1043         buf[pos++] = (msg->header >> 8) & 0xFF;
1044
1045         len -= 2;
1046         memcpy(&buf[pos], msg->payload, len);
1047         pos += len;
1048
1049         /* CRC */
1050         buf[pos++] = FUSB302_TKN_JAMCRC;
1051         /* EOP */
1052         buf[pos++] = FUSB302_TKN_EOP;
1053         /* turn tx off after sending message */
1054         buf[pos++] = FUSB302_TKN_TXOFF;
1055         /* start transmission */
1056         buf[pos++] = FUSB302_TKN_TXON;
1057
1058         ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
1059         if (ret < 0)
1060                 return ret;
1061         fusb302_log(chip, "sending PD message header: %x", msg->header);
1062         fusb302_log(chip, "sending PD message len: %d", len);
1063
1064         return ret;
1065 }
1066
1067 static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
1068 {
1069         return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
1070                                     FUSB_REG_CONTROL3_SEND_HARDRESET);
1071 }
1072
1073 static const char * const transmit_type_name[] = {
1074         [TCPC_TX_SOP]                   = "SOP",
1075         [TCPC_TX_SOP_PRIME]             = "SOP'",
1076         [TCPC_TX_SOP_PRIME_PRIME]       = "SOP''",
1077         [TCPC_TX_SOP_DEBUG_PRIME]       = "DEBUG'",
1078         [TCPC_TX_SOP_DEBUG_PRIME_PRIME] = "DEBUG''",
1079         [TCPC_TX_HARD_RESET]            = "HARD_RESET",
1080         [TCPC_TX_CABLE_RESET]           = "CABLE_RESET",
1081         [TCPC_TX_BIST_MODE_2]           = "BIST_MODE_2",
1082 };
1083
1084 static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
1085                             const struct pd_message *msg)
1086 {
1087         struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
1088                                                  tcpc_dev);
1089         int ret = 0;
1090
1091         mutex_lock(&chip->lock);
1092         switch (type) {
1093         case TCPC_TX_SOP:
1094                 ret = fusb302_pd_send_message(chip, msg);
1095                 if (ret < 0)
1096                         fusb302_log(chip,
1097                                     "cannot send PD message, ret=%d", ret);
1098                 break;
1099         case TCPC_TX_HARD_RESET:
1100                 ret = fusb302_pd_send_hardreset(chip);
1101                 if (ret < 0)
1102                         fusb302_log(chip,
1103                                     "cannot send hardreset, ret=%d", ret);
1104                 break;
1105         default:
1106                 fusb302_log(chip, "type %s not supported",
1107                             transmit_type_name[type]);
1108                 ret = -EINVAL;
1109         }
1110         mutex_unlock(&chip->lock);
1111
1112         return ret;
1113 }
1114
1115 static enum typec_cc_status fusb302_bc_lvl_to_cc(u8 bc_lvl)
1116 {
1117         if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_1230_MAX)
1118                 return TYPEC_CC_RP_3_0;
1119         if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_600_1230)
1120                 return TYPEC_CC_RP_1_5;
1121         if (bc_lvl == FUSB_REG_STATUS0_BC_LVL_200_600)
1122                 return TYPEC_CC_RP_DEF;
1123         return TYPEC_CC_OPEN;
1124 }
1125
1126 static void fusb302_bc_lvl_handler_work(struct work_struct *work)
1127 {
1128         struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
1129                                                  bc_lvl_handler.work);
1130         int ret = 0;
1131         u8 status0;
1132         u8 bc_lvl;
1133         enum typec_cc_status cc_status;
1134
1135         mutex_lock(&chip->lock);
1136         if (!chip->intr_bc_lvl) {
1137                 fusb302_log(chip, "BC_LVL interrupt is turned off, abort");
1138                 goto done;
1139         }
1140         ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1141         if (ret < 0)
1142                 goto done;
1143         fusb302_log(chip, "BC_LVL handler, status0=0x%02x", status0);
1144         if (status0 & FUSB_REG_STATUS0_ACTIVITY) {
1145                 fusb302_log(chip, "CC activities detected, delay handling");
1146                 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1147                                  msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1148                 goto done;
1149         }
1150         bc_lvl = status0 & FUSB_REG_STATUS0_BC_LVL_MASK;
1151         cc_status = fusb302_bc_lvl_to_cc(bc_lvl);
1152         if (chip->cc_polarity == TYPEC_POLARITY_CC1) {
1153                 if (chip->cc1 != cc_status) {
1154                         fusb302_log(chip, "cc1: %s -> %s",
1155                                     typec_cc_status_name[chip->cc1],
1156                                     typec_cc_status_name[cc_status]);
1157                         chip->cc1 = cc_status;
1158                         tcpm_cc_change(chip->tcpm_port);
1159                 }
1160         } else {
1161                 if (chip->cc2 != cc_status) {
1162                         fusb302_log(chip, "cc2: %s -> %s",
1163                                     typec_cc_status_name[chip->cc2],
1164                                     typec_cc_status_name[cc_status]);
1165                         chip->cc2 = cc_status;
1166                         tcpm_cc_change(chip->tcpm_port);
1167                 }
1168         }
1169
1170 done:
1171         mutex_unlock(&chip->lock);
1172 }
1173
1174 #define PDO_FIXED_FLAGS \
1175         (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
1176
1177 static const u32 src_pdo[] = {
1178         PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1179 };
1180
1181 static const u32 snk_pdo[] = {
1182         PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
1183 };
1184
1185 static const struct tcpc_config fusb302_tcpc_config = {
1186         .src_pdo = src_pdo,
1187         .nr_src_pdo = ARRAY_SIZE(src_pdo),
1188         .snk_pdo = snk_pdo,
1189         .nr_snk_pdo = ARRAY_SIZE(snk_pdo),
1190         .max_snk_mv = 9000,
1191         .max_snk_ma = 3000,
1192         .max_snk_mw = 27000,
1193         .operating_snk_mw = 2500,
1194         .type = TYPEC_PORT_DRP,
1195         .default_role = TYPEC_SINK,
1196         .alt_modes = NULL,
1197 };
1198
1199 static void init_tcpc_dev(struct tcpc_dev *fusb302_tcpc_dev)
1200 {
1201         fusb302_tcpc_dev->config = &fusb302_tcpc_config;
1202         fusb302_tcpc_dev->init = tcpm_init;
1203         fusb302_tcpc_dev->get_vbus = tcpm_get_vbus;
1204         fusb302_tcpc_dev->set_cc = tcpm_set_cc;
1205         fusb302_tcpc_dev->get_cc = tcpm_get_cc;
1206         fusb302_tcpc_dev->set_polarity = tcpm_set_polarity;
1207         fusb302_tcpc_dev->set_vconn = tcpm_set_vconn;
1208         fusb302_tcpc_dev->set_vbus = tcpm_set_vbus;
1209         fusb302_tcpc_dev->set_current_limit = tcpm_set_current_limit;
1210         fusb302_tcpc_dev->set_pd_rx = tcpm_set_pd_rx;
1211         fusb302_tcpc_dev->set_roles = tcpm_set_roles;
1212         fusb302_tcpc_dev->start_drp_toggling = tcpm_start_drp_toggling;
1213         fusb302_tcpc_dev->pd_transmit = tcpm_pd_transmit;
1214         fusb302_tcpc_dev->mux = NULL;
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         return ret;
1508 }
1509
1510 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
1511 {
1512         struct fusb302_chip *chip = dev_id;
1513         int ret = 0;
1514         u8 interrupt;
1515         u8 interrupta;
1516         u8 interruptb;
1517         u8 status0;
1518         bool vbus_present;
1519         bool comp_result;
1520         bool intr_togdone;
1521         bool intr_bc_lvl;
1522         bool intr_comp_chng;
1523         struct pd_message pd_msg;
1524
1525         mutex_lock(&chip->lock);
1526         /* grab a snapshot of intr flags */
1527         intr_togdone = chip->intr_togdone;
1528         intr_bc_lvl = chip->intr_bc_lvl;
1529         intr_comp_chng = chip->intr_comp_chng;
1530
1531         ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
1532         if (ret < 0)
1533                 goto done;
1534         ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
1535         if (ret < 0)
1536                 goto done;
1537         ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
1538         if (ret < 0)
1539                 goto done;
1540         ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
1541         if (ret < 0)
1542                 goto done;
1543         fusb302_log(chip,
1544                     "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
1545                     interrupt, interrupta, interruptb, status0);
1546
1547         if (interrupt & FUSB_REG_INTERRUPT_VBUSOK) {
1548                 vbus_present = !!(status0 & FUSB_REG_STATUS0_VBUSOK);
1549                 fusb302_log(chip, "IRQ: VBUS_OK, vbus=%s",
1550                             vbus_present ? "On" : "Off");
1551                 if (vbus_present != chip->vbus_present) {
1552                         chip->vbus_present = vbus_present;
1553                         tcpm_vbus_change(chip->tcpm_port);
1554                 }
1555         }
1556
1557         if ((interrupta & FUSB_REG_INTERRUPTA_TOGDONE) && intr_togdone) {
1558                 fusb302_log(chip, "IRQ: TOGDONE");
1559                 ret = fusb302_handle_togdone(chip);
1560                 if (ret < 0) {
1561                         fusb302_log(chip,
1562                                     "handle togdone error, ret=%d", ret);
1563                         goto done;
1564                 }
1565         }
1566
1567         if ((interrupt & FUSB_REG_INTERRUPT_BC_LVL) && intr_bc_lvl) {
1568                 fusb302_log(chip, "IRQ: BC_LVL, handler pending");
1569                 /*
1570                  * as BC_LVL interrupt can be affected by PD activity,
1571                  * apply delay to for the handler to wait for the PD
1572                  * signaling to finish.
1573                  */
1574                 mod_delayed_work(chip->wq, &chip->bc_lvl_handler,
1575                                  msecs_to_jiffies(T_BC_LVL_DEBOUNCE_DELAY_MS));
1576         }
1577
1578         if ((interrupt & FUSB_REG_INTERRUPT_COMP_CHNG) && intr_comp_chng) {
1579                 comp_result = !!(status0 & FUSB_REG_STATUS0_COMP);
1580                 fusb302_log(chip, "IRQ: COMP_CHNG, comp=%s",
1581                             comp_result ? "true" : "false");
1582                 if (comp_result) {
1583                         /* cc level > Rd_threashold, detach */
1584                         if (chip->cc_polarity == TYPEC_POLARITY_CC1)
1585                                 chip->cc1 = TYPEC_CC_OPEN;
1586                         else
1587                                 chip->cc2 = TYPEC_CC_OPEN;
1588                         tcpm_cc_change(chip->tcpm_port);
1589                 }
1590         }
1591
1592         if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
1593                 fusb302_log(chip, "IRQ: PD collision");
1594                 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1595         }
1596
1597         if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
1598                 fusb302_log(chip, "IRQ: PD retry failed");
1599                 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
1600         }
1601
1602         if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
1603                 fusb302_log(chip, "IRQ: PD hardreset sent");
1604                 ret = fusb302_pd_reset(chip);
1605                 if (ret < 0) {
1606                         fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1607                         goto done;
1608                 }
1609                 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1610         }
1611
1612         if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
1613                 fusb302_log(chip, "IRQ: PD tx success");
1614                 /* read out the received good CRC */
1615                 ret = fusb302_pd_read_message(chip, &pd_msg);
1616                 if (ret < 0) {
1617                         fusb302_log(chip, "cannot read in GCRC, ret=%d", ret);
1618                         goto done;
1619                 }
1620                 tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
1621         }
1622
1623         if (interrupta & FUSB_REG_INTERRUPTA_HARDRESET) {
1624                 fusb302_log(chip, "IRQ: PD received hardreset");
1625                 ret = fusb302_pd_reset(chip);
1626                 if (ret < 0) {
1627                         fusb302_log(chip, "cannot PD reset, ret=%d", ret);
1628                         goto done;
1629                 }
1630                 tcpm_pd_hard_reset(chip->tcpm_port);
1631         }
1632
1633         if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
1634                 fusb302_log(chip, "IRQ: PD sent good CRC");
1635                 ret = fusb302_pd_read_message(chip, &pd_msg);
1636                 if (ret < 0) {
1637                         fusb302_log(chip,
1638                                     "cannot read in PD message, ret=%d", ret);
1639                         goto done;
1640                 }
1641                 tcpm_pd_receive(chip->tcpm_port, &pd_msg);
1642         }
1643 done:
1644         mutex_unlock(&chip->lock);
1645
1646         return IRQ_HANDLED;
1647 }
1648
1649 static int init_gpio(struct fusb302_chip *chip)
1650 {
1651         struct device_node *node;
1652         int ret = 0;
1653
1654         node = chip->dev->of_node;
1655         chip->gpio_int_n = of_get_named_gpio(node, "fcs,int_n", 0);
1656         if (!gpio_is_valid(chip->gpio_int_n)) {
1657                 ret = chip->gpio_int_n;
1658                 fusb302_log(chip, "cannot get named GPIO Int_N, ret=%d", ret);
1659                 return ret;
1660         }
1661         ret = devm_gpio_request(chip->dev, chip->gpio_int_n, "fcs,int_n");
1662         if (ret < 0) {
1663                 fusb302_log(chip, "cannot request GPIO Int_N, ret=%d", ret);
1664                 return ret;
1665         }
1666         ret = gpio_direction_input(chip->gpio_int_n);
1667         if (ret < 0) {
1668                 fusb302_log(chip,
1669                             "cannot set GPIO Int_N to input, ret=%d", ret);
1670                 return ret;
1671         }
1672         ret = gpio_to_irq(chip->gpio_int_n);
1673         if (ret < 0) {
1674                 fusb302_log(chip,
1675                             "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1676                 return ret;
1677         }
1678         chip->gpio_int_n_irq = ret;
1679         return 0;
1680 }
1681
1682 static int fusb302_probe(struct i2c_client *client,
1683                          const struct i2c_device_id *id)
1684 {
1685         struct fusb302_chip *chip;
1686         struct i2c_adapter *adapter;
1687         int ret = 0;
1688
1689         adapter = to_i2c_adapter(client->dev.parent);
1690         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
1691                 dev_err(&client->dev,
1692                         "I2C/SMBus block functionality not supported!\n");
1693                 return -ENODEV;
1694         }
1695         chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
1696         if (!chip)
1697                 return -ENOMEM;
1698
1699         chip->i2c_client = client;
1700         i2c_set_clientdata(client, chip);
1701         chip->dev = &client->dev;
1702         mutex_init(&chip->lock);
1703
1704         ret = fusb302_debugfs_init(chip);
1705         if (ret < 0)
1706                 return ret;
1707
1708         chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
1709         if (!chip->wq) {
1710                 ret = -ENOMEM;
1711                 goto clear_client_data;
1712         }
1713         INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
1714         init_tcpc_dev(&chip->tcpc_dev);
1715
1716         chip->vbus = devm_regulator_get(chip->dev, "vbus");
1717         if (IS_ERR(chip->vbus)) {
1718                 ret = PTR_ERR(chip->vbus);
1719                 goto destroy_workqueue;
1720         }
1721
1722         ret = init_gpio(chip);
1723         if (ret < 0)
1724                 goto destroy_workqueue;
1725
1726         chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
1727         if (IS_ERR(chip->tcpm_port)) {
1728                 ret = PTR_ERR(chip->tcpm_port);
1729                 fusb302_log(chip, "cannot register tcpm port, ret=%d", ret);
1730                 goto destroy_workqueue;
1731         }
1732
1733         ret = devm_request_threaded_irq(chip->dev, chip->gpio_int_n_irq,
1734                                         NULL, fusb302_irq_intn,
1735                                         IRQF_ONESHOT | IRQF_TRIGGER_LOW,
1736                                         "fsc_interrupt_int_n", chip);
1737         if (ret < 0) {
1738                 fusb302_log(chip,
1739                             "cannot request IRQ for GPIO Int_N, ret=%d", ret);
1740                 goto tcpm_unregister_port;
1741         }
1742         enable_irq_wake(chip->gpio_int_n_irq);
1743         return ret;
1744
1745 tcpm_unregister_port:
1746         tcpm_unregister_port(chip->tcpm_port);
1747 destroy_workqueue:
1748         destroy_workqueue(chip->wq);
1749 clear_client_data:
1750         i2c_set_clientdata(client, NULL);
1751         fusb302_debugfs_exit(chip);
1752
1753         return ret;
1754 }
1755
1756 static int fusb302_remove(struct i2c_client *client)
1757 {
1758         struct fusb302_chip *chip = i2c_get_clientdata(client);
1759
1760         tcpm_unregister_port(chip->tcpm_port);
1761         destroy_workqueue(chip->wq);
1762         i2c_set_clientdata(client, NULL);
1763         fusb302_debugfs_exit(chip);
1764
1765         return 0;
1766 }
1767
1768 static int fusb302_pm_suspend(struct device *dev)
1769 {
1770         struct fusb302_chip *chip = dev->driver_data;
1771
1772         if (atomic_read(&chip->i2c_busy))
1773                 return -EBUSY;
1774         atomic_set(&chip->pm_suspend, 1);
1775
1776         return 0;
1777 }
1778
1779 static int fusb302_pm_resume(struct device *dev)
1780 {
1781         struct fusb302_chip *chip = dev->driver_data;
1782
1783         atomic_set(&chip->pm_suspend, 0);
1784
1785         return 0;
1786 }
1787
1788 static const struct of_device_id fusb302_dt_match[] = {
1789         {.compatible = "fcs,fusb302"},
1790         {},
1791 };
1792 MODULE_DEVICE_TABLE(of, fusb302_dt_match);
1793
1794 static const struct i2c_device_id fusb302_i2c_device_id[] = {
1795         {"typec_fusb302", 0},
1796         {},
1797 };
1798 MODULE_DEVICE_TABLE(i2c, fusb302_i2c_device_id);
1799
1800 static const struct dev_pm_ops fusb302_pm_ops = {
1801         .suspend = fusb302_pm_suspend,
1802         .resume = fusb302_pm_resume,
1803 };
1804
1805 static struct i2c_driver fusb302_driver = {
1806         .driver = {
1807                    .name = "typec_fusb302",
1808                    .pm = &fusb302_pm_ops,
1809                    .of_match_table = of_match_ptr(fusb302_dt_match),
1810                    },
1811         .probe = fusb302_probe,
1812         .remove = fusb302_remove,
1813         .id_table = fusb302_i2c_device_id,
1814 };
1815 module_i2c_driver(fusb302_driver);
1816
1817 MODULE_AUTHOR("Yueyao Zhu <yueyao.zhu@gmail.com>");
1818 MODULE_DESCRIPTION("Fairchild FUSB302 Type-C Chip Driver");
1819 MODULE_LICENSE("GPL");