This source file includes following definitions.
- tcpc_to_tcpci
- tcpci_read16
- tcpci_write16
- tcpci_set_cc
- tcpci_start_toggling
- tcpci_to_typec_cc
- tcpci_get_cc
- tcpci_set_polarity
- tcpci_set_vconn
- tcpci_set_roles
- tcpci_set_pd_rx
- tcpci_get_vbus
- tcpci_set_vbus
- tcpci_pd_transmit
- tcpci_init
- tcpci_irq
- _tcpci_irq
- tcpci_parse_config
- tcpci_register_port
- tcpci_unregister_port
- tcpci_probe
- tcpci_remove
1
2
3
4
5
6
7
8 #include <linux/delay.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/property.h>
14 #include <linux/regmap.h>
15 #include <linux/usb/pd.h>
16 #include <linux/usb/tcpm.h>
17 #include <linux/usb/typec.h>
18
19 #include "tcpci.h"
20
21 #define PD_RETRY_COUNT 3
22
23 struct tcpci {
24 struct device *dev;
25
26 struct tcpm_port *port;
27
28 struct regmap *regmap;
29
30 bool controls_vbus;
31
32 struct tcpc_dev tcpc;
33 struct tcpci_data *data;
34 };
35
36 struct tcpci_chip {
37 struct tcpci *tcpci;
38 struct tcpci_data data;
39 };
40
41 static inline struct tcpci *tcpc_to_tcpci(struct tcpc_dev *tcpc)
42 {
43 return container_of(tcpc, struct tcpci, tcpc);
44 }
45
46 static int tcpci_read16(struct tcpci *tcpci, unsigned int reg, u16 *val)
47 {
48 return regmap_raw_read(tcpci->regmap, reg, val, sizeof(u16));
49 }
50
51 static int tcpci_write16(struct tcpci *tcpci, unsigned int reg, u16 val)
52 {
53 return regmap_raw_write(tcpci->regmap, reg, &val, sizeof(u16));
54 }
55
56 static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc)
57 {
58 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
59 unsigned int reg;
60 int ret;
61
62 switch (cc) {
63 case TYPEC_CC_RA:
64 reg = (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC1_SHIFT) |
65 (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC2_SHIFT);
66 break;
67 case TYPEC_CC_RD:
68 reg = (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
69 (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
70 break;
71 case TYPEC_CC_RP_DEF:
72 reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
73 (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
74 (TCPC_ROLE_CTRL_RP_VAL_DEF <<
75 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
76 break;
77 case TYPEC_CC_RP_1_5:
78 reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
79 (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
80 (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
81 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
82 break;
83 case TYPEC_CC_RP_3_0:
84 reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
85 (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
86 (TCPC_ROLE_CTRL_RP_VAL_3_0 <<
87 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
88 break;
89 case TYPEC_CC_OPEN:
90 default:
91 reg = (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT) |
92 (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT);
93 break;
94 }
95
96 ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
97 if (ret < 0)
98 return ret;
99
100 return 0;
101 }
102
103 static int tcpci_start_toggling(struct tcpc_dev *tcpc,
104 enum typec_port_type port_type,
105 enum typec_cc_status cc)
106 {
107 int ret;
108 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
109 unsigned int reg = TCPC_ROLE_CTRL_DRP;
110
111 if (port_type != TYPEC_PORT_DRP)
112 return -EOPNOTSUPP;
113
114
115 if (tcpci->data->start_drp_toggling) {
116 ret = tcpci->data->start_drp_toggling(tcpci, tcpci->data, cc);
117 if (ret < 0)
118 return ret;
119 }
120
121 switch (cc) {
122 default:
123 case TYPEC_CC_RP_DEF:
124 reg |= (TCPC_ROLE_CTRL_RP_VAL_DEF <<
125 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
126 break;
127 case TYPEC_CC_RP_1_5:
128 reg |= (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
129 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
130 break;
131 case TYPEC_CC_RP_3_0:
132 reg |= (TCPC_ROLE_CTRL_RP_VAL_3_0 <<
133 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
134 break;
135 }
136
137 if (cc == TYPEC_CC_RD)
138 reg |= (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
139 (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
140 else
141 reg |= (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
142 (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT);
143 ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
144 if (ret < 0)
145 return ret;
146 return regmap_write(tcpci->regmap, TCPC_COMMAND,
147 TCPC_CMD_LOOK4CONNECTION);
148 }
149
150 static enum typec_cc_status tcpci_to_typec_cc(unsigned int cc, bool sink)
151 {
152 switch (cc) {
153 case 0x1:
154 return sink ? TYPEC_CC_RP_DEF : TYPEC_CC_RA;
155 case 0x2:
156 return sink ? TYPEC_CC_RP_1_5 : TYPEC_CC_RD;
157 case 0x3:
158 if (sink)
159 return TYPEC_CC_RP_3_0;
160
161 case 0x0:
162 default:
163 return TYPEC_CC_OPEN;
164 }
165 }
166
167 static int tcpci_get_cc(struct tcpc_dev *tcpc,
168 enum typec_cc_status *cc1, enum typec_cc_status *cc2)
169 {
170 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
171 unsigned int reg;
172 int ret;
173
174 ret = regmap_read(tcpci->regmap, TCPC_CC_STATUS, ®);
175 if (ret < 0)
176 return ret;
177
178 *cc1 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC1_SHIFT) &
179 TCPC_CC_STATUS_CC1_MASK,
180 reg & TCPC_CC_STATUS_TERM);
181 *cc2 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC2_SHIFT) &
182 TCPC_CC_STATUS_CC2_MASK,
183 reg & TCPC_CC_STATUS_TERM);
184
185 return 0;
186 }
187
188 static int tcpci_set_polarity(struct tcpc_dev *tcpc,
189 enum typec_cc_polarity polarity)
190 {
191 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
192 unsigned int reg;
193 int ret;
194
195
196 ret = regmap_read(tcpci->regmap, TCPC_ROLE_CTRL, ®);
197 if (ret < 0)
198 return ret;
199
200 if (polarity == TYPEC_POLARITY_CC2)
201 reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT;
202 else
203 reg |= TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT;
204 ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
205 if (ret < 0)
206 return ret;
207
208 return regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
209 (polarity == TYPEC_POLARITY_CC2) ?
210 TCPC_TCPC_CTRL_ORIENTATION : 0);
211 }
212
213 static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable)
214 {
215 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
216 int ret;
217
218
219 if (tcpci->data->set_vconn) {
220 ret = tcpci->data->set_vconn(tcpci, tcpci->data, enable);
221 if (ret < 0)
222 return ret;
223 }
224
225 return regmap_update_bits(tcpci->regmap, TCPC_POWER_CTRL,
226 TCPC_POWER_CTRL_VCONN_ENABLE,
227 enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
228 }
229
230 static int tcpci_set_roles(struct tcpc_dev *tcpc, bool attached,
231 enum typec_role role, enum typec_data_role data)
232 {
233 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
234 unsigned int reg;
235 int ret;
236
237 reg = PD_REV20 << TCPC_MSG_HDR_INFO_REV_SHIFT;
238 if (role == TYPEC_SOURCE)
239 reg |= TCPC_MSG_HDR_INFO_PWR_ROLE;
240 if (data == TYPEC_HOST)
241 reg |= TCPC_MSG_HDR_INFO_DATA_ROLE;
242 ret = regmap_write(tcpci->regmap, TCPC_MSG_HDR_INFO, reg);
243 if (ret < 0)
244 return ret;
245
246 return 0;
247 }
248
249 static int tcpci_set_pd_rx(struct tcpc_dev *tcpc, bool enable)
250 {
251 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
252 unsigned int reg = 0;
253 int ret;
254
255 if (enable)
256 reg = TCPC_RX_DETECT_SOP | TCPC_RX_DETECT_HARD_RESET;
257 ret = regmap_write(tcpci->regmap, TCPC_RX_DETECT, reg);
258 if (ret < 0)
259 return ret;
260
261 return 0;
262 }
263
264 static int tcpci_get_vbus(struct tcpc_dev *tcpc)
265 {
266 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
267 unsigned int reg;
268 int ret;
269
270 ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, ®);
271 if (ret < 0)
272 return ret;
273
274 return !!(reg & TCPC_POWER_STATUS_VBUS_PRES);
275 }
276
277 static int tcpci_set_vbus(struct tcpc_dev *tcpc, bool source, bool sink)
278 {
279 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
280 int ret;
281
282
283
284 if (!source) {
285 ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
286 TCPC_CMD_DISABLE_SRC_VBUS);
287 if (ret < 0)
288 return ret;
289 }
290
291 if (!sink) {
292 ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
293 TCPC_CMD_DISABLE_SINK_VBUS);
294 if (ret < 0)
295 return ret;
296 }
297
298 if (source) {
299 ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
300 TCPC_CMD_SRC_VBUS_DEFAULT);
301 if (ret < 0)
302 return ret;
303 }
304
305 if (sink) {
306 ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
307 TCPC_CMD_SINK_VBUS);
308 if (ret < 0)
309 return ret;
310 }
311
312 return 0;
313 }
314
315 static int tcpci_pd_transmit(struct tcpc_dev *tcpc,
316 enum tcpm_transmit_type type,
317 const struct pd_message *msg)
318 {
319 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
320 u16 header = msg ? le16_to_cpu(msg->header) : 0;
321 unsigned int reg, cnt;
322 int ret;
323
324 cnt = msg ? pd_header_cnt(header) * 4 : 0;
325 ret = regmap_write(tcpci->regmap, TCPC_TX_BYTE_CNT, cnt + 2);
326 if (ret < 0)
327 return ret;
328
329 ret = tcpci_write16(tcpci, TCPC_TX_HDR, header);
330 if (ret < 0)
331 return ret;
332
333 if (cnt > 0) {
334 ret = regmap_raw_write(tcpci->regmap, TCPC_TX_DATA,
335 &msg->payload, cnt);
336 if (ret < 0)
337 return ret;
338 }
339
340 reg = (PD_RETRY_COUNT << TCPC_TRANSMIT_RETRY_SHIFT) |
341 (type << TCPC_TRANSMIT_TYPE_SHIFT);
342 ret = regmap_write(tcpci->regmap, TCPC_TRANSMIT, reg);
343 if (ret < 0)
344 return ret;
345
346 return 0;
347 }
348
349 static int tcpci_init(struct tcpc_dev *tcpc)
350 {
351 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
352 unsigned long timeout = jiffies + msecs_to_jiffies(2000);
353 unsigned int reg;
354 int ret;
355
356 while (time_before_eq(jiffies, timeout)) {
357 ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, ®);
358 if (ret < 0)
359 return ret;
360 if (!(reg & TCPC_POWER_STATUS_UNINIT))
361 break;
362 usleep_range(10000, 20000);
363 }
364 if (time_after(jiffies, timeout))
365 return -ETIMEDOUT;
366
367
368 if (tcpci->data->init) {
369 ret = tcpci->data->init(tcpci, tcpci->data);
370 if (ret < 0)
371 return ret;
372 }
373
374
375 ret = tcpci_write16(tcpci, TCPC_ALERT, 0xffff);
376 if (ret < 0)
377 return ret;
378
379 if (tcpci->controls_vbus)
380 reg = TCPC_POWER_STATUS_VBUS_PRES;
381 else
382 reg = 0;
383 ret = regmap_write(tcpci->regmap, TCPC_POWER_STATUS_MASK, reg);
384 if (ret < 0)
385 return ret;
386
387
388 ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
389 TCPC_CMD_ENABLE_VBUS_DETECT);
390 if (ret < 0)
391 return ret;
392
393 reg = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_FAILED |
394 TCPC_ALERT_TX_DISCARDED | TCPC_ALERT_RX_STATUS |
395 TCPC_ALERT_RX_HARD_RST | TCPC_ALERT_CC_STATUS;
396 if (tcpci->controls_vbus)
397 reg |= TCPC_ALERT_POWER_STATUS;
398 return tcpci_write16(tcpci, TCPC_ALERT_MASK, reg);
399 }
400
401 irqreturn_t tcpci_irq(struct tcpci *tcpci)
402 {
403 u16 status;
404
405 tcpci_read16(tcpci, TCPC_ALERT, &status);
406
407
408
409
410
411 if (status & ~TCPC_ALERT_RX_STATUS)
412 tcpci_write16(tcpci, TCPC_ALERT,
413 status & ~TCPC_ALERT_RX_STATUS);
414
415 if (status & TCPC_ALERT_CC_STATUS)
416 tcpm_cc_change(tcpci->port);
417
418 if (status & TCPC_ALERT_POWER_STATUS) {
419 unsigned int reg;
420
421 regmap_read(tcpci->regmap, TCPC_POWER_STATUS_MASK, ®);
422
423
424
425
426
427 if (reg == 0xff)
428 tcpm_tcpc_reset(tcpci->port);
429 else
430 tcpm_vbus_change(tcpci->port);
431 }
432
433 if (status & TCPC_ALERT_RX_STATUS) {
434 struct pd_message msg;
435 unsigned int cnt, payload_cnt;
436 u16 header;
437
438 regmap_read(tcpci->regmap, TCPC_RX_BYTE_CNT, &cnt);
439
440
441
442
443
444
445 if (cnt > 3)
446 payload_cnt = cnt - (1 + sizeof(msg.header));
447 else
448 payload_cnt = 0;
449
450 tcpci_read16(tcpci, TCPC_RX_HDR, &header);
451 msg.header = cpu_to_le16(header);
452
453 if (WARN_ON(payload_cnt > sizeof(msg.payload)))
454 payload_cnt = sizeof(msg.payload);
455
456 if (payload_cnt > 0)
457 regmap_raw_read(tcpci->regmap, TCPC_RX_DATA,
458 &msg.payload, payload_cnt);
459
460
461 tcpci_write16(tcpci, TCPC_ALERT, TCPC_ALERT_RX_STATUS);
462
463 tcpm_pd_receive(tcpci->port, &msg);
464 }
465
466 if (status & TCPC_ALERT_RX_HARD_RST)
467 tcpm_pd_hard_reset(tcpci->port);
468
469 if (status & TCPC_ALERT_TX_SUCCESS)
470 tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_SUCCESS);
471 else if (status & TCPC_ALERT_TX_DISCARDED)
472 tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_DISCARDED);
473 else if (status & TCPC_ALERT_TX_FAILED)
474 tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_FAILED);
475
476 return IRQ_HANDLED;
477 }
478 EXPORT_SYMBOL_GPL(tcpci_irq);
479
480 static irqreturn_t _tcpci_irq(int irq, void *dev_id)
481 {
482 struct tcpci_chip *chip = dev_id;
483
484 return tcpci_irq(chip->tcpci);
485 }
486
487 static const struct regmap_config tcpci_regmap_config = {
488 .reg_bits = 8,
489 .val_bits = 8,
490
491 .max_register = 0x7F,
492 };
493
494 static int tcpci_parse_config(struct tcpci *tcpci)
495 {
496 tcpci->controls_vbus = true;
497
498 tcpci->tcpc.fwnode = device_get_named_child_node(tcpci->dev,
499 "connector");
500 if (!tcpci->tcpc.fwnode) {
501 dev_err(tcpci->dev, "Can't find connector node.\n");
502 return -EINVAL;
503 }
504
505 return 0;
506 }
507
508 struct tcpci *tcpci_register_port(struct device *dev, struct tcpci_data *data)
509 {
510 struct tcpci *tcpci;
511 int err;
512
513 tcpci = devm_kzalloc(dev, sizeof(*tcpci), GFP_KERNEL);
514 if (!tcpci)
515 return ERR_PTR(-ENOMEM);
516
517 tcpci->dev = dev;
518 tcpci->data = data;
519 tcpci->regmap = data->regmap;
520
521 tcpci->tcpc.init = tcpci_init;
522 tcpci->tcpc.get_vbus = tcpci_get_vbus;
523 tcpci->tcpc.set_vbus = tcpci_set_vbus;
524 tcpci->tcpc.set_cc = tcpci_set_cc;
525 tcpci->tcpc.get_cc = tcpci_get_cc;
526 tcpci->tcpc.set_polarity = tcpci_set_polarity;
527 tcpci->tcpc.set_vconn = tcpci_set_vconn;
528 tcpci->tcpc.start_toggling = tcpci_start_toggling;
529
530 tcpci->tcpc.set_pd_rx = tcpci_set_pd_rx;
531 tcpci->tcpc.set_roles = tcpci_set_roles;
532 tcpci->tcpc.pd_transmit = tcpci_pd_transmit;
533
534 err = tcpci_parse_config(tcpci);
535 if (err < 0)
536 return ERR_PTR(err);
537
538 tcpci->port = tcpm_register_port(tcpci->dev, &tcpci->tcpc);
539 if (IS_ERR(tcpci->port))
540 return ERR_CAST(tcpci->port);
541
542 return tcpci;
543 }
544 EXPORT_SYMBOL_GPL(tcpci_register_port);
545
546 void tcpci_unregister_port(struct tcpci *tcpci)
547 {
548 tcpm_unregister_port(tcpci->port);
549 }
550 EXPORT_SYMBOL_GPL(tcpci_unregister_port);
551
552 static int tcpci_probe(struct i2c_client *client,
553 const struct i2c_device_id *i2c_id)
554 {
555 struct tcpci_chip *chip;
556 int err;
557 u16 val = 0;
558
559 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
560 if (!chip)
561 return -ENOMEM;
562
563 chip->data.regmap = devm_regmap_init_i2c(client, &tcpci_regmap_config);
564 if (IS_ERR(chip->data.regmap))
565 return PTR_ERR(chip->data.regmap);
566
567 i2c_set_clientdata(client, chip);
568
569
570 err = regmap_raw_write(chip->data.regmap, TCPC_ALERT_MASK, &val,
571 sizeof(u16));
572 if (err < 0)
573 return err;
574
575 chip->tcpci = tcpci_register_port(&client->dev, &chip->data);
576 if (IS_ERR(chip->tcpci))
577 return PTR_ERR(chip->tcpci);
578
579 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
580 _tcpci_irq,
581 IRQF_ONESHOT | IRQF_TRIGGER_LOW,
582 dev_name(&client->dev), chip);
583 if (err < 0) {
584 tcpci_unregister_port(chip->tcpci);
585 return err;
586 }
587
588 return 0;
589 }
590
591 static int tcpci_remove(struct i2c_client *client)
592 {
593 struct tcpci_chip *chip = i2c_get_clientdata(client);
594 int err;
595
596
597 err = tcpci_write16(chip->tcpci, TCPC_ALERT_MASK, 0);
598 if (err < 0)
599 return err;
600
601 tcpci_unregister_port(chip->tcpci);
602
603 return 0;
604 }
605
606 static const struct i2c_device_id tcpci_id[] = {
607 { "tcpci", 0 },
608 { }
609 };
610 MODULE_DEVICE_TABLE(i2c, tcpci_id);
611
612 #ifdef CONFIG_OF
613 static const struct of_device_id tcpci_of_match[] = {
614 { .compatible = "nxp,ptn5110", },
615 {},
616 };
617 MODULE_DEVICE_TABLE(of, tcpci_of_match);
618 #endif
619
620 static struct i2c_driver tcpci_i2c_driver = {
621 .driver = {
622 .name = "tcpci",
623 .of_match_table = of_match_ptr(tcpci_of_match),
624 },
625 .probe = tcpci_probe,
626 .remove = tcpci_remove,
627 .id_table = tcpci_id,
628 };
629 module_i2c_driver(tcpci_i2c_driver);
630
631 MODULE_DESCRIPTION("USB Type-C Port Controller Interface driver");
632 MODULE_LICENSE("GPL");