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