2 * Copyright 2015-2017 Google, Inc
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * USB Type-C Port Controller Interface.
17 #include <linux/delay.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/i2c.h>
21 #include <linux/interrupt.h>
22 #include <linux/regmap.h>
23 #include <linux/usb/typec.h>
29 #define PD_RETRY_COUNT 3
33 struct i2c_client *client;
35 struct tcpm_port *port;
37 struct regmap *regmap;
44 static inline struct tcpci *tcpc_to_tcpci(struct tcpc_dev *tcpc)
46 return container_of(tcpc, struct tcpci, tcpc);
49 static int tcpci_read16(struct tcpci *tcpci, unsigned int reg,
52 return regmap_raw_read(tcpci->regmap, reg, val, sizeof(u16));
55 static int tcpci_write16(struct tcpci *tcpci, unsigned int reg, u16 val)
57 return regmap_raw_write(tcpci->regmap, reg, &val, sizeof(u16));
60 static int tcpci_set_cc(struct tcpc_dev *tcpc, enum typec_cc_status cc)
62 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
68 reg = (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC1_SHIFT) |
69 (TCPC_ROLE_CTRL_CC_RA << TCPC_ROLE_CTRL_CC2_SHIFT);
72 reg = (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
73 (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
76 reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
77 (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
78 (TCPC_ROLE_CTRL_RP_VAL_DEF <<
79 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
82 reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
83 (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
84 (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
85 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
88 reg = (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
89 (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT) |
90 (TCPC_ROLE_CTRL_RP_VAL_3_0 <<
91 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
95 reg = (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC1_SHIFT) |
96 (TCPC_ROLE_CTRL_CC_OPEN << TCPC_ROLE_CTRL_CC2_SHIFT);
100 ret = regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
107 static int tcpci_start_drp_toggling(struct tcpc_dev *tcpc,
108 enum typec_cc_status cc)
110 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
111 unsigned int reg = TCPC_ROLE_CTRL_DRP;
115 case TYPEC_CC_RP_DEF:
116 reg |= (TCPC_ROLE_CTRL_RP_VAL_DEF <<
117 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
119 case TYPEC_CC_RP_1_5:
120 reg |= (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
121 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
123 case TYPEC_CC_RP_3_0:
124 reg |= (TCPC_ROLE_CTRL_RP_VAL_3_0 <<
125 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
129 return regmap_write(tcpci->regmap, TCPC_ROLE_CTRL, reg);
132 static enum typec_cc_status tcpci_to_typec_cc(unsigned int cc, bool sink)
136 return sink ? TYPEC_CC_RP_DEF : TYPEC_CC_RA;
138 return sink ? TYPEC_CC_RP_1_5 : TYPEC_CC_RD;
141 return TYPEC_CC_RP_3_0;
144 return TYPEC_CC_OPEN;
148 static int tcpci_get_cc(struct tcpc_dev *tcpc,
149 enum typec_cc_status *cc1, enum typec_cc_status *cc2)
151 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
155 ret = regmap_read(tcpci->regmap, TCPC_CC_STATUS, ®);
159 *cc1 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC1_SHIFT) &
160 TCPC_CC_STATUS_CC1_MASK,
161 reg & TCPC_CC_STATUS_TERM);
162 *cc2 = tcpci_to_typec_cc((reg >> TCPC_CC_STATUS_CC2_SHIFT) &
163 TCPC_CC_STATUS_CC2_MASK,
164 reg & TCPC_CC_STATUS_TERM);
169 static int tcpci_set_polarity(struct tcpc_dev *tcpc,
170 enum typec_cc_polarity polarity)
172 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
175 ret = regmap_write(tcpci->regmap, TCPC_TCPC_CTRL,
176 (polarity == TYPEC_POLARITY_CC2) ?
177 TCPC_TCPC_CTRL_ORIENTATION : 0);
184 static int tcpci_set_vconn(struct tcpc_dev *tcpc, bool enable)
186 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
189 ret = regmap_write(tcpci->regmap, TCPC_POWER_CTRL,
190 enable ? TCPC_POWER_CTRL_VCONN_ENABLE : 0);
197 static int tcpci_set_roles(struct tcpc_dev *tcpc, bool attached,
198 enum typec_role role, enum typec_data_role data)
200 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
204 reg = PD_REV20 << TCPC_MSG_HDR_INFO_REV_SHIFT;
205 if (role == TYPEC_SOURCE)
206 reg |= TCPC_MSG_HDR_INFO_PWR_ROLE;
207 if (data == TYPEC_HOST)
208 reg |= TCPC_MSG_HDR_INFO_DATA_ROLE;
209 ret = regmap_write(tcpci->regmap, TCPC_MSG_HDR_INFO, reg);
216 static int tcpci_set_pd_rx(struct tcpc_dev *tcpc, bool enable)
218 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
219 unsigned int reg = 0;
223 reg = TCPC_RX_DETECT_SOP | TCPC_RX_DETECT_HARD_RESET;
224 ret = regmap_write(tcpci->regmap, TCPC_RX_DETECT, reg);
231 static int tcpci_get_vbus(struct tcpc_dev *tcpc)
233 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
237 ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, ®);
241 return !!(reg & TCPC_POWER_STATUS_VBUS_PRES);
244 static int tcpci_set_vbus(struct tcpc_dev *tcpc, bool source, bool sink)
246 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
249 /* Disable both source and sink first before enabling anything */
252 ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
253 TCPC_CMD_DISABLE_SRC_VBUS);
259 ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
260 TCPC_CMD_DISABLE_SINK_VBUS);
266 ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
267 TCPC_CMD_SRC_VBUS_DEFAULT);
273 ret = regmap_write(tcpci->regmap, TCPC_COMMAND,
282 static int tcpci_pd_transmit(struct tcpc_dev *tcpc,
283 enum tcpm_transmit_type type,
284 const struct pd_message *msg)
286 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
287 unsigned int reg, cnt, header;
290 cnt = msg ? pd_header_cnt(msg->header) * 4 : 0;
291 ret = regmap_write(tcpci->regmap, TCPC_TX_BYTE_CNT, cnt + 2);
295 header = msg ? msg->header : 0;
296 ret = tcpci_write16(tcpci, TCPC_TX_HDR, header);
301 ret = regmap_raw_write(tcpci->regmap, TCPC_TX_DATA,
307 reg = (PD_RETRY_COUNT << TCPC_TRANSMIT_RETRY_SHIFT) |
308 (type << TCPC_TRANSMIT_TYPE_SHIFT);
309 ret = regmap_write(tcpci->regmap, TCPC_TRANSMIT, reg);
316 static int tcpci_init(struct tcpc_dev *tcpc)
318 struct tcpci *tcpci = tcpc_to_tcpci(tcpc);
319 unsigned long timeout = jiffies + msecs_to_jiffies(2000); /* XXX */
323 while (time_before_eq(jiffies, timeout)) {
324 ret = regmap_read(tcpci->regmap, TCPC_POWER_STATUS, ®);
327 if (!(reg & TCPC_POWER_STATUS_UNINIT))
329 usleep_range(10000, 20000);
331 if (time_after(jiffies, timeout))
334 /* Clear all events */
335 ret = tcpci_write16(tcpci, TCPC_ALERT, 0xffff);
339 if (tcpci->controls_vbus)
340 reg = TCPC_POWER_STATUS_VBUS_PRES;
343 ret = regmap_write(tcpci->regmap, TCPC_POWER_STATUS_MASK, reg);
347 reg = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_FAILED |
348 TCPC_ALERT_TX_DISCARDED | TCPC_ALERT_RX_STATUS |
349 TCPC_ALERT_RX_HARD_RST | TCPC_ALERT_CC_STATUS;
350 if (tcpci->controls_vbus)
351 reg |= TCPC_ALERT_POWER_STATUS;
352 return tcpci_write16(tcpci, TCPC_ALERT_MASK, reg);
355 static irqreturn_t tcpci_irq(int irq, void *dev_id)
357 struct tcpci *tcpci = dev_id;
358 unsigned int status, reg;
360 tcpci_read16(tcpci, TCPC_ALERT, &status);
363 * Clear alert status for everything except RX_STATUS, which shouldn't
364 * be cleared until we have successfully retrieved message.
366 if (status & ~TCPC_ALERT_RX_STATUS)
367 tcpci_write16(tcpci, TCPC_ALERT,
368 status & ~TCPC_ALERT_RX_STATUS);
370 if (status & TCPC_ALERT_CC_STATUS)
371 tcpm_cc_change(tcpci->port);
373 if (status & TCPC_ALERT_POWER_STATUS) {
374 regmap_read(tcpci->regmap, TCPC_POWER_STATUS_MASK, ®);
377 * If power status mask has been reset, then the TCPC
381 tcpm_tcpc_reset(tcpci->port);
383 tcpm_vbus_change(tcpci->port);
386 if (status & TCPC_ALERT_RX_STATUS) {
387 struct pd_message msg;
390 regmap_read(tcpci->regmap, TCPC_RX_BYTE_CNT, &cnt);
392 tcpci_read16(tcpci, TCPC_RX_HDR, ®);
395 if (WARN_ON(cnt > sizeof(msg.payload)))
396 cnt = sizeof(msg.payload);
399 regmap_raw_read(tcpci->regmap, TCPC_RX_DATA,
402 /* Read complete, clear RX status alert bit */
403 tcpci_write16(tcpci, TCPC_ALERT, TCPC_ALERT_RX_STATUS);
405 tcpm_pd_receive(tcpci->port, &msg);
408 if (status & TCPC_ALERT_RX_HARD_RST)
409 tcpm_pd_hard_reset(tcpci->port);
411 if (status & TCPC_ALERT_TX_SUCCESS)
412 tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_SUCCESS);
413 else if (status & TCPC_ALERT_TX_DISCARDED)
414 tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_DISCARDED);
415 else if (status & TCPC_ALERT_TX_FAILED)
416 tcpm_pd_transmit_complete(tcpci->port, TCPC_TX_FAILED);
421 static const struct regmap_config tcpci_regmap_config = {
425 .max_register = 0x7F, /* 0x80 .. 0xFF are vendor defined */
428 const struct tcpc_config tcpci_tcpc_config = {
429 .type = TYPEC_PORT_DFP,
430 .default_role = TYPEC_SINK,
433 static int tcpci_parse_config(struct tcpci *tcpci)
435 tcpci->controls_vbus = true; /* XXX */
437 /* TODO: Populate struct tcpc_config from ACPI/device-tree */
438 tcpci->tcpc.config = &tcpci_tcpc_config;
443 static int tcpci_probe(struct i2c_client *client,
444 const struct i2c_device_id *i2c_id)
449 tcpci = devm_kzalloc(&client->dev, sizeof(*tcpci), GFP_KERNEL);
453 tcpci->client = client;
454 tcpci->dev = &client->dev;
455 i2c_set_clientdata(client, tcpci);
456 tcpci->regmap = devm_regmap_init_i2c(client, &tcpci_regmap_config);
457 if (IS_ERR(tcpci->regmap))
458 return PTR_ERR(tcpci->regmap);
460 tcpci->tcpc.init = tcpci_init;
461 tcpci->tcpc.get_vbus = tcpci_get_vbus;
462 tcpci->tcpc.set_vbus = tcpci_set_vbus;
463 tcpci->tcpc.set_cc = tcpci_set_cc;
464 tcpci->tcpc.get_cc = tcpci_get_cc;
465 tcpci->tcpc.set_polarity = tcpci_set_polarity;
466 tcpci->tcpc.set_vconn = tcpci_set_vconn;
467 tcpci->tcpc.start_drp_toggling = tcpci_start_drp_toggling;
469 tcpci->tcpc.set_pd_rx = tcpci_set_pd_rx;
470 tcpci->tcpc.set_roles = tcpci_set_roles;
471 tcpci->tcpc.pd_transmit = tcpci_pd_transmit;
473 err = tcpci_parse_config(tcpci);
477 /* Disable chip interrupts */
478 tcpci_write16(tcpci, TCPC_ALERT_MASK, 0);
480 err = devm_request_threaded_irq(tcpci->dev, client->irq, NULL,
482 IRQF_ONESHOT | IRQF_TRIGGER_LOW,
483 dev_name(tcpci->dev), tcpci);
487 tcpci->port = tcpm_register_port(tcpci->dev, &tcpci->tcpc);
488 return PTR_ERR_OR_ZERO(tcpci->port);
491 static int tcpci_remove(struct i2c_client *client)
493 struct tcpci *tcpci = i2c_get_clientdata(client);
495 tcpm_unregister_port(tcpci->port);
500 static const struct i2c_device_id tcpci_id[] = {
504 MODULE_DEVICE_TABLE(i2c, tcpci_id);
507 static const struct of_device_id tcpci_of_match[] = {
508 { .compatible = "usb,tcpci", },
511 MODULE_DEVICE_TABLE(of, tcpci_of_match);
514 static struct i2c_driver tcpci_i2c_driver = {
517 .of_match_table = of_match_ptr(tcpci_of_match),
519 .probe = tcpci_probe,
520 .remove = tcpci_remove,
521 .id_table = tcpci_id,
523 module_i2c_driver(tcpci_i2c_driver);
525 MODULE_DESCRIPTION("USB Type-C Port Controller Interface driver");
526 MODULE_LICENSE("GPL");