1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Driver for TI TPS6598x USB Power Delivery controller family
4 *
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 */
8
9 #include <linux/i2c.h>
10 #include <linux/acpi.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/power_supply.h>
15 #include <linux/regmap.h>
16 #include <linux/interrupt.h>
17 #include <linux/usb/typec.h>
18 #include <linux/usb/typec_altmode.h>
19 #include <linux/usb/role.h>
20 #include <linux/workqueue.h>
21 #include <linux/firmware.h>
22
23 #include "tps6598x.h"
24 #include "trace.h"
25
26 /* Register offsets */
27 #define TPS_REG_VID 0x00
28 #define TPS_REG_MODE 0x03
29 #define TPS_REG_CMD1 0x08
30 #define TPS_REG_DATA1 0x09
31 #define TPS_REG_VERSION 0x0F
32 #define TPS_REG_INT_EVENT1 0x14
33 #define TPS_REG_INT_EVENT2 0x15
34 #define TPS_REG_INT_MASK1 0x16
35 #define TPS_REG_INT_MASK2 0x17
36 #define TPS_REG_INT_CLEAR1 0x18
37 #define TPS_REG_INT_CLEAR2 0x19
38 #define TPS_REG_SYSTEM_POWER_STATE 0x20
39 #define TPS_REG_STATUS 0x1a
40 #define TPS_REG_SYSTEM_CONF 0x28
41 #define TPS_REG_CTRL_CONF 0x29
42 #define TPS_REG_BOOT_STATUS 0x2D
43 #define TPS_REG_POWER_STATUS 0x3f
44 #define TPS_REG_PD_STATUS 0x40
45 #define TPS_REG_RX_IDENTITY_SOP 0x48
46 #define TPS_REG_DATA_STATUS 0x5f
47 #define TPS_REG_SLEEP_CONF 0x70
48
49 /* TPS_REG_SYSTEM_CONF bits */
50 #define TPS_SYSCONF_PORTINFO(c) ((c) & 7)
51
52 /*
53 * BPMs task timeout, recommended 5 seconds
54 * pg.48 TPS2575 Host Interface Technical Reference
55 * Manual (Rev. A)
56 * https://www.ti.com/lit/ug/slvuc05a/slvuc05a.pdf
57 */
58 #define TPS_BUNDLE_TIMEOUT 0x32
59
60 /* BPMs return code */
61 #define TPS_TASK_BPMS_INVALID_BUNDLE_SIZE 0x4
62 #define TPS_TASK_BPMS_INVALID_SLAVE_ADDR 0x5
63 #define TPS_TASK_BPMS_INVALID_TIMEOUT 0x6
64
65 /* PBMc data out */
66 #define TPS_PBMC_RC 0 /* Return code */
67 #define TPS_PBMC_DPCS 2 /* device patch complete status */
68
69 /* reset de-assertion to ready for operation */
70 #define TPS_SETUP_MS 1000
71
72 enum {
73 TPS_PORTINFO_SINK,
74 TPS_PORTINFO_SINK_ACCESSORY,
75 TPS_PORTINFO_DRP_UFP,
76 TPS_PORTINFO_DRP_UFP_DRD,
77 TPS_PORTINFO_DRP_DFP,
78 TPS_PORTINFO_DRP_DFP_DRD,
79 TPS_PORTINFO_SOURCE,
80 };
81
82 /* TPS_REG_RX_IDENTITY_SOP */
83 struct tps6598x_rx_identity_reg {
84 u8 status;
85 struct usb_pd_identity identity;
86 } __packed;
87
88 /* Standard Task return codes */
89 #define TPS_TASK_TIMEOUT 1
90 #define TPS_TASK_REJECTED 3
91
92 enum {
93 TPS_MODE_APP,
94 TPS_MODE_BOOT,
95 TPS_MODE_BIST,
96 TPS_MODE_DISC,
97 TPS_MODE_PTCH,
98 };
99
100 static const char *const modes[] = {
101 [TPS_MODE_APP] = "APP ",
102 [TPS_MODE_BOOT] = "BOOT",
103 [TPS_MODE_BIST] = "BIST",
104 [TPS_MODE_DISC] = "DISC",
105 [TPS_MODE_PTCH] = "PTCH",
106 };
107
108 /* Unrecognized commands will be replaced with "!CMD" */
109 #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
110
111 struct tps6598x;
112
113 struct tipd_data {
114 irq_handler_t irq_handler;
115 int (*register_port)(struct tps6598x *tps, struct fwnode_handle *node);
116 void (*trace_power_status)(u16 status);
117 void (*trace_status)(u32 status);
118 int (*apply_patch)(struct tps6598x *tps);
119 int (*init)(struct tps6598x *tps);
120 int (*reset)(struct tps6598x *tps);
121 };
122
123 struct tps6598x {
124 struct device *dev;
125 struct regmap *regmap;
126 struct mutex lock; /* device lock */
127 u8 i2c_protocol:1;
128
129 struct gpio_desc *reset;
130 struct typec_port *port;
131 struct typec_partner *partner;
132 struct usb_pd_identity partner_identity;
133 struct usb_role_switch *role_sw;
134 struct typec_capability typec_cap;
135
136 struct power_supply *psy;
137 struct power_supply_desc psy_desc;
138 enum power_supply_usb_type usb_type;
139
140 int wakeup;
141 u32 status; /* status reg */
142 u16 pwr_status;
143 struct delayed_work wq_poll;
144
145 const struct tipd_data *data;
146 };
147
148 static enum power_supply_property tps6598x_psy_props[] = {
149 POWER_SUPPLY_PROP_USB_TYPE,
150 POWER_SUPPLY_PROP_ONLINE,
151 };
152
153 static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-";
154
155 /*
156 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
157 * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
158 */
159 #define TPS_MAX_LEN 64
160
161 static int
tps6598x_block_read(struct tps6598x * tps,u8 reg,void * val,size_t len)162 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
163 {
164 u8 data[TPS_MAX_LEN + 1];
165 int ret;
166
167 if (len + 1 > sizeof(data))
168 return -EINVAL;
169
170 if (!tps->i2c_protocol)
171 return regmap_raw_read(tps->regmap, reg, val, len);
172
173 ret = regmap_raw_read(tps->regmap, reg, data, len + 1);
174 if (ret)
175 return ret;
176
177 if (data[0] < len)
178 return -EIO;
179
180 memcpy(val, &data[1], len);
181 return 0;
182 }
183
tps6598x_block_write(struct tps6598x * tps,u8 reg,const void * val,size_t len)184 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
185 const void *val, size_t len)
186 {
187 u8 data[TPS_MAX_LEN + 1];
188
189 if (len + 1 > sizeof(data))
190 return -EINVAL;
191
192 if (!tps->i2c_protocol)
193 return regmap_raw_write(tps->regmap, reg, val, len);
194
195 data[0] = len;
196 memcpy(&data[1], val, len);
197
198 return regmap_raw_write(tps->regmap, reg, data, len + 1);
199 }
200
tps6598x_read8(struct tps6598x * tps,u8 reg,u8 * val)201 static inline int tps6598x_read8(struct tps6598x *tps, u8 reg, u8 *val)
202 {
203 return tps6598x_block_read(tps, reg, val, sizeof(u8));
204 }
205
tps6598x_read16(struct tps6598x * tps,u8 reg,u16 * val)206 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
207 {
208 return tps6598x_block_read(tps, reg, val, sizeof(u16));
209 }
210
tps6598x_read32(struct tps6598x * tps,u8 reg,u32 * val)211 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
212 {
213 return tps6598x_block_read(tps, reg, val, sizeof(u32));
214 }
215
tps6598x_read64(struct tps6598x * tps,u8 reg,u64 * val)216 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
217 {
218 return tps6598x_block_read(tps, reg, val, sizeof(u64));
219 }
220
tps6598x_write8(struct tps6598x * tps,u8 reg,u8 val)221 static inline int tps6598x_write8(struct tps6598x *tps, u8 reg, u8 val)
222 {
223 return tps6598x_block_write(tps, reg, &val, sizeof(u8));
224 }
225
tps6598x_write64(struct tps6598x * tps,u8 reg,u64 val)226 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
227 {
228 return tps6598x_block_write(tps, reg, &val, sizeof(u64));
229 }
230
231 static inline int
tps6598x_write_4cc(struct tps6598x * tps,u8 reg,const char * val)232 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
233 {
234 return tps6598x_block_write(tps, reg, val, 4);
235 }
236
tps6598x_read_partner_identity(struct tps6598x * tps)237 static int tps6598x_read_partner_identity(struct tps6598x *tps)
238 {
239 struct tps6598x_rx_identity_reg id;
240 int ret;
241
242 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
243 &id, sizeof(id));
244 if (ret)
245 return ret;
246
247 tps->partner_identity = id.identity;
248
249 return 0;
250 }
251
tps6598x_set_data_role(struct tps6598x * tps,enum typec_data_role role,bool connected)252 static void tps6598x_set_data_role(struct tps6598x *tps,
253 enum typec_data_role role, bool connected)
254 {
255 enum usb_role role_val;
256
257 if (role == TYPEC_HOST)
258 role_val = USB_ROLE_HOST;
259 else
260 role_val = USB_ROLE_DEVICE;
261
262 if (!connected)
263 role_val = USB_ROLE_NONE;
264
265 usb_role_switch_set_role(tps->role_sw, role_val);
266 typec_set_data_role(tps->port, role);
267 }
268
tps6598x_connect(struct tps6598x * tps,u32 status)269 static int tps6598x_connect(struct tps6598x *tps, u32 status)
270 {
271 struct typec_partner_desc desc;
272 enum typec_pwr_opmode mode;
273 int ret;
274
275 if (tps->partner)
276 return 0;
277
278 mode = TPS_POWER_STATUS_PWROPMODE(tps->pwr_status);
279
280 desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
281 desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
282 desc.identity = NULL;
283
284 if (desc.usb_pd) {
285 ret = tps6598x_read_partner_identity(tps);
286 if (ret)
287 return ret;
288 desc.identity = &tps->partner_identity;
289 }
290
291 typec_set_pwr_opmode(tps->port, mode);
292 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
293 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
294 if (TPS_STATUS_TO_UPSIDE_DOWN(status))
295 typec_set_orientation(tps->port, TYPEC_ORIENTATION_REVERSE);
296 else
297 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NORMAL);
298 typec_set_mode(tps->port, TYPEC_STATE_USB);
299 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), true);
300
301 tps->partner = typec_register_partner(tps->port, &desc);
302 if (IS_ERR(tps->partner))
303 return PTR_ERR(tps->partner);
304
305 if (desc.identity)
306 typec_partner_set_identity(tps->partner);
307
308 power_supply_changed(tps->psy);
309
310 return 0;
311 }
312
tps6598x_disconnect(struct tps6598x * tps,u32 status)313 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
314 {
315 if (!IS_ERR(tps->partner))
316 typec_unregister_partner(tps->partner);
317 tps->partner = NULL;
318 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
319 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
320 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
321 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NONE);
322 typec_set_mode(tps->port, TYPEC_STATE_SAFE);
323 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), false);
324
325 power_supply_changed(tps->psy);
326 }
327
tps6598x_exec_cmd_tmo(struct tps6598x * tps,const char * cmd,size_t in_len,const u8 * in_data,size_t out_len,u8 * out_data,u32 cmd_timeout_ms,u32 res_delay_ms)328 static int tps6598x_exec_cmd_tmo(struct tps6598x *tps, const char *cmd,
329 size_t in_len, const u8 *in_data,
330 size_t out_len, u8 *out_data,
331 u32 cmd_timeout_ms, u32 res_delay_ms)
332 {
333 unsigned long timeout;
334 u32 val;
335 int ret;
336
337 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
338 if (ret)
339 return ret;
340 if (val && !INVALID_CMD(val))
341 return -EBUSY;
342
343 if (in_len) {
344 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
345 in_data, in_len);
346 if (ret)
347 return ret;
348 }
349
350 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
351 if (ret < 0)
352 return ret;
353
354 timeout = jiffies + msecs_to_jiffies(cmd_timeout_ms);
355
356 do {
357 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
358 if (ret)
359 return ret;
360 if (INVALID_CMD(val))
361 return -EINVAL;
362
363 if (time_is_before_jiffies(timeout))
364 return -ETIMEDOUT;
365 } while (val);
366
367 /* some commands require delay for the result to be available */
368 mdelay(res_delay_ms);
369
370 if (out_len) {
371 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
372 out_data, out_len);
373 if (ret)
374 return ret;
375 val = out_data[0];
376 } else {
377 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
378 if (ret)
379 return ret;
380 }
381
382 switch (val) {
383 case TPS_TASK_TIMEOUT:
384 return -ETIMEDOUT;
385 case TPS_TASK_REJECTED:
386 return -EPERM;
387 default:
388 break;
389 }
390
391 return 0;
392 }
393
tps6598x_exec_cmd(struct tps6598x * tps,const char * cmd,size_t in_len,const u8 * in_data,size_t out_len,u8 * out_data)394 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
395 size_t in_len, const u8 *in_data,
396 size_t out_len, u8 *out_data)
397 {
398 return tps6598x_exec_cmd_tmo(tps, cmd, in_len, in_data,
399 out_len, out_data, 1000, 0);
400 }
401
tps6598x_dr_set(struct typec_port * port,enum typec_data_role role)402 static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
403 {
404 const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
405 struct tps6598x *tps = typec_get_drvdata(port);
406 u32 status;
407 int ret;
408
409 mutex_lock(&tps->lock);
410
411 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
412 if (ret)
413 goto out_unlock;
414
415 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
416 if (ret)
417 goto out_unlock;
418
419 if (role != TPS_STATUS_TO_TYPEC_DATAROLE(status)) {
420 ret = -EPROTO;
421 goto out_unlock;
422 }
423
424 tps6598x_set_data_role(tps, role, true);
425
426 out_unlock:
427 mutex_unlock(&tps->lock);
428
429 return ret;
430 }
431
tps6598x_pr_set(struct typec_port * port,enum typec_role role)432 static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
433 {
434 const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
435 struct tps6598x *tps = typec_get_drvdata(port);
436 u32 status;
437 int ret;
438
439 mutex_lock(&tps->lock);
440
441 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
442 if (ret)
443 goto out_unlock;
444
445 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
446 if (ret)
447 goto out_unlock;
448
449 if (role != TPS_STATUS_TO_TYPEC_PORTROLE(status)) {
450 ret = -EPROTO;
451 goto out_unlock;
452 }
453
454 typec_set_pwr_role(tps->port, role);
455
456 out_unlock:
457 mutex_unlock(&tps->lock);
458
459 return ret;
460 }
461
462 static const struct typec_operations tps6598x_ops = {
463 .dr_set = tps6598x_dr_set,
464 .pr_set = tps6598x_pr_set,
465 };
466
tps6598x_read_status(struct tps6598x * tps,u32 * status)467 static bool tps6598x_read_status(struct tps6598x *tps, u32 *status)
468 {
469 int ret;
470
471 ret = tps6598x_read32(tps, TPS_REG_STATUS, status);
472 if (ret) {
473 dev_err(tps->dev, "%s: failed to read status\n", __func__);
474 return false;
475 }
476
477 if (tps->data->trace_status)
478 tps->data->trace_status(*status);
479
480 return true;
481 }
482
tps6598x_read_data_status(struct tps6598x * tps)483 static bool tps6598x_read_data_status(struct tps6598x *tps)
484 {
485 u32 data_status;
486 int ret;
487
488 ret = tps6598x_read32(tps, TPS_REG_DATA_STATUS, &data_status);
489 if (ret < 0) {
490 dev_err(tps->dev, "failed to read data status: %d\n", ret);
491 return false;
492 }
493 trace_tps6598x_data_status(data_status);
494
495 return true;
496 }
497
tps6598x_read_power_status(struct tps6598x * tps)498 static bool tps6598x_read_power_status(struct tps6598x *tps)
499 {
500 u16 pwr_status;
501 int ret;
502
503 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
504 if (ret < 0) {
505 dev_err(tps->dev, "failed to read power status: %d\n", ret);
506 return false;
507 }
508 tps->pwr_status = pwr_status;
509
510 if (tps->data->trace_power_status)
511 tps->data->trace_power_status(pwr_status);
512
513 return true;
514 }
515
tps6598x_handle_plug_event(struct tps6598x * tps,u32 status)516 static void tps6598x_handle_plug_event(struct tps6598x *tps, u32 status)
517 {
518 int ret;
519
520 if (status & TPS_STATUS_PLUG_PRESENT) {
521 ret = tps6598x_connect(tps, status);
522 if (ret)
523 dev_err(tps->dev, "failed to register partner\n");
524 } else {
525 tps6598x_disconnect(tps, status);
526 }
527 }
528
cd321x_interrupt(int irq,void * data)529 static irqreturn_t cd321x_interrupt(int irq, void *data)
530 {
531 struct tps6598x *tps = data;
532 u64 event = 0;
533 u32 status;
534 int ret;
535
536 mutex_lock(&tps->lock);
537
538 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event);
539 if (ret) {
540 dev_err(tps->dev, "%s: failed to read events\n", __func__);
541 goto err_unlock;
542 }
543 trace_cd321x_irq(event);
544
545 if (!event)
546 goto err_unlock;
547
548 if (!tps6598x_read_status(tps, &status))
549 goto err_clear_ints;
550
551 if (event & APPLE_CD_REG_INT_POWER_STATUS_UPDATE)
552 if (!tps6598x_read_power_status(tps))
553 goto err_clear_ints;
554
555 if (event & APPLE_CD_REG_INT_DATA_STATUS_UPDATE)
556 if (!tps6598x_read_data_status(tps))
557 goto err_clear_ints;
558
559 /* Handle plug insert or removal */
560 if (event & APPLE_CD_REG_INT_PLUG_EVENT)
561 tps6598x_handle_plug_event(tps, status);
562
563 err_clear_ints:
564 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event);
565
566 err_unlock:
567 mutex_unlock(&tps->lock);
568
569 if (event)
570 return IRQ_HANDLED;
571 return IRQ_NONE;
572 }
573
tps6598x_has_role_changed(struct tps6598x * tps,u32 status)574 static bool tps6598x_has_role_changed(struct tps6598x *tps, u32 status)
575 {
576 status ^= tps->status;
577
578 return status & (TPS_STATUS_PORTROLE | TPS_STATUS_DATAROLE);
579 }
580
tps25750_interrupt(int irq,void * data)581 static irqreturn_t tps25750_interrupt(int irq, void *data)
582 {
583 struct tps6598x *tps = data;
584 u64 event[2] = { };
585 u32 status;
586 int ret;
587
588 mutex_lock(&tps->lock);
589
590 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event, 11);
591 if (ret) {
592 dev_err(tps->dev, "%s: failed to read events\n", __func__);
593 goto err_unlock;
594 }
595 trace_tps25750_irq(event[0]);
596
597 if (!(event[0] | event[1]))
598 goto err_unlock;
599
600 if (!tps6598x_read_status(tps, &status))
601 goto err_clear_ints;
602
603 if (event[0] & TPS_REG_INT_POWER_STATUS_UPDATE)
604 if (!tps6598x_read_power_status(tps))
605 goto err_clear_ints;
606
607 if (event[0] & TPS_REG_INT_DATA_STATUS_UPDATE)
608 if (!tps6598x_read_data_status(tps))
609 goto err_clear_ints;
610
611 /*
612 * data/port roles could be updated independently after
613 * a plug event. Therefore, we need to check
614 * for pr/dr status change to set TypeC dr/pr accordingly.
615 */
616 if (event[0] & TPS_REG_INT_PLUG_EVENT ||
617 tps6598x_has_role_changed(tps, status))
618 tps6598x_handle_plug_event(tps, status);
619
620 tps->status = status;
621
622 err_clear_ints:
623 tps6598x_block_write(tps, TPS_REG_INT_CLEAR1, event, 11);
624
625 err_unlock:
626 mutex_unlock(&tps->lock);
627
628 if (event[0] | event[1])
629 return IRQ_HANDLED;
630 return IRQ_NONE;
631 }
632
tps6598x_interrupt(int irq,void * data)633 static irqreturn_t tps6598x_interrupt(int irq, void *data)
634 {
635 int intev_len = TPS_65981_2_6_INTEVENT_LEN;
636 struct tps6598x *tps = data;
637 u64 event1[2] = { };
638 u64 event2[2] = { };
639 u32 version;
640 u32 status;
641 int ret;
642
643 mutex_lock(&tps->lock);
644
645 ret = tps6598x_read32(tps, TPS_REG_VERSION, &version);
646 if (ret)
647 dev_warn(tps->dev, "%s: failed to read version (%d)\n",
648 __func__, ret);
649
650 if (TPS_VERSION_HW_VERSION(version) == TPS_VERSION_HW_65987_8_DH ||
651 TPS_VERSION_HW_VERSION(version) == TPS_VERSION_HW_65987_8_DK)
652 intev_len = TPS_65987_8_INTEVENT_LEN;
653
654 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event1, intev_len);
655
656 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event1, intev_len);
657 if (ret) {
658 dev_err(tps->dev, "%s: failed to read event1\n", __func__);
659 goto err_unlock;
660 }
661 ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT2, event2, intev_len);
662 if (ret) {
663 dev_err(tps->dev, "%s: failed to read event2\n", __func__);
664 goto err_unlock;
665 }
666 trace_tps6598x_irq(event1[0], event2[0]);
667
668 if (!(event1[0] | event1[1] | event2[0] | event2[1]))
669 goto err_unlock;
670
671 if (!tps6598x_read_status(tps, &status))
672 goto err_clear_ints;
673
674 if ((event1[0] | event2[0]) & TPS_REG_INT_POWER_STATUS_UPDATE)
675 if (!tps6598x_read_power_status(tps))
676 goto err_clear_ints;
677
678 if ((event1[0] | event2[0]) & TPS_REG_INT_DATA_STATUS_UPDATE)
679 if (!tps6598x_read_data_status(tps))
680 goto err_clear_ints;
681
682 /* Handle plug insert or removal */
683 if ((event1[0] | event2[0]) & TPS_REG_INT_PLUG_EVENT)
684 tps6598x_handle_plug_event(tps, status);
685
686 err_clear_ints:
687 tps6598x_block_write(tps, TPS_REG_INT_CLEAR1, event1, intev_len);
688 tps6598x_block_write(tps, TPS_REG_INT_CLEAR2, event2, intev_len);
689
690 err_unlock:
691 mutex_unlock(&tps->lock);
692
693 if (event1[0] | event1[1] | event2[0] | event2[1])
694 return IRQ_HANDLED;
695
696 return IRQ_NONE;
697 }
698
699 /* Time interval for Polling */
700 #define POLL_INTERVAL 500 /* msecs */
tps6598x_poll_work(struct work_struct * work)701 static void tps6598x_poll_work(struct work_struct *work)
702 {
703 struct tps6598x *tps = container_of(to_delayed_work(work),
704 struct tps6598x, wq_poll);
705
706 tps->data->irq_handler(0, tps);
707 queue_delayed_work(system_power_efficient_wq,
708 &tps->wq_poll, msecs_to_jiffies(POLL_INTERVAL));
709 }
710
tps6598x_check_mode(struct tps6598x * tps)711 static int tps6598x_check_mode(struct tps6598x *tps)
712 {
713 char mode[5] = { };
714 int ret;
715
716 ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
717 if (ret)
718 return ret;
719
720 ret = match_string(modes, ARRAY_SIZE(modes), mode);
721
722 switch (ret) {
723 case TPS_MODE_APP:
724 case TPS_MODE_PTCH:
725 return ret;
726 case TPS_MODE_BOOT:
727 dev_warn(tps->dev, "dead-battery condition\n");
728 return ret;
729 case TPS_MODE_BIST:
730 case TPS_MODE_DISC:
731 default:
732 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
733 mode);
734 break;
735 }
736
737 return -ENODEV;
738 }
739
740 static const struct regmap_config tps6598x_regmap_config = {
741 .reg_bits = 8,
742 .val_bits = 8,
743 .max_register = 0x7F,
744 };
745
tps6598x_psy_get_online(struct tps6598x * tps,union power_supply_propval * val)746 static int tps6598x_psy_get_online(struct tps6598x *tps,
747 union power_supply_propval *val)
748 {
749 if (TPS_POWER_STATUS_CONNECTION(tps->pwr_status) &&
750 TPS_POWER_STATUS_SOURCESINK(tps->pwr_status)) {
751 val->intval = 1;
752 } else {
753 val->intval = 0;
754 }
755 return 0;
756 }
757
tps6598x_psy_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)758 static int tps6598x_psy_get_prop(struct power_supply *psy,
759 enum power_supply_property psp,
760 union power_supply_propval *val)
761 {
762 struct tps6598x *tps = power_supply_get_drvdata(psy);
763 int ret = 0;
764
765 switch (psp) {
766 case POWER_SUPPLY_PROP_USB_TYPE:
767 if (TPS_POWER_STATUS_PWROPMODE(tps->pwr_status) == TYPEC_PWR_MODE_PD)
768 val->intval = POWER_SUPPLY_USB_TYPE_PD;
769 else
770 val->intval = POWER_SUPPLY_USB_TYPE_C;
771 break;
772 case POWER_SUPPLY_PROP_ONLINE:
773 ret = tps6598x_psy_get_online(tps, val);
774 break;
775 default:
776 ret = -EINVAL;
777 break;
778 }
779
780 return ret;
781 }
782
cd321x_switch_power_state(struct tps6598x * tps,u8 target_state)783 static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state)
784 {
785 u8 state;
786 int ret;
787
788 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
789 if (ret)
790 return ret;
791
792 if (state == target_state)
793 return 0;
794
795 ret = tps6598x_exec_cmd(tps, "SSPS", sizeof(u8), &target_state, 0, NULL);
796 if (ret)
797 return ret;
798
799 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
800 if (ret)
801 return ret;
802
803 if (state != target_state)
804 return -EINVAL;
805
806 return 0;
807 }
808
devm_tps6598_psy_register(struct tps6598x * tps)809 static int devm_tps6598_psy_register(struct tps6598x *tps)
810 {
811 struct power_supply_config psy_cfg = {};
812 const char *port_dev_name = dev_name(tps->dev);
813 char *psy_name;
814
815 psy_cfg.drv_data = tps;
816 psy_cfg.fwnode = dev_fwnode(tps->dev);
817
818 psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
819 port_dev_name);
820 if (!psy_name)
821 return -ENOMEM;
822
823 tps->psy_desc.name = psy_name;
824 tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
825 tps->psy_desc.usb_types = BIT(POWER_SUPPLY_USB_TYPE_C) |
826 BIT(POWER_SUPPLY_USB_TYPE_PD);
827 tps->psy_desc.properties = tps6598x_psy_props;
828 tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
829 tps->psy_desc.get_property = tps6598x_psy_get_prop;
830
831 tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
832
833 tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
834 &psy_cfg);
835 return PTR_ERR_OR_ZERO(tps->psy);
836 }
837
838 static int
tps6598x_register_port(struct tps6598x * tps,struct fwnode_handle * fwnode)839 tps6598x_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
840 {
841 int ret;
842 u32 conf;
843 struct typec_capability typec_cap = { };
844
845 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
846 if (ret)
847 return ret;
848
849 typec_cap.revision = USB_TYPEC_REV_1_2;
850 typec_cap.pd_revision = 0x200;
851 typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
852 typec_cap.driver_data = tps;
853 typec_cap.ops = &tps6598x_ops;
854 typec_cap.fwnode = fwnode;
855
856 switch (TPS_SYSCONF_PORTINFO(conf)) {
857 case TPS_PORTINFO_SINK_ACCESSORY:
858 case TPS_PORTINFO_SINK:
859 typec_cap.type = TYPEC_PORT_SNK;
860 typec_cap.data = TYPEC_PORT_UFP;
861 break;
862 case TPS_PORTINFO_DRP_UFP_DRD:
863 case TPS_PORTINFO_DRP_DFP_DRD:
864 typec_cap.type = TYPEC_PORT_DRP;
865 typec_cap.data = TYPEC_PORT_DRD;
866 break;
867 case TPS_PORTINFO_DRP_UFP:
868 typec_cap.type = TYPEC_PORT_DRP;
869 typec_cap.data = TYPEC_PORT_UFP;
870 break;
871 case TPS_PORTINFO_DRP_DFP:
872 typec_cap.type = TYPEC_PORT_DRP;
873 typec_cap.data = TYPEC_PORT_DFP;
874 break;
875 case TPS_PORTINFO_SOURCE:
876 typec_cap.type = TYPEC_PORT_SRC;
877 typec_cap.data = TYPEC_PORT_DFP;
878 break;
879 default:
880 return -ENODEV;
881 }
882
883 tps->port = typec_register_port(tps->dev, &typec_cap);
884 if (IS_ERR(tps->port))
885 return PTR_ERR(tps->port);
886
887 return 0;
888 }
889
tps_request_firmware(struct tps6598x * tps,const struct firmware ** fw,const char ** firmware_name)890 static int tps_request_firmware(struct tps6598x *tps, const struct firmware **fw,
891 const char **firmware_name)
892 {
893 int ret;
894
895 ret = device_property_read_string(tps->dev, "firmware-name",
896 firmware_name);
897 if (ret)
898 return ret;
899
900 ret = request_firmware(fw, *firmware_name, tps->dev);
901 if (ret) {
902 dev_err(tps->dev, "failed to retrieve \"%s\"\n", *firmware_name);
903 return ret;
904 }
905
906 if ((*fw)->size == 0) {
907 release_firmware(*fw);
908 ret = -EINVAL;
909 }
910
911 return ret;
912 }
913
914 static int
tps25750_write_firmware(struct tps6598x * tps,u8 bpms_addr,const u8 * data,size_t len)915 tps25750_write_firmware(struct tps6598x *tps,
916 u8 bpms_addr, const u8 *data, size_t len)
917 {
918 struct i2c_client *client = to_i2c_client(tps->dev);
919 int ret;
920 u8 slave_addr;
921 int timeout;
922
923 slave_addr = client->addr;
924 timeout = client->adapter->timeout;
925
926 /*
927 * binary configuration size is around ~16Kbytes
928 * which might take some time to finish writing it
929 */
930 client->adapter->timeout = msecs_to_jiffies(5000);
931 client->addr = bpms_addr;
932
933 ret = regmap_raw_write(tps->regmap, data[0], &data[1], len - 1);
934
935 client->addr = slave_addr;
936 client->adapter->timeout = timeout;
937
938 return ret;
939 }
940
941 static int
tps25750_exec_pbms(struct tps6598x * tps,u8 * in_data,size_t in_len)942 tps25750_exec_pbms(struct tps6598x *tps, u8 *in_data, size_t in_len)
943 {
944 int ret;
945 u8 rc;
946
947 ret = tps6598x_exec_cmd_tmo(tps, "PBMs", in_len, in_data,
948 sizeof(rc), &rc, 4000, 0);
949 if (ret)
950 return ret;
951
952 switch (rc) {
953 case TPS_TASK_BPMS_INVALID_BUNDLE_SIZE:
954 dev_err(tps->dev, "%s: invalid fw size\n", __func__);
955 return -EINVAL;
956 case TPS_TASK_BPMS_INVALID_SLAVE_ADDR:
957 dev_err(tps->dev, "%s: invalid slave address\n", __func__);
958 return -EINVAL;
959 case TPS_TASK_BPMS_INVALID_TIMEOUT:
960 dev_err(tps->dev, "%s: timed out\n", __func__);
961 return -ETIMEDOUT;
962 default:
963 break;
964 }
965
966 return 0;
967 }
968
tps25750_abort_patch_process(struct tps6598x * tps)969 static int tps25750_abort_patch_process(struct tps6598x *tps)
970 {
971 int ret;
972
973 ret = tps6598x_exec_cmd(tps, "PBMe", 0, NULL, 0, NULL);
974 if (ret)
975 return ret;
976
977 ret = tps6598x_check_mode(tps);
978 if (ret != TPS_MODE_PTCH)
979 dev_err(tps->dev, "failed to switch to \"PTCH\" mode\n");
980
981 return ret;
982 }
983
tps25750_start_patch_burst_mode(struct tps6598x * tps)984 static int tps25750_start_patch_burst_mode(struct tps6598x *tps)
985 {
986 int ret;
987 const struct firmware *fw;
988 const char *firmware_name;
989 struct {
990 u32 fw_size;
991 u8 addr;
992 u8 timeout;
993 } __packed bpms_data;
994 u32 addr;
995 struct device_node *np = tps->dev->of_node;
996
997 ret = tps_request_firmware(tps, &fw, &firmware_name);
998 if (ret)
999 return ret;
1000
1001 ret = of_property_match_string(np, "reg-names", "patch-address");
1002 if (ret < 0) {
1003 dev_err(tps->dev, "failed to get patch-address %d\n", ret);
1004 goto release_fw;
1005 }
1006
1007 ret = of_property_read_u32_index(np, "reg", ret, &addr);
1008 if (ret)
1009 goto release_fw;
1010
1011 if (addr == 0 || (addr >= 0x20 && addr <= 0x23)) {
1012 dev_err(tps->dev, "wrong patch address %u\n", addr);
1013 ret = -EINVAL;
1014 goto release_fw;
1015 }
1016
1017 bpms_data.addr = (u8)addr;
1018 bpms_data.fw_size = fw->size;
1019 bpms_data.timeout = TPS_BUNDLE_TIMEOUT;
1020
1021 ret = tps25750_exec_pbms(tps, (u8 *)&bpms_data, sizeof(bpms_data));
1022 if (ret)
1023 goto release_fw;
1024
1025 ret = tps25750_write_firmware(tps, bpms_data.addr, fw->data, fw->size);
1026 if (ret) {
1027 dev_err(tps->dev, "Failed to write patch %s of %zu bytes\n",
1028 firmware_name, fw->size);
1029 goto release_fw;
1030 }
1031
1032 /*
1033 * A delay of 500us is required after the firmware is written
1034 * based on pg.62 in tps6598x Host Interface Technical
1035 * Reference Manual
1036 * https://www.ti.com/lit/ug/slvuc05a/slvuc05a.pdf
1037 */
1038 udelay(500);
1039
1040 release_fw:
1041 release_firmware(fw);
1042
1043 return ret;
1044 }
1045
tps25750_complete_patch_process(struct tps6598x * tps)1046 static int tps25750_complete_patch_process(struct tps6598x *tps)
1047 {
1048 int ret;
1049 u8 out_data[40];
1050 u8 dummy[2] = { };
1051
1052 /*
1053 * Without writing something to DATA_IN, this command would
1054 * return an error
1055 */
1056 ret = tps6598x_exec_cmd_tmo(tps, "PBMc", sizeof(dummy), dummy,
1057 sizeof(out_data), out_data, 2000, 20);
1058 if (ret)
1059 return ret;
1060
1061 if (out_data[TPS_PBMC_RC]) {
1062 dev_err(tps->dev,
1063 "%s: pbmc failed: %u\n", __func__,
1064 out_data[TPS_PBMC_RC]);
1065 return -EIO;
1066 }
1067
1068 if (out_data[TPS_PBMC_DPCS]) {
1069 dev_err(tps->dev,
1070 "%s: failed device patch complete status: %u\n",
1071 __func__, out_data[TPS_PBMC_DPCS]);
1072 return -EIO;
1073 }
1074
1075 return 0;
1076 }
1077
tps25750_apply_patch(struct tps6598x * tps)1078 static int tps25750_apply_patch(struct tps6598x *tps)
1079 {
1080 int ret;
1081 unsigned long timeout;
1082 u64 status = 0;
1083
1084 ret = tps6598x_block_read(tps, TPS_REG_BOOT_STATUS, &status, 5);
1085 if (ret)
1086 return ret;
1087 /*
1088 * Nothing to be done if the configuration
1089 * is being loaded from EERPOM
1090 */
1091 if (status & TPS_BOOT_STATUS_I2C_EEPROM_PRESENT)
1092 goto wait_for_app;
1093
1094 ret = tps25750_start_patch_burst_mode(tps);
1095 if (ret) {
1096 tps25750_abort_patch_process(tps);
1097 return ret;
1098 }
1099
1100 ret = tps25750_complete_patch_process(tps);
1101 if (ret)
1102 return ret;
1103
1104 wait_for_app:
1105 timeout = jiffies + msecs_to_jiffies(1000);
1106
1107 do {
1108 ret = tps6598x_check_mode(tps);
1109 if (ret < 0)
1110 return ret;
1111
1112 if (time_is_before_jiffies(timeout))
1113 return -ETIMEDOUT;
1114
1115 } while (ret != TPS_MODE_APP);
1116
1117 /*
1118 * The dead battery flag may be triggered when the controller
1119 * port is connected to a device that can source power and
1120 * attempts to power up both the controller and the board it is on.
1121 * To restore controller functionality, it is necessary to clear
1122 * this flag
1123 */
1124 if (status & TPS_BOOT_STATUS_DEAD_BATTERY_FLAG) {
1125 ret = tps6598x_exec_cmd(tps, "DBfg", 0, NULL, 0, NULL);
1126 if (ret) {
1127 dev_err(tps->dev, "failed to clear dead battery %d\n", ret);
1128 return ret;
1129 }
1130 }
1131
1132 dev_info(tps->dev, "controller switched to \"APP\" mode\n");
1133
1134 return 0;
1135 };
1136
tps6598x_apply_patch(struct tps6598x * tps)1137 static int tps6598x_apply_patch(struct tps6598x *tps)
1138 {
1139 u8 in = TPS_PTCS_CONTENT_DEV | TPS_PTCS_CONTENT_APP;
1140 u8 out[TPS_MAX_LEN] = {0};
1141 size_t in_len = sizeof(in);
1142 size_t copied_bytes = 0;
1143 size_t bytes_left;
1144 const struct firmware *fw;
1145 const char *firmware_name;
1146 int ret;
1147
1148 ret = tps_request_firmware(tps, &fw, &firmware_name);
1149 if (ret)
1150 return ret;
1151
1152 ret = tps6598x_exec_cmd(tps, "PTCs", in_len, &in,
1153 TPS_PTCS_OUT_BYTES, out);
1154 if (ret || out[TPS_PTCS_STATUS] == TPS_PTCS_STATUS_FAIL) {
1155 if (!ret)
1156 ret = -EBUSY;
1157 dev_err(tps->dev, "Update start failed (%d)\n", ret);
1158 goto release_fw;
1159 }
1160
1161 bytes_left = fw->size;
1162 while (bytes_left) {
1163 in_len = min(bytes_left, TPS_MAX_LEN);
1164 ret = tps6598x_exec_cmd(tps, "PTCd", in_len,
1165 fw->data + copied_bytes,
1166 TPS_PTCD_OUT_BYTES, out);
1167 if (ret || out[TPS_PTCD_TRANSFER_STATUS] ||
1168 out[TPS_PTCD_LOADING_STATE] == TPS_PTCD_LOAD_ERR) {
1169 if (!ret)
1170 ret = -EBUSY;
1171 dev_err(tps->dev, "Patch download failed (%d)\n", ret);
1172 goto release_fw;
1173 }
1174 copied_bytes += in_len;
1175 bytes_left -= in_len;
1176 }
1177
1178 ret = tps6598x_exec_cmd(tps, "PTCc", 0, NULL, TPS_PTCC_OUT_BYTES, out);
1179 if (ret || out[TPS_PTCC_DEV] || out[TPS_PTCC_APP]) {
1180 if (!ret)
1181 ret = -EBUSY;
1182 dev_err(tps->dev, "Update completion failed (%d)\n", ret);
1183 goto release_fw;
1184 }
1185 msleep(TPS_SETUP_MS);
1186 dev_info(tps->dev, "Firmware update succeeded\n");
1187
1188 release_fw:
1189 if (ret) {
1190 dev_err(tps->dev, "Failed to write patch %s of %zu bytes\n",
1191 firmware_name, fw->size);
1192 }
1193 release_firmware(fw);
1194
1195 return ret;
1196 }
1197
cd321x_init(struct tps6598x * tps)1198 static int cd321x_init(struct tps6598x *tps)
1199 {
1200 return 0;
1201 }
1202
tps25750_init(struct tps6598x * tps)1203 static int tps25750_init(struct tps6598x *tps)
1204 {
1205 int ret;
1206
1207 ret = tps->data->apply_patch(tps);
1208 if (ret)
1209 return ret;
1210
1211 ret = tps6598x_write8(tps, TPS_REG_SLEEP_CONF,
1212 TPS_SLEEP_CONF_SLEEP_MODE_ALLOWED);
1213 if (ret)
1214 dev_warn(tps->dev,
1215 "%s: failed to enable sleep mode: %d\n",
1216 __func__, ret);
1217
1218 return 0;
1219 }
1220
tps6598x_init(struct tps6598x * tps)1221 static int tps6598x_init(struct tps6598x *tps)
1222 {
1223 return tps->data->apply_patch(tps);
1224 }
1225
cd321x_reset(struct tps6598x * tps)1226 static int cd321x_reset(struct tps6598x *tps)
1227 {
1228 return 0;
1229 }
1230
tps25750_reset(struct tps6598x * tps)1231 static int tps25750_reset(struct tps6598x *tps)
1232 {
1233 return tps6598x_exec_cmd_tmo(tps, "GAID", 0, NULL, 0, NULL, 2000, 0);
1234 }
1235
tps6598x_reset(struct tps6598x * tps)1236 static int tps6598x_reset(struct tps6598x *tps)
1237 {
1238 return 0;
1239 }
1240
1241 static int
tps25750_register_port(struct tps6598x * tps,struct fwnode_handle * fwnode)1242 tps25750_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
1243 {
1244 struct typec_capability typec_cap = { };
1245 const char *data_role;
1246 u8 pd_status;
1247 int ret;
1248
1249 ret = tps6598x_read8(tps, TPS_REG_PD_STATUS, &pd_status);
1250 if (ret)
1251 return ret;
1252
1253 ret = fwnode_property_read_string(fwnode, "data-role", &data_role);
1254 if (ret) {
1255 dev_err(tps->dev, "data-role not found: %d\n", ret);
1256 return ret;
1257 }
1258
1259 ret = typec_find_port_data_role(data_role);
1260 if (ret < 0) {
1261 dev_err(tps->dev, "unknown data-role: %s\n", data_role);
1262 return ret;
1263 }
1264
1265 typec_cap.data = ret;
1266 typec_cap.revision = USB_TYPEC_REV_1_3;
1267 typec_cap.pd_revision = 0x300;
1268 typec_cap.driver_data = tps;
1269 typec_cap.ops = &tps6598x_ops;
1270 typec_cap.fwnode = fwnode;
1271 typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
1272
1273 switch (TPS_PD_STATUS_PORT_TYPE(pd_status)) {
1274 case TPS_PD_STATUS_PORT_TYPE_SINK_SOURCE:
1275 case TPS_PD_STATUS_PORT_TYPE_SOURCE_SINK:
1276 typec_cap.type = TYPEC_PORT_DRP;
1277 break;
1278 case TPS_PD_STATUS_PORT_TYPE_SINK:
1279 typec_cap.type = TYPEC_PORT_SNK;
1280 break;
1281 case TPS_PD_STATUS_PORT_TYPE_SOURCE:
1282 typec_cap.type = TYPEC_PORT_SRC;
1283 break;
1284 default:
1285 return -ENODEV;
1286 }
1287
1288 tps->port = typec_register_port(tps->dev, &typec_cap);
1289 if (IS_ERR(tps->port))
1290 return PTR_ERR(tps->port);
1291
1292 return 0;
1293 }
1294
tps6598x_probe(struct i2c_client * client)1295 static int tps6598x_probe(struct i2c_client *client)
1296 {
1297 struct device_node *np = client->dev.of_node;
1298 struct tps6598x *tps;
1299 struct fwnode_handle *fwnode;
1300 u32 status;
1301 u32 vid;
1302 int ret;
1303 u64 mask1;
1304
1305 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
1306 if (!tps)
1307 return -ENOMEM;
1308
1309 mutex_init(&tps->lock);
1310 tps->dev = &client->dev;
1311
1312 tps->reset = devm_gpiod_get_optional(tps->dev, "reset", GPIOD_OUT_LOW);
1313 if (IS_ERR(tps->reset))
1314 return dev_err_probe(tps->dev, PTR_ERR(tps->reset),
1315 "failed to get reset GPIO\n");
1316 if (tps->reset)
1317 msleep(TPS_SETUP_MS);
1318
1319 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
1320 if (IS_ERR(tps->regmap))
1321 return PTR_ERR(tps->regmap);
1322
1323 if (!device_is_compatible(tps->dev, "ti,tps25750")) {
1324 ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
1325 if (ret < 0 || !vid)
1326 return -ENODEV;
1327 }
1328
1329 /*
1330 * Checking can the adapter handle SMBus protocol. If it can not, the
1331 * driver needs to take care of block reads separately.
1332 */
1333 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
1334 tps->i2c_protocol = true;
1335
1336 if (np && of_device_is_compatible(np, "apple,cd321x")) {
1337 /* Switch CD321X chips to the correct system power state */
1338 ret = cd321x_switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0);
1339 if (ret)
1340 return ret;
1341
1342 /* CD321X chips have all interrupts masked initially */
1343 mask1 = APPLE_CD_REG_INT_POWER_STATUS_UPDATE |
1344 APPLE_CD_REG_INT_DATA_STATUS_UPDATE |
1345 APPLE_CD_REG_INT_PLUG_EVENT;
1346
1347 } else {
1348 /* Enable power status, data status and plug event interrupts */
1349 mask1 = TPS_REG_INT_POWER_STATUS_UPDATE |
1350 TPS_REG_INT_DATA_STATUS_UPDATE |
1351 TPS_REG_INT_PLUG_EVENT;
1352 }
1353
1354 tps->data = i2c_get_match_data(client);
1355 if (!tps->data)
1356 return -EINVAL;
1357
1358 /* Make sure the controller has application firmware running */
1359 ret = tps6598x_check_mode(tps);
1360 if (ret < 0)
1361 return ret;
1362
1363 if (ret == TPS_MODE_PTCH) {
1364 ret = tps->data->init(tps);
1365 if (ret)
1366 return ret;
1367 }
1368
1369 ret = tps6598x_write64(tps, TPS_REG_INT_MASK1, mask1);
1370 if (ret)
1371 goto err_reset_controller;
1372
1373 if (!tps6598x_read_status(tps, &status)) {
1374 ret = -ENODEV;
1375 goto err_clear_mask;
1376 }
1377
1378 /*
1379 * This fwnode has a "compatible" property, but is never populated as a
1380 * struct device. Instead we simply parse it to read the properties.
1381 * This breaks fw_devlink=on. To maintain backward compatibility
1382 * with existing DT files, we work around this by deleting any
1383 * fwnode_links to/from this fwnode.
1384 */
1385 fwnode = device_get_named_child_node(&client->dev, "connector");
1386 if (fwnode)
1387 fw_devlink_purge_absent_suppliers(fwnode);
1388
1389 tps->role_sw = fwnode_usb_role_switch_get(fwnode);
1390 if (IS_ERR(tps->role_sw)) {
1391 ret = PTR_ERR(tps->role_sw);
1392 goto err_fwnode_put;
1393 }
1394
1395 ret = devm_tps6598_psy_register(tps);
1396 if (ret)
1397 goto err_role_put;
1398
1399 ret = tps->data->register_port(tps, fwnode);
1400 if (ret)
1401 goto err_role_put;
1402
1403 if (status & TPS_STATUS_PLUG_PRESENT) {
1404 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &tps->pwr_status);
1405 if (ret < 0) {
1406 dev_err(tps->dev, "failed to read power status: %d\n", ret);
1407 goto err_unregister_port;
1408 }
1409 ret = tps6598x_connect(tps, status);
1410 if (ret)
1411 dev_err(&client->dev, "failed to register partner\n");
1412 }
1413
1414 if (client->irq) {
1415 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
1416 tps->data->irq_handler,
1417 IRQF_SHARED | IRQF_ONESHOT,
1418 dev_name(&client->dev), tps);
1419 } else {
1420 dev_warn(tps->dev, "Unable to find the interrupt, switching to polling\n");
1421 INIT_DELAYED_WORK(&tps->wq_poll, tps6598x_poll_work);
1422 queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,
1423 msecs_to_jiffies(POLL_INTERVAL));
1424 }
1425
1426 if (ret)
1427 goto err_disconnect;
1428
1429 i2c_set_clientdata(client, tps);
1430 fwnode_handle_put(fwnode);
1431
1432 tps->wakeup = device_property_read_bool(tps->dev, "wakeup-source");
1433 if (tps->wakeup && client->irq) {
1434 devm_device_init_wakeup(&client->dev);
1435 enable_irq_wake(client->irq);
1436 }
1437
1438 return 0;
1439
1440 err_disconnect:
1441 tps6598x_disconnect(tps, 0);
1442 err_unregister_port:
1443 typec_unregister_port(tps->port);
1444 err_role_put:
1445 usb_role_switch_put(tps->role_sw);
1446 err_fwnode_put:
1447 fwnode_handle_put(fwnode);
1448 err_clear_mask:
1449 tps6598x_write64(tps, TPS_REG_INT_MASK1, 0);
1450 err_reset_controller:
1451 /* Reset PD controller to remove any applied patch */
1452 tps->data->reset(tps);
1453
1454 return ret;
1455 }
1456
tps6598x_remove(struct i2c_client * client)1457 static void tps6598x_remove(struct i2c_client *client)
1458 {
1459 struct tps6598x *tps = i2c_get_clientdata(client);
1460
1461 if (!client->irq)
1462 cancel_delayed_work_sync(&tps->wq_poll);
1463 else
1464 devm_free_irq(tps->dev, client->irq, tps);
1465
1466 tps6598x_disconnect(tps, 0);
1467 typec_unregister_port(tps->port);
1468 usb_role_switch_put(tps->role_sw);
1469
1470 /* Reset PD controller to remove any applied patch */
1471 tps->data->reset(tps);
1472
1473 if (tps->reset)
1474 gpiod_set_value_cansleep(tps->reset, 1);
1475 }
1476
tps6598x_suspend(struct device * dev)1477 static int __maybe_unused tps6598x_suspend(struct device *dev)
1478 {
1479 struct i2c_client *client = to_i2c_client(dev);
1480 struct tps6598x *tps = i2c_get_clientdata(client);
1481
1482 if (tps->wakeup) {
1483 disable_irq(client->irq);
1484 enable_irq_wake(client->irq);
1485 } else if (tps->reset) {
1486 gpiod_set_value_cansleep(tps->reset, 1);
1487 }
1488
1489 if (!client->irq)
1490 cancel_delayed_work_sync(&tps->wq_poll);
1491
1492 return 0;
1493 }
1494
tps6598x_resume(struct device * dev)1495 static int __maybe_unused tps6598x_resume(struct device *dev)
1496 {
1497 struct i2c_client *client = to_i2c_client(dev);
1498 struct tps6598x *tps = i2c_get_clientdata(client);
1499 int ret;
1500
1501 ret = tps6598x_check_mode(tps);
1502 if (ret < 0)
1503 return ret;
1504
1505 if (ret == TPS_MODE_PTCH) {
1506 ret = tps->data->init(tps);
1507 if (ret)
1508 return ret;
1509 }
1510
1511 if (tps->wakeup) {
1512 disable_irq_wake(client->irq);
1513 enable_irq(client->irq);
1514 } else if (tps->reset) {
1515 gpiod_set_value_cansleep(tps->reset, 0);
1516 msleep(TPS_SETUP_MS);
1517 }
1518
1519 if (!client->irq)
1520 queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,
1521 msecs_to_jiffies(POLL_INTERVAL));
1522
1523 return 0;
1524 }
1525
1526 static const struct dev_pm_ops tps6598x_pm_ops = {
1527 SET_SYSTEM_SLEEP_PM_OPS(tps6598x_suspend, tps6598x_resume)
1528 };
1529
1530 static const struct tipd_data cd321x_data = {
1531 .irq_handler = cd321x_interrupt,
1532 .register_port = tps6598x_register_port,
1533 .trace_power_status = trace_tps6598x_power_status,
1534 .trace_status = trace_tps6598x_status,
1535 .init = cd321x_init,
1536 .reset = cd321x_reset,
1537 };
1538
1539 static const struct tipd_data tps6598x_data = {
1540 .irq_handler = tps6598x_interrupt,
1541 .register_port = tps6598x_register_port,
1542 .trace_power_status = trace_tps6598x_power_status,
1543 .trace_status = trace_tps6598x_status,
1544 .apply_patch = tps6598x_apply_patch,
1545 .init = tps6598x_init,
1546 .reset = tps6598x_reset,
1547 };
1548
1549 static const struct tipd_data tps25750_data = {
1550 .irq_handler = tps25750_interrupt,
1551 .register_port = tps25750_register_port,
1552 .trace_power_status = trace_tps25750_power_status,
1553 .trace_status = trace_tps25750_status,
1554 .apply_patch = tps25750_apply_patch,
1555 .init = tps25750_init,
1556 .reset = tps25750_reset,
1557 };
1558
1559 static const struct of_device_id tps6598x_of_match[] = {
1560 { .compatible = "ti,tps6598x", &tps6598x_data},
1561 { .compatible = "apple,cd321x", &cd321x_data},
1562 { .compatible = "ti,tps25750", &tps25750_data},
1563 {}
1564 };
1565 MODULE_DEVICE_TABLE(of, tps6598x_of_match);
1566
1567 static const struct i2c_device_id tps6598x_id[] = {
1568 { "tps6598x", (kernel_ulong_t)&tps6598x_data },
1569 { }
1570 };
1571 MODULE_DEVICE_TABLE(i2c, tps6598x_id);
1572
1573 static struct i2c_driver tps6598x_i2c_driver = {
1574 .driver = {
1575 .name = "tps6598x",
1576 .pm = &tps6598x_pm_ops,
1577 .of_match_table = tps6598x_of_match,
1578 },
1579 .probe = tps6598x_probe,
1580 .remove = tps6598x_remove,
1581 .id_table = tps6598x_id,
1582 };
1583 module_i2c_driver(tps6598x_i2c_driver);
1584
1585 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1586 MODULE_LICENSE("GPL v2");
1587 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");
1588