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