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