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