1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2020, Google LLC
4 *
5 * MAXIM TCPCI based TCPC driver
6 */
7
8 #include <linux/gpio.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/interrupt.h>
11 #include <linux/i2c.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of_gpio.h>
15 #include <linux/regmap.h>
16 #include <linux/usb/pd.h>
17 #include <linux/usb/tcpm.h>
18 #include <linux/usb/typec.h>
19
20 #include "tcpci.h"
21
22 #define PD_ACTIVITY_TIMEOUT_MS 10000
23
24 #define TCPC_VENDOR_ALERT 0x80
25 #define TCPC_VENDOR_USBSW_CTRL 0x93
26 #define TCPC_VENDOR_USBSW_CTRL_ENABLE_USB_DATA 0x9
27 #define TCPC_VENDOR_USBSW_CTRL_DISABLE_USB_DATA 0
28
29 #define TCPC_RECEIVE_BUFFER_COUNT_OFFSET 0
30 #define TCPC_RECEIVE_BUFFER_FRAME_TYPE_OFFSET 1
31 #define TCPC_RECEIVE_BUFFER_RX_BYTE_BUF_OFFSET 2
32
33 /*
34 * LongMessage not supported, hence 32 bytes for buf to be read from RECEIVE_BUFFER.
35 * DEVICE_CAPABILITIES_2.LongMessage = 0, the value in READABLE_BYTE_COUNT reg shall be
36 * less than or equal to 31. Since, RECEIVE_BUFFER len = 31 + 1(READABLE_BYTE_COUNT).
37 */
38 #define TCPC_RECEIVE_BUFFER_LEN 32
39
40 #define MAX_BUCK_BOOST_SID 0x69
41 #define MAX_BUCK_BOOST_OP 0xb9
42 #define MAX_BUCK_BOOST_OFF 0
43 #define MAX_BUCK_BOOST_SOURCE 0xa
44 #define MAX_BUCK_BOOST_SINK 0x5
45
46 struct max_tcpci_chip {
47 struct tcpci_data data;
48 struct tcpci *tcpci;
49 struct device *dev;
50 struct i2c_client *client;
51 struct tcpm_port *port;
52 };
53
54 static const struct regmap_range max_tcpci_tcpci_range[] = {
55 regmap_reg_range(0x00, 0x95)
56 };
57
58 const struct regmap_access_table max_tcpci_tcpci_write_table = {
59 .yes_ranges = max_tcpci_tcpci_range,
60 .n_yes_ranges = ARRAY_SIZE(max_tcpci_tcpci_range),
61 };
62
63 static const struct regmap_config max_tcpci_regmap_config = {
64 .reg_bits = 8,
65 .val_bits = 8,
66 .max_register = 0x95,
67 .wr_table = &max_tcpci_tcpci_write_table,
68 };
69
tdata_to_max_tcpci(struct tcpci_data * tdata)70 static struct max_tcpci_chip *tdata_to_max_tcpci(struct tcpci_data *tdata)
71 {
72 return container_of(tdata, struct max_tcpci_chip, data);
73 }
74
max_tcpci_read16(struct max_tcpci_chip * chip,unsigned int reg,u16 * val)75 static int max_tcpci_read16(struct max_tcpci_chip *chip, unsigned int reg, u16 *val)
76 {
77 return regmap_raw_read(chip->data.regmap, reg, val, sizeof(u16));
78 }
79
max_tcpci_write16(struct max_tcpci_chip * chip,unsigned int reg,u16 val)80 static int max_tcpci_write16(struct max_tcpci_chip *chip, unsigned int reg, u16 val)
81 {
82 return regmap_raw_write(chip->data.regmap, reg, &val, sizeof(u16));
83 }
84
max_tcpci_read8(struct max_tcpci_chip * chip,unsigned int reg,u8 * val)85 static int max_tcpci_read8(struct max_tcpci_chip *chip, unsigned int reg, u8 *val)
86 {
87 return regmap_raw_read(chip->data.regmap, reg, val, sizeof(u8));
88 }
89
max_tcpci_write8(struct max_tcpci_chip * chip,unsigned int reg,u8 val)90 static int max_tcpci_write8(struct max_tcpci_chip *chip, unsigned int reg, u8 val)
91 {
92 return regmap_raw_write(chip->data.regmap, reg, &val, sizeof(u8));
93 }
94
max_tcpci_init_regs(struct max_tcpci_chip * chip)95 static void max_tcpci_init_regs(struct max_tcpci_chip *chip)
96 {
97 u16 alert_mask = 0;
98 int ret;
99
100 ret = max_tcpci_write16(chip, TCPC_ALERT, 0xffff);
101 if (ret < 0) {
102 dev_err(chip->dev, "Error writing to TCPC_ALERT ret:%d\n", ret);
103 return;
104 }
105
106 ret = max_tcpci_write16(chip, TCPC_VENDOR_ALERT, 0xffff);
107 if (ret < 0) {
108 dev_err(chip->dev, "Error writing to TCPC_VENDOR_ALERT ret:%d\n", ret);
109 return;
110 }
111
112 ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED, 0xff);
113 if (ret < 0) {
114 dev_err(chip->dev, "Unable to clear TCPC_ALERT_EXTENDED ret:%d\n", ret);
115 return;
116 }
117
118 /* Enable VSAFE0V detection */
119 ret = max_tcpci_write8(chip, TCPC_EXTENDED_STATUS_MASK, TCPC_EXTENDED_STATUS_VSAFE0V);
120 if (ret < 0) {
121 dev_err(chip->dev, "Unable to unmask TCPC_EXTENDED_STATUS_VSAFE0V ret:%d\n", ret);
122 return;
123 }
124
125 alert_mask = TCPC_ALERT_TX_SUCCESS | TCPC_ALERT_TX_DISCARDED | TCPC_ALERT_TX_FAILED |
126 TCPC_ALERT_RX_HARD_RST | TCPC_ALERT_RX_STATUS | TCPC_ALERT_CC_STATUS |
127 TCPC_ALERT_VBUS_DISCNCT | TCPC_ALERT_RX_BUF_OVF | TCPC_ALERT_POWER_STATUS |
128 /* Enable Extended alert for detecting Fast Role Swap Signal */
129 TCPC_ALERT_EXTND | TCPC_ALERT_EXTENDED_STATUS;
130
131 ret = max_tcpci_write16(chip, TCPC_ALERT_MASK, alert_mask);
132 if (ret < 0) {
133 dev_err(chip->dev,
134 "Error enabling TCPC_ALERT: TCPC_ALERT_MASK write failed ret:%d\n", ret);
135 return;
136 }
137
138 /* Enable vbus voltage monitoring and voltage alerts */
139 ret = max_tcpci_write8(chip, TCPC_POWER_CTRL, 0);
140 if (ret < 0) {
141 dev_err(chip->dev, "Error writing to TCPC_POWER_CTRL ret:%d\n", ret);
142 return;
143 }
144
145 ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED_MASK, TCPC_SINK_FAST_ROLE_SWAP);
146 if (ret < 0)
147 return;
148 }
149
process_rx(struct max_tcpci_chip * chip,u16 status)150 static void process_rx(struct max_tcpci_chip *chip, u16 status)
151 {
152 struct pd_message msg;
153 u8 count, frame_type, rx_buf[TCPC_RECEIVE_BUFFER_LEN];
154 int ret, payload_index;
155 u8 *rx_buf_ptr;
156
157 /*
158 * READABLE_BYTE_COUNT: Indicates the number of bytes in the RX_BUF_BYTE_x registers
159 * plus one (for the RX_BUF_FRAME_TYPE) Table 4-36.
160 * Read the count and frame type.
161 */
162 ret = regmap_raw_read(chip->data.regmap, TCPC_RX_BYTE_CNT, rx_buf, 2);
163 if (ret < 0) {
164 dev_err(chip->dev, "TCPC_RX_BYTE_CNT read failed ret:%d", ret);
165 return;
166 }
167
168 count = rx_buf[TCPC_RECEIVE_BUFFER_COUNT_OFFSET];
169 frame_type = rx_buf[TCPC_RECEIVE_BUFFER_FRAME_TYPE_OFFSET];
170
171 if (count == 0 || frame_type != TCPC_RX_BUF_FRAME_TYPE_SOP) {
172 max_tcpci_write16(chip, TCPC_ALERT, TCPC_ALERT_RX_STATUS);
173 dev_err(chip->dev, "%s", count == 0 ? "error: count is 0" :
174 "error frame_type is not SOP");
175 return;
176 }
177
178 if (count > sizeof(struct pd_message) || count + 1 > TCPC_RECEIVE_BUFFER_LEN) {
179 dev_err(chip->dev, "Invalid TCPC_RX_BYTE_CNT %d", count);
180 return;
181 }
182
183 /*
184 * Read count + 1 as RX_BUF_BYTE_x is hidden and can only be read through
185 * TCPC_RX_BYTE_CNT
186 */
187 count += 1;
188 ret = regmap_raw_read(chip->data.regmap, TCPC_RX_BYTE_CNT, rx_buf, count);
189 if (ret < 0) {
190 dev_err(chip->dev, "Error: TCPC_RX_BYTE_CNT read failed: %d", ret);
191 return;
192 }
193
194 rx_buf_ptr = rx_buf + TCPC_RECEIVE_BUFFER_RX_BYTE_BUF_OFFSET;
195 msg.header = cpu_to_le16(*(u16 *)rx_buf_ptr);
196 rx_buf_ptr = rx_buf_ptr + sizeof(msg.header);
197 for (payload_index = 0; payload_index < pd_header_cnt_le(msg.header); payload_index++,
198 rx_buf_ptr += sizeof(msg.payload[0]))
199 msg.payload[payload_index] = cpu_to_le32(*(u32 *)rx_buf_ptr);
200
201 /*
202 * Read complete, clear RX status alert bit.
203 * Clear overflow as well if set.
204 */
205 ret = max_tcpci_write16(chip, TCPC_ALERT, status & TCPC_ALERT_RX_BUF_OVF ?
206 TCPC_ALERT_RX_STATUS | TCPC_ALERT_RX_BUF_OVF :
207 TCPC_ALERT_RX_STATUS);
208 if (ret < 0)
209 return;
210
211 tcpm_pd_receive(chip->port, &msg);
212 }
213
max_tcpci_set_vbus(struct tcpci * tcpci,struct tcpci_data * tdata,bool source,bool sink)214 static int max_tcpci_set_vbus(struct tcpci *tcpci, struct tcpci_data *tdata, bool source, bool sink)
215 {
216 struct max_tcpci_chip *chip = tdata_to_max_tcpci(tdata);
217 u8 buffer_source[2] = {MAX_BUCK_BOOST_OP, MAX_BUCK_BOOST_SOURCE};
218 u8 buffer_sink[2] = {MAX_BUCK_BOOST_OP, MAX_BUCK_BOOST_SINK};
219 u8 buffer_none[2] = {MAX_BUCK_BOOST_OP, MAX_BUCK_BOOST_OFF};
220 struct i2c_client *i2c = chip->client;
221 int ret;
222
223 struct i2c_msg msgs[] = {
224 {
225 .addr = MAX_BUCK_BOOST_SID,
226 .flags = i2c->flags & I2C_M_TEN,
227 .len = 2,
228 .buf = source ? buffer_source : sink ? buffer_sink : buffer_none,
229 },
230 };
231
232 if (source && sink) {
233 dev_err(chip->dev, "Both source and sink set\n");
234 return -EINVAL;
235 }
236
237 ret = i2c_transfer(i2c->adapter, msgs, 1);
238
239 return ret < 0 ? ret : 1;
240 }
241
process_power_status(struct max_tcpci_chip * chip)242 static void process_power_status(struct max_tcpci_chip *chip)
243 {
244 u8 pwr_status;
245 int ret;
246
247 ret = max_tcpci_read8(chip, TCPC_POWER_STATUS, &pwr_status);
248 if (ret < 0)
249 return;
250
251 if (pwr_status == 0xff)
252 max_tcpci_init_regs(chip);
253 else if (pwr_status & TCPC_POWER_STATUS_SOURCING_VBUS)
254 tcpm_sourcing_vbus(chip->port);
255 else
256 tcpm_vbus_change(chip->port);
257 }
258
max_tcpci_frs_sourcing_vbus(struct tcpci * tcpci,struct tcpci_data * tdata)259 static void max_tcpci_frs_sourcing_vbus(struct tcpci *tcpci, struct tcpci_data *tdata)
260 {
261 /*
262 * For Fast Role Swap case, Boost turns on autonomously without
263 * AP intervention, but, needs AP to enable source mode explicitly
264 * for AP to regain control.
265 */
266 max_tcpci_set_vbus(tcpci, tdata, true, false);
267 }
268
process_tx(struct max_tcpci_chip * chip,u16 status)269 static void process_tx(struct max_tcpci_chip *chip, u16 status)
270 {
271 if (status & TCPC_ALERT_TX_SUCCESS)
272 tcpm_pd_transmit_complete(chip->port, TCPC_TX_SUCCESS);
273 else if (status & TCPC_ALERT_TX_DISCARDED)
274 tcpm_pd_transmit_complete(chip->port, TCPC_TX_DISCARDED);
275 else if (status & TCPC_ALERT_TX_FAILED)
276 tcpm_pd_transmit_complete(chip->port, TCPC_TX_FAILED);
277
278 /* Reinit regs as Hard reset sets them to default value */
279 if ((status & TCPC_ALERT_TX_SUCCESS) && (status & TCPC_ALERT_TX_FAILED))
280 max_tcpci_init_regs(chip);
281 }
282
283 /* 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)284 static void max_tcpci_set_partner_usb_comm_capable(struct tcpci *tcpci, struct tcpci_data *data,
285 bool capable)
286 {
287 struct max_tcpci_chip *chip = tdata_to_max_tcpci(data);
288 int ret;
289
290 ret = max_tcpci_write8(chip, TCPC_VENDOR_USBSW_CTRL, capable ?
291 TCPC_VENDOR_USBSW_CTRL_ENABLE_USB_DATA :
292 TCPC_VENDOR_USBSW_CTRL_DISABLE_USB_DATA);
293
294 if (ret < 0)
295 dev_err(chip->dev, "Failed to enable USB switches");
296 }
297
_max_tcpci_irq(struct max_tcpci_chip * chip,u16 status)298 static irqreturn_t _max_tcpci_irq(struct max_tcpci_chip *chip, u16 status)
299 {
300 u16 mask;
301 int ret;
302 u8 reg_status;
303
304 /*
305 * Clear alert status for everything except RX_STATUS, which shouldn't
306 * be cleared until we have successfully retrieved message.
307 */
308 if (status & ~TCPC_ALERT_RX_STATUS) {
309 mask = status & TCPC_ALERT_RX_BUF_OVF ?
310 status & ~(TCPC_ALERT_RX_STATUS | TCPC_ALERT_RX_BUF_OVF) :
311 status & ~TCPC_ALERT_RX_STATUS;
312 ret = max_tcpci_write16(chip, TCPC_ALERT, mask);
313 if (ret < 0) {
314 dev_err(chip->dev, "ALERT clear failed\n");
315 return ret;
316 }
317 }
318
319 if (status & TCPC_ALERT_RX_BUF_OVF && !(status & TCPC_ALERT_RX_STATUS)) {
320 ret = max_tcpci_write16(chip, TCPC_ALERT, (TCPC_ALERT_RX_STATUS |
321 TCPC_ALERT_RX_BUF_OVF));
322 if (ret < 0) {
323 dev_err(chip->dev, "ALERT clear failed\n");
324 return ret;
325 }
326 }
327
328 if (status & TCPC_ALERT_EXTND) {
329 ret = max_tcpci_read8(chip, TCPC_ALERT_EXTENDED, ®_status);
330 if (ret < 0)
331 return ret;
332
333 ret = max_tcpci_write8(chip, TCPC_ALERT_EXTENDED, reg_status);
334 if (ret < 0)
335 return ret;
336
337 if (reg_status & TCPC_SINK_FAST_ROLE_SWAP) {
338 dev_info(chip->dev, "FRS Signal");
339 tcpm_sink_frs(chip->port);
340 }
341 }
342
343 if (status & TCPC_ALERT_EXTENDED_STATUS) {
344 ret = max_tcpci_read8(chip, TCPC_EXTENDED_STATUS, (u8 *)®_status);
345 if (ret >= 0 && (reg_status & TCPC_EXTENDED_STATUS_VSAFE0V))
346 tcpm_vbus_change(chip->port);
347 }
348
349 if (status & TCPC_ALERT_RX_STATUS)
350 process_rx(chip, status);
351
352 if (status & TCPC_ALERT_VBUS_DISCNCT)
353 tcpm_vbus_change(chip->port);
354
355 if (status & TCPC_ALERT_CC_STATUS)
356 tcpm_cc_change(chip->port);
357
358 if (status & TCPC_ALERT_POWER_STATUS)
359 process_power_status(chip);
360
361 if (status & TCPC_ALERT_RX_HARD_RST) {
362 tcpm_pd_hard_reset(chip->port);
363 max_tcpci_init_regs(chip);
364 }
365
366 if (status & TCPC_ALERT_TX_SUCCESS || status & TCPC_ALERT_TX_DISCARDED || status &
367 TCPC_ALERT_TX_FAILED)
368 process_tx(chip, status);
369
370 return IRQ_HANDLED;
371 }
372
max_tcpci_irq(int irq,void * dev_id)373 static irqreturn_t max_tcpci_irq(int irq, void *dev_id)
374 {
375 struct max_tcpci_chip *chip = dev_id;
376 u16 status;
377 irqreturn_t irq_return = IRQ_HANDLED;
378 int ret;
379
380 if (!chip->port)
381 return IRQ_HANDLED;
382
383 ret = max_tcpci_read16(chip, TCPC_ALERT, &status);
384 if (ret < 0) {
385 dev_err(chip->dev, "ALERT read failed\n");
386 return ret;
387 }
388 while (status) {
389 irq_return = _max_tcpci_irq(chip, status);
390 /* Do not return if the ALERT is already set. */
391 ret = max_tcpci_read16(chip, TCPC_ALERT, &status);
392 if (ret < 0)
393 break;
394 }
395
396 return irq_return;
397 }
398
max_tcpci_isr(int irq,void * dev_id)399 static irqreturn_t max_tcpci_isr(int irq, void *dev_id)
400 {
401 struct max_tcpci_chip *chip = dev_id;
402
403 pm_wakeup_event(chip->dev, PD_ACTIVITY_TIMEOUT_MS);
404
405 if (!chip->port)
406 return IRQ_HANDLED;
407
408 return IRQ_WAKE_THREAD;
409 }
410
max_tcpci_init_alert(struct max_tcpci_chip * chip,struct i2c_client * client)411 static int max_tcpci_init_alert(struct max_tcpci_chip *chip, struct i2c_client *client)
412 {
413 int ret;
414
415 ret = devm_request_threaded_irq(chip->dev, client->irq, max_tcpci_isr, max_tcpci_irq,
416 (IRQF_TRIGGER_LOW | IRQF_ONESHOT), dev_name(chip->dev),
417 chip);
418
419 if (ret < 0)
420 return ret;
421
422 enable_irq_wake(client->irq);
423 return 0;
424 }
425
max_tcpci_start_toggling(struct tcpci * tcpci,struct tcpci_data * tdata,enum typec_cc_status cc)426 static int max_tcpci_start_toggling(struct tcpci *tcpci, struct tcpci_data *tdata,
427 enum typec_cc_status cc)
428 {
429 struct max_tcpci_chip *chip = tdata_to_max_tcpci(tdata);
430
431 max_tcpci_init_regs(chip);
432
433 return 0;
434 }
435
tcpci_init(struct tcpci * tcpci,struct tcpci_data * data)436 static int tcpci_init(struct tcpci *tcpci, struct tcpci_data *data)
437 {
438 /*
439 * Generic TCPCI overwrites the regs once this driver initializes
440 * them. Prevent this by returning -1.
441 */
442 return -1;
443 }
444
max_tcpci_probe(struct i2c_client * client,const struct i2c_device_id * i2c_id)445 static int max_tcpci_probe(struct i2c_client *client, const struct i2c_device_id *i2c_id)
446 {
447 int ret;
448 struct max_tcpci_chip *chip;
449 u8 power_status;
450
451 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
452 if (!chip)
453 return -ENOMEM;
454
455 chip->client = client;
456 chip->data.regmap = devm_regmap_init_i2c(client, &max_tcpci_regmap_config);
457 if (IS_ERR(chip->data.regmap)) {
458 dev_err(&client->dev, "Regmap init failed\n");
459 return PTR_ERR(chip->data.regmap);
460 }
461
462 chip->dev = &client->dev;
463 i2c_set_clientdata(client, chip);
464
465 ret = max_tcpci_read8(chip, TCPC_POWER_STATUS, &power_status);
466 if (ret < 0)
467 return ret;
468
469 /* Chip level tcpci callbacks */
470 chip->data.set_vbus = max_tcpci_set_vbus;
471 chip->data.start_drp_toggling = max_tcpci_start_toggling;
472 chip->data.TX_BUF_BYTE_x_hidden = true;
473 chip->data.init = tcpci_init;
474 chip->data.frs_sourcing_vbus = max_tcpci_frs_sourcing_vbus;
475 chip->data.auto_discharge_disconnect = true;
476 chip->data.vbus_vsafe0v = true;
477 chip->data.set_partner_usb_comm_capable = max_tcpci_set_partner_usb_comm_capable;
478
479 max_tcpci_init_regs(chip);
480 chip->tcpci = tcpci_register_port(chip->dev, &chip->data);
481 if (IS_ERR(chip->tcpci)) {
482 dev_err(&client->dev, "TCPCI port registration failed");
483 ret = PTR_ERR(chip->tcpci);
484 return PTR_ERR(chip->tcpci);
485 }
486 chip->port = tcpci_get_tcpm_port(chip->tcpci);
487 ret = max_tcpci_init_alert(chip, client);
488 if (ret < 0)
489 goto unreg_port;
490
491 device_init_wakeup(chip->dev, true);
492 return 0;
493
494 unreg_port:
495 tcpci_unregister_port(chip->tcpci);
496
497 return ret;
498 }
499
max_tcpci_remove(struct i2c_client * client)500 static int max_tcpci_remove(struct i2c_client *client)
501 {
502 struct max_tcpci_chip *chip = i2c_get_clientdata(client);
503
504 if (!IS_ERR_OR_NULL(chip->tcpci))
505 tcpci_unregister_port(chip->tcpci);
506
507 return 0;
508 }
509
510 static const struct i2c_device_id max_tcpci_id[] = {
511 { "maxtcpc", 0 },
512 { }
513 };
514 MODULE_DEVICE_TABLE(i2c, max_tcpci_id);
515
516 #ifdef CONFIG_OF
517 static const struct of_device_id max_tcpci_of_match[] = {
518 { .compatible = "maxim,max33359", },
519 {},
520 };
521 MODULE_DEVICE_TABLE(of, max_tcpci_of_match);
522 #endif
523
524 static struct i2c_driver max_tcpci_i2c_driver = {
525 .driver = {
526 .name = "maxtcpc",
527 .of_match_table = of_match_ptr(max_tcpci_of_match),
528 },
529 .probe = max_tcpci_probe,
530 .remove = max_tcpci_remove,
531 .id_table = max_tcpci_id,
532 };
533 module_i2c_driver(max_tcpci_i2c_driver);
534
535 MODULE_AUTHOR("Badhri Jagan Sridharan <badhri@google.com>");
536 MODULE_DESCRIPTION("Maxim TCPCI based USB Type-C Port Controller Interface Driver");
537 MODULE_LICENSE("GPL v2");
538