1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020 - 2022, Google LLC
5 * MAXIM TCPCI based TCPC driver
8 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/usb/pd.h>
14 #include <linux/usb/tcpci.h>
15 #include <linux/usb/tcpm.h>
16 #include <linux/usb/typec.h>
18 #include "tcpci_maxim.h"
20 #define PD_ACTIVITY_TIMEOUT_MS 10000
22 #define TCPC_VENDOR_ALERT 0x80
23 #define TCPC_VENDOR_USBSW_CTRL 0x93
24 #define TCPC_VENDOR_USBSW_CTRL_ENABLE_USB_DATA 0x9
25 #define TCPC_VENDOR_USBSW_CTRL_DISABLE_USB_DATA 0
27 #define TCPC_RECEIVE_BUFFER_COUNT_OFFSET 0
28 #define TCPC_RECEIVE_BUFFER_FRAME_TYPE_OFFSET 1
29 #define TCPC_RECEIVE_BUFFER_RX_BYTE_BUF_OFFSET 2
32 * LongMessage not supported, hence 32 bytes for buf to be read from RECEIVE_BUFFER.
33 * DEVICE_CAPABILITIES_2.LongMessage = 0, the value in READABLE_BYTE_COUNT reg shall be
34 * less than or equal to 31. Since, RECEIVE_BUFFER len = 31 + 1(READABLE_BYTE_COUNT).
36 #define TCPC_RECEIVE_BUFFER_LEN 32
38 #define MAX_BUCK_BOOST_SID 0x69
39 #define MAX_BUCK_BOOST_OP 0xb9
40 #define MAX_BUCK_BOOST_OFF 0
41 #define MAX_BUCK_BOOST_SOURCE 0xa
42 #define MAX_BUCK_BOOST_SINK 0x5
44 static const struct regmap_range max_tcpci_tcpci_range[] = {
45 regmap_reg_range(0x00, 0x95)
48 static const struct regmap_access_table max_tcpci_tcpci_write_table = {
49 .yes_ranges = max_tcpci_tcpci_range,
50 .n_yes_ranges = ARRAY_SIZE(max_tcpci_tcpci_range),
53 static const struct regmap_config max_tcpci_regmap_config = {
57 .wr_table = &max_tcpci_tcpci_write_table,
60 static struct max_tcpci_chip *tdata_to_max_tcpci(struct tcpci_data *tdata)
62 return container_of(tdata, struct max_tcpci_chip, data);
65 static void max_tcpci_init_regs(struct max_tcpci_chip *chip)
70 ret = max_tcpci_write16(chip, TCPC_ALERT, 0xffff);
72 dev_err(chip->dev, "Error writing to TCPC_ALERT ret:%d\n", ret);
76 ret = max_tcpci_write16(chip, TCPC_VENDOR_ALERT, 0xffff);
78 dev_err(chip->dev, "Error writing to TCPC_VENDOR_ALERT ret:%d\n", ret);
82 ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED, 0xff);
84 dev_err(chip->dev, "Unable to clear TCPC_ALERT_EXTENDED ret:%d\n", ret);
88 /* Enable VSAFE0V detection */
89 ret = max_tcpci_write8(chip, TCPC_EXTENDED_STATUS_MASK, TCPC_EXTENDED_STATUS_VSAFE0V);
91 dev_err(chip->dev, "Unable to unmask TCPC_EXTENDED_STATUS_VSAFE0V ret:%d\n", ret);
95 alert_mask = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_DISCARDED | TCPC_ALERT_TX_FAILED |
96 TCPC_ALERT_RX_HARD_RST | TCPC_ALERT_RX_STATUS | TCPC_ALERT_CC_STATUS |
97 TCPC_ALERT_VBUS_DISCNCT | TCPC_ALERT_RX_BUF_OVF | TCPC_ALERT_POWER_STATUS |
98 /* Enable Extended alert for detecting Fast Role Swap Signal */
99 TCPC_ALERT_EXTND | TCPC_ALERT_EXTENDED_STATUS;
101 ret = max_tcpci_write16(chip, TCPC_ALERT_MASK, alert_mask);
104 "Error enabling TCPC_ALERT: TCPC_ALERT_MASK write failed ret:%d\n", ret);
108 /* Enable vbus voltage monitoring and voltage alerts */
109 ret = max_tcpci_write8(chip, TCPC_POWER_CTRL, 0);
111 dev_err(chip->dev, "Error writing to TCPC_POWER_CTRL ret:%d\n", ret);
115 ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED_MASK, TCPC_SINK_FAST_ROLE_SWAP);
120 static void process_rx(struct max_tcpci_chip *chip, u16 status)
122 struct pd_message msg;
123 u8 count, frame_type, rx_buf[TCPC_RECEIVE_BUFFER_LEN];
124 int ret, payload_index;
128 * READABLE_BYTE_COUNT: Indicates the number of bytes in the RX_BUF_BYTE_x registers
129 * plus one (for the RX_BUF_FRAME_TYPE) Table 4-36.
130 * Read the count and frame type.
132 ret = regmap_raw_read(chip->data.regmap, TCPC_RX_BYTE_CNT, rx_buf, 2);
134 dev_err(chip->dev, "TCPC_RX_BYTE_CNT read failed ret:%d\n", ret);
138 count = rx_buf[TCPC_RECEIVE_BUFFER_COUNT_OFFSET];
139 frame_type = rx_buf[TCPC_RECEIVE_BUFFER_FRAME_TYPE_OFFSET];
141 if (count == 0 || frame_type != TCPC_RX_BUF_FRAME_TYPE_SOP) {
142 max_tcpci_write16(chip, TCPC_ALERT, TCPC_ALERT_RX_STATUS);
143 dev_err(chip->dev, "%s\n", count == 0 ? "error: count is 0" :
144 "error frame_type is not SOP");
148 if (count > sizeof(struct pd_message) || count + 1 > TCPC_RECEIVE_BUFFER_LEN) {
149 dev_err(chip->dev, "Invalid TCPC_RX_BYTE_CNT %d\n", count);
154 * Read count + 1 as RX_BUF_BYTE_x is hidden and can only be read through
158 ret = regmap_raw_read(chip->data.regmap, TCPC_RX_BYTE_CNT, rx_buf, count);
160 dev_err(chip->dev, "Error: TCPC_RX_BYTE_CNT read failed: %d\n", ret);
164 rx_buf_ptr = rx_buf + TCPC_RECEIVE_BUFFER_RX_BYTE_BUF_OFFSET;
165 msg.header = cpu_to_le16(*(u16 *)rx_buf_ptr);
166 rx_buf_ptr = rx_buf_ptr + sizeof(msg.header);
167 for (payload_index = 0; payload_index < pd_header_cnt_le(msg.header); payload_index++,
168 rx_buf_ptr += sizeof(msg.payload[0]))
169 msg.payload[payload_index] = cpu_to_le32(*(u32 *)rx_buf_ptr);
172 * Read complete, clear RX status alert bit.
173 * Clear overflow as well if set.
175 ret = max_tcpci_write16(chip, TCPC_ALERT, status & TCPC_ALERT_RX_BUF_OVF ?
176 TCPC_ALERT_RX_STATUS | TCPC_ALERT_RX_BUF_OVF :
177 TCPC_ALERT_RX_STATUS);
181 tcpm_pd_receive(chip->port, &msg);
184 static int max_tcpci_set_vbus(struct tcpci *tcpci, struct tcpci_data *tdata, bool source, bool sink)
186 struct max_tcpci_chip *chip = tdata_to_max_tcpci(tdata);
187 u8 buffer_source[2] = {MAX_BUCK_BOOST_OP, MAX_BUCK_BOOST_SOURCE};
188 u8 buffer_sink[2] = {MAX_BUCK_BOOST_OP, MAX_BUCK_BOOST_SINK};
189 u8 buffer_none[2] = {MAX_BUCK_BOOST_OP, MAX_BUCK_BOOST_OFF};
190 struct i2c_client *i2c = chip->client;
193 struct i2c_msg msgs[] = {
195 .addr = MAX_BUCK_BOOST_SID,
196 .flags = i2c->flags & I2C_M_TEN,
198 .buf = source ? buffer_source : sink ? buffer_sink : buffer_none,
202 if (source && sink) {
203 dev_err(chip->dev, "Both source and sink set\n");
207 ret = i2c_transfer(i2c->adapter, msgs, 1);
209 return ret < 0 ? ret : 1;
212 static void process_power_status(struct max_tcpci_chip *chip)
217 ret = max_tcpci_read8(chip, TCPC_POWER_STATUS, &pwr_status);
221 if (pwr_status == 0xff)
222 max_tcpci_init_regs(chip);
223 else if (pwr_status & TCPC_POWER_STATUS_SOURCING_VBUS)
224 tcpm_sourcing_vbus(chip->port);
226 tcpm_vbus_change(chip->port);
229 static void max_tcpci_frs_sourcing_vbus(struct tcpci *tcpci, struct tcpci_data *tdata)
232 * For Fast Role Swap case, Boost turns on autonomously without
233 * AP intervention, but, needs AP to enable source mode explicitly
234 * for AP to regain control.
236 max_tcpci_set_vbus(tcpci, tdata, true, false);
239 static void process_tx(struct max_tcpci_chip *chip, u16 status)
241 if (status & TCPC_ALERT_TX_SUCCESS)
242 tcpm_pd_transmit_complete(chip->port, TCPC_TX_SUCCESS);
243 else if (status & TCPC_ALERT_TX_DISCARDED)
244 tcpm_pd_transmit_complete(chip->port, TCPC_TX_DISCARDED);
245 else if (status & TCPC_ALERT_TX_FAILED)
246 tcpm_pd_transmit_complete(chip->port, TCPC_TX_FAILED);
248 /* Reinit regs as Hard reset sets them to default value */
249 if ((status & TCPC_ALERT_TX_SUCCESS) && (status & TCPC_ALERT_TX_FAILED))
250 max_tcpci_init_regs(chip);
253 /* Enable USB switches when partner is USB communications capable */
254 static void max_tcpci_set_partner_usb_comm_capable(struct tcpci *tcpci, struct tcpci_data *data,
257 struct max_tcpci_chip *chip = tdata_to_max_tcpci(data);
260 ret = max_tcpci_write8(chip, TCPC_VENDOR_USBSW_CTRL, capable ?
261 TCPC_VENDOR_USBSW_CTRL_ENABLE_USB_DATA :
262 TCPC_VENDOR_USBSW_CTRL_DISABLE_USB_DATA);
265 dev_err(chip->dev, "Failed to enable USB switches");
268 static irqreturn_t _max_tcpci_irq(struct max_tcpci_chip *chip, u16 status)
275 * Clear alert status for everything except RX_STATUS, which shouldn't
276 * be cleared until we have successfully retrieved message.
278 if (status & ~TCPC_ALERT_RX_STATUS) {
279 mask = status & TCPC_ALERT_RX_BUF_OVF ?
280 status & ~(TCPC_ALERT_RX_STATUS | TCPC_ALERT_RX_BUF_OVF) :
281 status & ~TCPC_ALERT_RX_STATUS;
282 ret = max_tcpci_write16(chip, TCPC_ALERT, mask);
284 dev_err(chip->dev, "ALERT clear failed\n");
289 if (status & TCPC_ALERT_RX_BUF_OVF && !(status & TCPC_ALERT_RX_STATUS)) {
290 ret = max_tcpci_write16(chip, TCPC_ALERT, (TCPC_ALERT_RX_STATUS |
291 TCPC_ALERT_RX_BUF_OVF));
293 dev_err(chip->dev, "ALERT clear failed\n");
298 if (status & TCPC_ALERT_EXTND) {
299 ret = max_tcpci_read8(chip, TCPC_ALERT_EXTENDED, ®_status);
303 ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED, reg_status);
307 if (reg_status & TCPC_SINK_FAST_ROLE_SWAP) {
308 dev_info(chip->dev, "FRS Signal\n");
309 tcpm_sink_frs(chip->port);
313 if (status & TCPC_ALERT_EXTENDED_STATUS) {
314 ret = max_tcpci_read8(chip, TCPC_EXTENDED_STATUS, (u8 *)®_status);
315 if (ret >= 0 && (reg_status & TCPC_EXTENDED_STATUS_VSAFE0V))
316 tcpm_vbus_change(chip->port);
319 if (status & TCPC_ALERT_RX_STATUS)
320 process_rx(chip, status);
322 if (status & TCPC_ALERT_VBUS_DISCNCT)
323 tcpm_vbus_change(chip->port);
325 if (status & TCPC_ALERT_CC_STATUS) {
326 if (chip->contaminant_state == DETECTED || tcpm_port_is_toggling(chip->port)) {
327 if (!max_contaminant_is_contaminant(chip, false))
328 tcpm_port_clean(chip->port);
330 tcpm_cc_change(chip->port);
334 if (status & TCPC_ALERT_POWER_STATUS)
335 process_power_status(chip);
337 if (status & TCPC_ALERT_RX_HARD_RST) {
338 tcpm_pd_hard_reset(chip->port);
339 max_tcpci_init_regs(chip);
342 if (status & TCPC_ALERT_TX_SUCCESS || status & TCPC_ALERT_TX_DISCARDED || status &
343 TCPC_ALERT_TX_FAILED)
344 process_tx(chip, status);
349 static irqreturn_t max_tcpci_irq(int irq, void *dev_id)
351 struct max_tcpci_chip *chip = dev_id;
353 irqreturn_t irq_return = IRQ_HANDLED;
359 ret = max_tcpci_read16(chip, TCPC_ALERT, &status);
361 dev_err(chip->dev, "ALERT read failed\n");
365 irq_return = _max_tcpci_irq(chip, status);
366 /* Do not return if the ALERT is already set. */
367 ret = max_tcpci_read16(chip, TCPC_ALERT, &status);
375 static irqreturn_t max_tcpci_isr(int irq, void *dev_id)
377 struct max_tcpci_chip *chip = dev_id;
379 pm_wakeup_event(chip->dev, PD_ACTIVITY_TIMEOUT_MS);
384 return IRQ_WAKE_THREAD;
387 static int max_tcpci_init_alert(struct max_tcpci_chip *chip, struct i2c_client *client)
391 ret = devm_request_threaded_irq(chip->dev, client->irq, max_tcpci_isr, max_tcpci_irq,
392 (IRQF_TRIGGER_LOW | IRQF_ONESHOT), dev_name(chip->dev),
398 enable_irq_wake(client->irq);
402 static int max_tcpci_start_toggling(struct tcpci *tcpci, struct tcpci_data *tdata,
403 enum typec_cc_status cc)
405 struct max_tcpci_chip *chip = tdata_to_max_tcpci(tdata);
407 max_tcpci_init_regs(chip);
412 static int tcpci_init(struct tcpci *tcpci, struct tcpci_data *data)
415 * Generic TCPCI overwrites the regs once this driver initializes
416 * them. Prevent this by returning -1.
421 static void max_tcpci_check_contaminant(struct tcpci *tcpci, struct tcpci_data *tdata)
423 struct max_tcpci_chip *chip = tdata_to_max_tcpci(tdata);
425 if (!max_contaminant_is_contaminant(chip, true))
426 tcpm_port_clean(chip->port);
429 static int max_tcpci_probe(struct i2c_client *client)
432 struct max_tcpci_chip *chip;
435 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
439 chip->client = client;
440 chip->data.regmap = devm_regmap_init_i2c(client, &max_tcpci_regmap_config);
441 if (IS_ERR(chip->data.regmap)) {
442 dev_err(&client->dev, "Regmap init failed\n");
443 return PTR_ERR(chip->data.regmap);
446 chip->dev = &client->dev;
447 i2c_set_clientdata(client, chip);
449 ret = max_tcpci_read8(chip, TCPC_POWER_STATUS, &power_status);
453 /* Chip level tcpci callbacks */
454 chip->data.set_vbus = max_tcpci_set_vbus;
455 chip->data.start_drp_toggling = max_tcpci_start_toggling;
456 chip->data.TX_BUF_BYTE_x_hidden = true;
457 chip->data.init = tcpci_init;
458 chip->data.frs_sourcing_vbus = max_tcpci_frs_sourcing_vbus;
459 chip->data.auto_discharge_disconnect = true;
460 chip->data.vbus_vsafe0v = true;
461 chip->data.set_partner_usb_comm_capable = max_tcpci_set_partner_usb_comm_capable;
462 chip->data.check_contaminant = max_tcpci_check_contaminant;
464 max_tcpci_init_regs(chip);
465 chip->tcpci = tcpci_register_port(chip->dev, &chip->data);
466 if (IS_ERR(chip->tcpci)) {
467 dev_err(&client->dev, "TCPCI port registration failed\n");
468 return PTR_ERR(chip->tcpci);
470 chip->port = tcpci_get_tcpm_port(chip->tcpci);
471 ret = max_tcpci_init_alert(chip, client);
475 device_init_wakeup(chip->dev, true);
479 tcpci_unregister_port(chip->tcpci);
484 static void max_tcpci_remove(struct i2c_client *client)
486 struct max_tcpci_chip *chip = i2c_get_clientdata(client);
488 if (!IS_ERR_OR_NULL(chip->tcpci))
489 tcpci_unregister_port(chip->tcpci);
492 static const struct i2c_device_id max_tcpci_id[] = {
496 MODULE_DEVICE_TABLE(i2c, max_tcpci_id);
499 static const struct of_device_id max_tcpci_of_match[] = {
500 { .compatible = "maxim,max33359", },
503 MODULE_DEVICE_TABLE(of, max_tcpci_of_match);
506 static struct i2c_driver max_tcpci_i2c_driver = {
509 .of_match_table = of_match_ptr(max_tcpci_of_match),
511 .probe = max_tcpci_probe,
512 .remove = max_tcpci_remove,
513 .id_table = max_tcpci_id,
515 module_i2c_driver(max_tcpci_i2c_driver);
517 MODULE_AUTHOR("Badhri Jagan Sridharan <badhri@google.com>");
518 MODULE_DESCRIPTION("Maxim TCPCI based USB Type-C Port Controller Interface Driver");
519 MODULE_LICENSE("GPL v2");