xref: /openbmc/linux/drivers/usb/typec/tcpm/tcpci_maxim_core.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1  // SPDX-License-Identifier: GPL-2.0+
2  /*
3   * Copyright (C) 2020 - 2022, Google LLC
4   *
5   * MAXIM TCPCI based TCPC driver
6   */
7  
8  #include <linux/interrupt.h>
9  #include <linux/i2c.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>
17  
18  #include "tcpci_maxim.h"
19  
20  #define PD_ACTIVITY_TIMEOUT_MS				10000
21  
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
26  
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
30  
31  /*
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).
35   */
36  #define TCPC_RECEIVE_BUFFER_LEN				32
37  
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
43  
44  static const struct regmap_range max_tcpci_tcpci_range[] = {
45  	regmap_reg_range(0x00, 0x95)
46  };
47  
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),
51  };
52  
53  static const struct regmap_config max_tcpci_regmap_config = {
54  	.reg_bits = 8,
55  	.val_bits = 8,
56  	.max_register = 0x95,
57  	.wr_table = &max_tcpci_tcpci_write_table,
58  };
59  
tdata_to_max_tcpci(struct tcpci_data * tdata)60  static struct max_tcpci_chip *tdata_to_max_tcpci(struct tcpci_data *tdata)
61  {
62  	return container_of(tdata, struct max_tcpci_chip, data);
63  }
64  
max_tcpci_init_regs(struct max_tcpci_chip * chip)65  static void max_tcpci_init_regs(struct max_tcpci_chip *chip)
66  {
67  	u16 alert_mask = 0;
68  	int ret;
69  
70  	ret = max_tcpci_write16(chip, TCPC_ALERT, 0xffff);
71  	if (ret < 0) {
72  		dev_err(chip->dev, "Error writing to TCPC_ALERT ret:%d\n", ret);
73  		return;
74  	}
75  
76  	ret = max_tcpci_write16(chip, TCPC_VENDOR_ALERT, 0xffff);
77  	if (ret < 0) {
78  		dev_err(chip->dev, "Error writing to TCPC_VENDOR_ALERT ret:%d\n", ret);
79  		return;
80  	}
81  
82  	ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED, 0xff);
83  	if (ret < 0) {
84  		dev_err(chip->dev, "Unable to clear TCPC_ALERT_EXTENDED ret:%d\n", ret);
85  		return;
86  	}
87  
88  	/* Enable VSAFE0V detection */
89  	ret = max_tcpci_write8(chip, TCPC_EXTENDED_STATUS_MASK, TCPC_EXTENDED_STATUS_VSAFE0V);
90  	if (ret < 0) {
91  		dev_err(chip->dev, "Unable to unmask TCPC_EXTENDED_STATUS_VSAFE0V ret:%d\n", ret);
92  		return;
93  	}
94  
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;
100  
101  	ret = max_tcpci_write16(chip, TCPC_ALERT_MASK, alert_mask);
102  	if (ret < 0) {
103  		dev_err(chip->dev,
104  			"Error enabling TCPC_ALERT: TCPC_ALERT_MASK write failed ret:%d\n", ret);
105  		return;
106  	}
107  
108  	/* Enable vbus voltage monitoring and voltage alerts */
109  	ret = max_tcpci_write8(chip, TCPC_POWER_CTRL, 0);
110  	if (ret < 0) {
111  		dev_err(chip->dev, "Error writing to TCPC_POWER_CTRL ret:%d\n", ret);
112  		return;
113  	}
114  
115  	ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED_MASK, TCPC_SINK_FAST_ROLE_SWAP);
116  	if (ret < 0)
117  		return;
118  }
119  
process_rx(struct max_tcpci_chip * chip,u16 status)120  static void process_rx(struct max_tcpci_chip *chip, u16 status)
121  {
122  	struct pd_message msg;
123  	u8 count, frame_type, rx_buf[TCPC_RECEIVE_BUFFER_LEN];
124  	int ret, payload_index;
125  	u8 *rx_buf_ptr;
126  
127  	/*
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.
131  	 */
132  	ret = regmap_raw_read(chip->data.regmap, TCPC_RX_BYTE_CNT, rx_buf, 2);
133  	if (ret < 0) {
134  		dev_err(chip->dev, "TCPC_RX_BYTE_CNT read failed ret:%d\n", ret);
135  		return;
136  	}
137  
138  	count = rx_buf[TCPC_RECEIVE_BUFFER_COUNT_OFFSET];
139  	frame_type = rx_buf[TCPC_RECEIVE_BUFFER_FRAME_TYPE_OFFSET];
140  
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");
145  		return;
146  	}
147  
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);
150  		return;
151  	}
152  
153  	/*
154  	 * Read count + 1 as RX_BUF_BYTE_x is hidden and can only be read through
155  	 * TCPC_RX_BYTE_CNT
156  	 */
157  	count += 1;
158  	ret = regmap_raw_read(chip->data.regmap, TCPC_RX_BYTE_CNT, rx_buf, count);
159  	if (ret < 0) {
160  		dev_err(chip->dev, "Error: TCPC_RX_BYTE_CNT read failed: %d\n", ret);
161  		return;
162  	}
163  
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);
170  
171  	/*
172  	 * Read complete, clear RX status alert bit.
173  	 * Clear overflow as well if set.
174  	 */
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);
178  	if (ret < 0)
179  		return;
180  
181  	tcpm_pd_receive(chip->port, &msg);
182  }
183  
max_tcpci_set_vbus(struct tcpci * tcpci,struct tcpci_data * tdata,bool source,bool sink)184  static int max_tcpci_set_vbus(struct tcpci *tcpci, struct tcpci_data *tdata, bool source, bool sink)
185  {
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;
191  	int ret;
192  
193  	struct i2c_msg msgs[] = {
194  		{
195  			.addr = MAX_BUCK_BOOST_SID,
196  			.flags = i2c->flags & I2C_M_TEN,
197  			.len = 2,
198  			.buf = source ? buffer_source : sink ? buffer_sink : buffer_none,
199  		},
200  	};
201  
202  	if (source && sink) {
203  		dev_err(chip->dev, "Both source and sink set\n");
204  		return -EINVAL;
205  	}
206  
207  	ret = i2c_transfer(i2c->adapter, msgs, 1);
208  
209  	return  ret < 0 ? ret : 1;
210  }
211  
process_power_status(struct max_tcpci_chip * chip)212  static void process_power_status(struct max_tcpci_chip *chip)
213  {
214  	u8 pwr_status;
215  	int ret;
216  
217  	ret = max_tcpci_read8(chip, TCPC_POWER_STATUS, &pwr_status);
218  	if (ret < 0)
219  		return;
220  
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);
225  	else
226  		tcpm_vbus_change(chip->port);
227  }
228  
max_tcpci_frs_sourcing_vbus(struct tcpci * tcpci,struct tcpci_data * tdata)229  static void max_tcpci_frs_sourcing_vbus(struct tcpci *tcpci, struct tcpci_data *tdata)
230  {
231  	/*
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.
235  	 */
236  	max_tcpci_set_vbus(tcpci, tdata, true, false);
237  }
238  
process_tx(struct max_tcpci_chip * chip,u16 status)239  static void process_tx(struct max_tcpci_chip *chip, u16 status)
240  {
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);
247  
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);
251  }
252  
253  /* Enable USB switches when partner is USB communications capable */
max_tcpci_set_partner_usb_comm_capable(struct tcpci * tcpci,struct tcpci_data * data,bool capable)254  static void max_tcpci_set_partner_usb_comm_capable(struct tcpci *tcpci, struct tcpci_data *data,
255  						   bool capable)
256  {
257  	struct max_tcpci_chip *chip = tdata_to_max_tcpci(data);
258  	int ret;
259  
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);
263  
264  	if (ret < 0)
265  		dev_err(chip->dev, "Failed to enable USB switches");
266  }
267  
_max_tcpci_irq(struct max_tcpci_chip * chip,u16 status)268  static irqreturn_t _max_tcpci_irq(struct max_tcpci_chip *chip, u16 status)
269  {
270  	u16 mask;
271  	int ret;
272  	u8 reg_status;
273  
274  	/*
275  	 * Clear alert status for everything except RX_STATUS, which shouldn't
276  	 * be cleared until we have successfully retrieved message.
277  	 */
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);
283  		if (ret < 0) {
284  			dev_err(chip->dev, "ALERT clear failed\n");
285  			return ret;
286  		}
287  	}
288  
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));
292  		if (ret < 0) {
293  			dev_err(chip->dev, "ALERT clear failed\n");
294  			return ret;
295  		}
296  	}
297  
298  	if (status & TCPC_ALERT_EXTND) {
299  		ret = max_tcpci_read8(chip, TCPC_ALERT_EXTENDED, &reg_status);
300  		if (ret < 0)
301  			return ret;
302  
303  		ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED, reg_status);
304  		if (ret < 0)
305  			return ret;
306  
307  		if (reg_status & TCPC_SINK_FAST_ROLE_SWAP) {
308  			dev_info(chip->dev, "FRS Signal\n");
309  			tcpm_sink_frs(chip->port);
310  		}
311  	}
312  
313  	if (status & TCPC_ALERT_EXTENDED_STATUS) {
314  		ret = max_tcpci_read8(chip, TCPC_EXTENDED_STATUS, (u8 *)&reg_status);
315  		if (ret >= 0 && (reg_status & TCPC_EXTENDED_STATUS_VSAFE0V))
316  			tcpm_vbus_change(chip->port);
317  	}
318  
319  	if (status & TCPC_ALERT_RX_STATUS)
320  		process_rx(chip, status);
321  
322  	if (status & TCPC_ALERT_VBUS_DISCNCT)
323  		tcpm_vbus_change(chip->port);
324  
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);
329  		} else {
330  			tcpm_cc_change(chip->port);
331  		}
332  	}
333  
334  	if (status & TCPC_ALERT_POWER_STATUS)
335  		process_power_status(chip);
336  
337  	if (status & TCPC_ALERT_RX_HARD_RST) {
338  		tcpm_pd_hard_reset(chip->port);
339  		max_tcpci_init_regs(chip);
340  	}
341  
342  	if (status & TCPC_ALERT_TX_SUCCESS || status & TCPC_ALERT_TX_DISCARDED || status &
343  	    TCPC_ALERT_TX_FAILED)
344  		process_tx(chip, status);
345  
346  	return IRQ_HANDLED;
347  }
348  
max_tcpci_irq(int irq,void * dev_id)349  static irqreturn_t max_tcpci_irq(int irq, void *dev_id)
350  {
351  	struct max_tcpci_chip *chip = dev_id;
352  	u16 status;
353  	irqreturn_t irq_return = IRQ_HANDLED;
354  	int ret;
355  
356  	if (!chip->port)
357  		return IRQ_HANDLED;
358  
359  	ret = max_tcpci_read16(chip, TCPC_ALERT, &status);
360  	if (ret < 0) {
361  		dev_err(chip->dev, "ALERT read failed\n");
362  		return ret;
363  	}
364  	while (status) {
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);
368  		if (ret < 0)
369  			break;
370  	}
371  
372  	return irq_return;
373  }
374  
max_tcpci_isr(int irq,void * dev_id)375  static irqreturn_t max_tcpci_isr(int irq, void *dev_id)
376  {
377  	struct max_tcpci_chip *chip = dev_id;
378  
379  	pm_wakeup_event(chip->dev, PD_ACTIVITY_TIMEOUT_MS);
380  
381  	if (!chip->port)
382  		return IRQ_HANDLED;
383  
384  	return IRQ_WAKE_THREAD;
385  }
386  
max_tcpci_init_alert(struct max_tcpci_chip * chip,struct i2c_client * client)387  static int max_tcpci_init_alert(struct max_tcpci_chip *chip, struct i2c_client *client)
388  {
389  	int ret;
390  
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),
393  					chip);
394  
395  	if (ret < 0)
396  		return ret;
397  
398  	enable_irq_wake(client->irq);
399  	return 0;
400  }
401  
max_tcpci_start_toggling(struct tcpci * tcpci,struct tcpci_data * tdata,enum typec_cc_status cc)402  static int max_tcpci_start_toggling(struct tcpci *tcpci, struct tcpci_data *tdata,
403  				    enum typec_cc_status cc)
404  {
405  	struct max_tcpci_chip *chip = tdata_to_max_tcpci(tdata);
406  
407  	max_tcpci_init_regs(chip);
408  
409  	return 0;
410  }
411  
tcpci_init(struct tcpci * tcpci,struct tcpci_data * data)412  static int tcpci_init(struct tcpci *tcpci, struct tcpci_data *data)
413  {
414  	/*
415  	 * Generic TCPCI overwrites the regs once this driver initializes
416  	 * them. Prevent this by returning -1.
417  	 */
418  	return -1;
419  }
420  
max_tcpci_check_contaminant(struct tcpci * tcpci,struct tcpci_data * tdata)421  static void max_tcpci_check_contaminant(struct tcpci *tcpci, struct tcpci_data *tdata)
422  {
423  	struct max_tcpci_chip *chip = tdata_to_max_tcpci(tdata);
424  
425  	if (!max_contaminant_is_contaminant(chip, true))
426  		tcpm_port_clean(chip->port);
427  }
428  
max_tcpci_probe(struct i2c_client * client)429  static int max_tcpci_probe(struct i2c_client *client)
430  {
431  	int ret;
432  	struct max_tcpci_chip *chip;
433  	u8 power_status;
434  
435  	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
436  	if (!chip)
437  		return -ENOMEM;
438  
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);
444  	}
445  
446  	chip->dev = &client->dev;
447  	i2c_set_clientdata(client, chip);
448  
449  	ret = max_tcpci_read8(chip, TCPC_POWER_STATUS, &power_status);
450  	if (ret < 0)
451  		return ret;
452  
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;
463  
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);
469  	}
470  	chip->port = tcpci_get_tcpm_port(chip->tcpci);
471  	ret = max_tcpci_init_alert(chip, client);
472  	if (ret < 0)
473  		goto unreg_port;
474  
475  	device_init_wakeup(chip->dev, true);
476  	return 0;
477  
478  unreg_port:
479  	tcpci_unregister_port(chip->tcpci);
480  
481  	return ret;
482  }
483  
max_tcpci_remove(struct i2c_client * client)484  static void max_tcpci_remove(struct i2c_client *client)
485  {
486  	struct max_tcpci_chip *chip = i2c_get_clientdata(client);
487  
488  	if (!IS_ERR_OR_NULL(chip->tcpci))
489  		tcpci_unregister_port(chip->tcpci);
490  }
491  
492  static const struct i2c_device_id max_tcpci_id[] = {
493  	{ "maxtcpc", 0 },
494  	{ }
495  };
496  MODULE_DEVICE_TABLE(i2c, max_tcpci_id);
497  
498  #ifdef CONFIG_OF
499  static const struct of_device_id max_tcpci_of_match[] = {
500  	{ .compatible = "maxim,max33359", },
501  	{},
502  };
503  MODULE_DEVICE_TABLE(of, max_tcpci_of_match);
504  #endif
505  
506  static struct i2c_driver max_tcpci_i2c_driver = {
507  	.driver = {
508  		.name = "maxtcpc",
509  		.of_match_table = of_match_ptr(max_tcpci_of_match),
510  	},
511  	.probe = max_tcpci_probe,
512  	.remove = max_tcpci_remove,
513  	.id_table = max_tcpci_id,
514  };
515  module_i2c_driver(max_tcpci_i2c_driver);
516  
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");
520