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/module.h>
12 #include <linux/of.h>
13 #include <linux/power_supply.h>
14 #include <linux/regmap.h>
15 #include <linux/interrupt.h>
16 #include <linux/usb/typec.h>
17 #include <linux/usb/typec_altmode.h>
18 #include <linux/usb/role.h>
19 #include <linux/workqueue.h>
20
21 #include "tps6598x.h"
22 #include "trace.h"
23
24 /* Register offsets */
25 #define TPS_REG_VID 0x00
26 #define TPS_REG_MODE 0x03
27 #define TPS_REG_CMD1 0x08
28 #define TPS_REG_DATA1 0x09
29 #define TPS_REG_INT_EVENT1 0x14
30 #define TPS_REG_INT_EVENT2 0x15
31 #define TPS_REG_INT_MASK1 0x16
32 #define TPS_REG_INT_MASK2 0x17
33 #define TPS_REG_INT_CLEAR1 0x18
34 #define TPS_REG_INT_CLEAR2 0x19
35 #define TPS_REG_SYSTEM_POWER_STATE 0x20
36 #define TPS_REG_STATUS 0x1a
37 #define TPS_REG_SYSTEM_CONF 0x28
38 #define TPS_REG_CTRL_CONF 0x29
39 #define TPS_REG_POWER_STATUS 0x3f
40 #define TPS_REG_RX_IDENTITY_SOP 0x48
41 #define TPS_REG_DATA_STATUS 0x5f
42
43 /* TPS_REG_SYSTEM_CONF bits */
44 #define TPS_SYSCONF_PORTINFO(c) ((c) & 7)
45
46 enum {
47 TPS_PORTINFO_SINK,
48 TPS_PORTINFO_SINK_ACCESSORY,
49 TPS_PORTINFO_DRP_UFP,
50 TPS_PORTINFO_DRP_UFP_DRD,
51 TPS_PORTINFO_DRP_DFP,
52 TPS_PORTINFO_DRP_DFP_DRD,
53 TPS_PORTINFO_SOURCE,
54 };
55
56 /* TPS_REG_RX_IDENTITY_SOP */
57 struct tps6598x_rx_identity_reg {
58 u8 status;
59 struct usb_pd_identity identity;
60 } __packed;
61
62 /* Standard Task return codes */
63 #define TPS_TASK_TIMEOUT 1
64 #define TPS_TASK_REJECTED 3
65
66 enum {
67 TPS_MODE_APP,
68 TPS_MODE_BOOT,
69 TPS_MODE_BIST,
70 TPS_MODE_DISC,
71 };
72
73 static const char *const modes[] = {
74 [TPS_MODE_APP] = "APP ",
75 [TPS_MODE_BOOT] = "BOOT",
76 [TPS_MODE_BIST] = "BIST",
77 [TPS_MODE_DISC] = "DISC",
78 };
79
80 /* Unrecognized commands will be replaced with "!CMD" */
81 #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
82
83 struct tps6598x {
84 struct device *dev;
85 struct regmap *regmap;
86 struct mutex lock; /* device lock */
87 u8 i2c_protocol:1;
88
89 struct typec_port *port;
90 struct typec_partner *partner;
91 struct usb_pd_identity partner_identity;
92 struct usb_role_switch *role_sw;
93 struct typec_capability typec_cap;
94
95 struct power_supply *psy;
96 struct power_supply_desc psy_desc;
97 enum power_supply_usb_type usb_type;
98
99 int wakeup;
100 u16 pwr_status;
101 struct delayed_work wq_poll;
102 irq_handler_t irq_handler;
103 };
104
105 static enum power_supply_property tps6598x_psy_props[] = {
106 POWER_SUPPLY_PROP_USB_TYPE,
107 POWER_SUPPLY_PROP_ONLINE,
108 };
109
110 static enum power_supply_usb_type tps6598x_psy_usb_types[] = {
111 POWER_SUPPLY_USB_TYPE_C,
112 POWER_SUPPLY_USB_TYPE_PD,
113 };
114
115 static const char *tps6598x_psy_name_prefix = "tps6598x-source-psy-";
116
117 /*
118 * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
119 * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
120 */
121 #define TPS_MAX_LEN 64
122
123 static int
tps6598x_block_read(struct tps6598x * tps,u8 reg,void * val,size_t len)124 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
125 {
126 u8 data[TPS_MAX_LEN + 1];
127 int ret;
128
129 if (len + 1 > sizeof(data))
130 return -EINVAL;
131
132 if (!tps->i2c_protocol)
133 return regmap_raw_read(tps->regmap, reg, val, len);
134
135 ret = regmap_raw_read(tps->regmap, reg, data, len + 1);
136 if (ret)
137 return ret;
138
139 if (data[0] < len)
140 return -EIO;
141
142 memcpy(val, &data[1], len);
143 return 0;
144 }
145
tps6598x_block_write(struct tps6598x * tps,u8 reg,const void * val,size_t len)146 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
147 const void *val, size_t len)
148 {
149 u8 data[TPS_MAX_LEN + 1];
150
151 if (len + 1 > sizeof(data))
152 return -EINVAL;
153
154 if (!tps->i2c_protocol)
155 return regmap_raw_write(tps->regmap, reg, val, len);
156
157 data[0] = len;
158 memcpy(&data[1], val, len);
159
160 return regmap_raw_write(tps->regmap, reg, data, len + 1);
161 }
162
tps6598x_read8(struct tps6598x * tps,u8 reg,u8 * val)163 static inline int tps6598x_read8(struct tps6598x *tps, u8 reg, u8 *val)
164 {
165 return tps6598x_block_read(tps, reg, val, sizeof(u8));
166 }
167
tps6598x_read16(struct tps6598x * tps,u8 reg,u16 * val)168 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
169 {
170 return tps6598x_block_read(tps, reg, val, sizeof(u16));
171 }
172
tps6598x_read32(struct tps6598x * tps,u8 reg,u32 * val)173 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
174 {
175 return tps6598x_block_read(tps, reg, val, sizeof(u32));
176 }
177
tps6598x_read64(struct tps6598x * tps,u8 reg,u64 * val)178 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
179 {
180 return tps6598x_block_read(tps, reg, val, sizeof(u64));
181 }
182
tps6598x_write64(struct tps6598x * tps,u8 reg,u64 val)183 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
184 {
185 return tps6598x_block_write(tps, reg, &val, sizeof(u64));
186 }
187
188 static inline int
tps6598x_write_4cc(struct tps6598x * tps,u8 reg,const char * val)189 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
190 {
191 return tps6598x_block_write(tps, reg, val, 4);
192 }
193
tps6598x_read_partner_identity(struct tps6598x * tps)194 static int tps6598x_read_partner_identity(struct tps6598x *tps)
195 {
196 struct tps6598x_rx_identity_reg id;
197 int ret;
198
199 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
200 &id, sizeof(id));
201 if (ret)
202 return ret;
203
204 tps->partner_identity = id.identity;
205
206 return 0;
207 }
208
tps6598x_set_data_role(struct tps6598x * tps,enum typec_data_role role,bool connected)209 static void tps6598x_set_data_role(struct tps6598x *tps,
210 enum typec_data_role role, bool connected)
211 {
212 enum usb_role role_val;
213
214 if (role == TYPEC_HOST)
215 role_val = USB_ROLE_HOST;
216 else
217 role_val = USB_ROLE_DEVICE;
218
219 if (!connected)
220 role_val = USB_ROLE_NONE;
221
222 usb_role_switch_set_role(tps->role_sw, role_val);
223 typec_set_data_role(tps->port, role);
224 }
225
tps6598x_connect(struct tps6598x * tps,u32 status)226 static int tps6598x_connect(struct tps6598x *tps, u32 status)
227 {
228 struct typec_partner_desc desc;
229 enum typec_pwr_opmode mode;
230 int ret;
231
232 if (tps->partner)
233 return 0;
234
235 mode = TPS_POWER_STATUS_PWROPMODE(tps->pwr_status);
236
237 desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
238 desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
239 desc.identity = NULL;
240
241 if (desc.usb_pd) {
242 ret = tps6598x_read_partner_identity(tps);
243 if (ret)
244 return ret;
245 desc.identity = &tps->partner_identity;
246 }
247
248 typec_set_pwr_opmode(tps->port, mode);
249 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
250 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
251 if (TPS_STATUS_TO_UPSIDE_DOWN(status))
252 typec_set_orientation(tps->port, TYPEC_ORIENTATION_REVERSE);
253 else
254 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NORMAL);
255 typec_set_mode(tps->port, TYPEC_STATE_USB);
256 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), true);
257
258 tps->partner = typec_register_partner(tps->port, &desc);
259 if (IS_ERR(tps->partner))
260 return PTR_ERR(tps->partner);
261
262 if (desc.identity)
263 typec_partner_set_identity(tps->partner);
264
265 power_supply_changed(tps->psy);
266
267 return 0;
268 }
269
tps6598x_disconnect(struct tps6598x * tps,u32 status)270 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
271 {
272 if (!IS_ERR(tps->partner))
273 typec_unregister_partner(tps->partner);
274 tps->partner = NULL;
275 typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
276 typec_set_pwr_role(tps->port, TPS_STATUS_TO_TYPEC_PORTROLE(status));
277 typec_set_vconn_role(tps->port, TPS_STATUS_TO_TYPEC_VCONN(status));
278 typec_set_orientation(tps->port, TYPEC_ORIENTATION_NONE);
279 typec_set_mode(tps->port, TYPEC_STATE_SAFE);
280 tps6598x_set_data_role(tps, TPS_STATUS_TO_TYPEC_DATAROLE(status), false);
281
282 power_supply_changed(tps->psy);
283 }
284
tps6598x_exec_cmd(struct tps6598x * tps,const char * cmd,size_t in_len,u8 * in_data,size_t out_len,u8 * out_data)285 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
286 size_t in_len, u8 *in_data,
287 size_t out_len, u8 *out_data)
288 {
289 unsigned long timeout;
290 u32 val;
291 int ret;
292
293 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
294 if (ret)
295 return ret;
296 if (val && !INVALID_CMD(val))
297 return -EBUSY;
298
299 if (in_len) {
300 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
301 in_data, in_len);
302 if (ret)
303 return ret;
304 }
305
306 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
307 if (ret < 0)
308 return ret;
309
310 /* XXX: Using 1s for now, but it may not be enough for every command. */
311 timeout = jiffies + msecs_to_jiffies(1000);
312
313 do {
314 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
315 if (ret)
316 return ret;
317 if (INVALID_CMD(val))
318 return -EINVAL;
319
320 if (time_is_before_jiffies(timeout))
321 return -ETIMEDOUT;
322 } while (val);
323
324 if (out_len) {
325 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
326 out_data, out_len);
327 if (ret)
328 return ret;
329 val = out_data[0];
330 } else {
331 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
332 if (ret)
333 return ret;
334 }
335
336 switch (val) {
337 case TPS_TASK_TIMEOUT:
338 return -ETIMEDOUT;
339 case TPS_TASK_REJECTED:
340 return -EPERM;
341 default:
342 break;
343 }
344
345 return 0;
346 }
347
tps6598x_dr_set(struct typec_port * port,enum typec_data_role role)348 static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
349 {
350 const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
351 struct tps6598x *tps = typec_get_drvdata(port);
352 u32 status;
353 int ret;
354
355 mutex_lock(&tps->lock);
356
357 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
358 if (ret)
359 goto out_unlock;
360
361 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
362 if (ret)
363 goto out_unlock;
364
365 if (role != TPS_STATUS_TO_TYPEC_DATAROLE(status)) {
366 ret = -EPROTO;
367 goto out_unlock;
368 }
369
370 tps6598x_set_data_role(tps, role, true);
371
372 out_unlock:
373 mutex_unlock(&tps->lock);
374
375 return ret;
376 }
377
tps6598x_pr_set(struct typec_port * port,enum typec_role role)378 static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
379 {
380 const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
381 struct tps6598x *tps = typec_get_drvdata(port);
382 u32 status;
383 int ret;
384
385 mutex_lock(&tps->lock);
386
387 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
388 if (ret)
389 goto out_unlock;
390
391 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
392 if (ret)
393 goto out_unlock;
394
395 if (role != TPS_STATUS_TO_TYPEC_PORTROLE(status)) {
396 ret = -EPROTO;
397 goto out_unlock;
398 }
399
400 typec_set_pwr_role(tps->port, role);
401
402 out_unlock:
403 mutex_unlock(&tps->lock);
404
405 return ret;
406 }
407
408 static const struct typec_operations tps6598x_ops = {
409 .dr_set = tps6598x_dr_set,
410 .pr_set = tps6598x_pr_set,
411 };
412
tps6598x_read_status(struct tps6598x * tps,u32 * status)413 static bool tps6598x_read_status(struct tps6598x *tps, u32 *status)
414 {
415 int ret;
416
417 ret = tps6598x_read32(tps, TPS_REG_STATUS, status);
418 if (ret) {
419 dev_err(tps->dev, "%s: failed to read status\n", __func__);
420 return false;
421 }
422 trace_tps6598x_status(*status);
423
424 return true;
425 }
426
tps6598x_read_data_status(struct tps6598x * tps)427 static bool tps6598x_read_data_status(struct tps6598x *tps)
428 {
429 u32 data_status;
430 int ret;
431
432 ret = tps6598x_read32(tps, TPS_REG_DATA_STATUS, &data_status);
433 if (ret < 0) {
434 dev_err(tps->dev, "failed to read data status: %d\n", ret);
435 return false;
436 }
437 trace_tps6598x_data_status(data_status);
438
439 return true;
440 }
441
tps6598x_read_power_status(struct tps6598x * tps)442 static bool tps6598x_read_power_status(struct tps6598x *tps)
443 {
444 u16 pwr_status;
445 int ret;
446
447 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
448 if (ret < 0) {
449 dev_err(tps->dev, "failed to read power status: %d\n", ret);
450 return false;
451 }
452 tps->pwr_status = pwr_status;
453 trace_tps6598x_power_status(pwr_status);
454
455 return true;
456 }
457
tps6598x_handle_plug_event(struct tps6598x * tps,u32 status)458 static void tps6598x_handle_plug_event(struct tps6598x *tps, u32 status)
459 {
460 int ret;
461
462 if (status & TPS_STATUS_PLUG_PRESENT) {
463 ret = tps6598x_connect(tps, status);
464 if (ret)
465 dev_err(tps->dev, "failed to register partner\n");
466 } else {
467 tps6598x_disconnect(tps, status);
468 }
469 }
470
cd321x_interrupt(int irq,void * data)471 static irqreturn_t cd321x_interrupt(int irq, void *data)
472 {
473 struct tps6598x *tps = data;
474 u64 event = 0;
475 u32 status;
476 int ret;
477
478 mutex_lock(&tps->lock);
479
480 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event);
481 if (ret) {
482 dev_err(tps->dev, "%s: failed to read events\n", __func__);
483 goto err_unlock;
484 }
485 trace_cd321x_irq(event);
486
487 if (!event)
488 goto err_unlock;
489
490 if (!tps6598x_read_status(tps, &status))
491 goto err_clear_ints;
492
493 if (event & APPLE_CD_REG_INT_POWER_STATUS_UPDATE)
494 if (!tps6598x_read_power_status(tps))
495 goto err_clear_ints;
496
497 if (event & APPLE_CD_REG_INT_DATA_STATUS_UPDATE)
498 if (!tps6598x_read_data_status(tps))
499 goto err_clear_ints;
500
501 /* Handle plug insert or removal */
502 if (event & APPLE_CD_REG_INT_PLUG_EVENT)
503 tps6598x_handle_plug_event(tps, status);
504
505 err_clear_ints:
506 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event);
507
508 err_unlock:
509 mutex_unlock(&tps->lock);
510
511 if (event)
512 return IRQ_HANDLED;
513 return IRQ_NONE;
514 }
515
tps6598x_interrupt(int irq,void * data)516 static irqreturn_t tps6598x_interrupt(int irq, void *data)
517 {
518 struct tps6598x *tps = data;
519 u64 event1 = 0;
520 u64 event2 = 0;
521 u32 status;
522 int ret;
523
524 mutex_lock(&tps->lock);
525
526 ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
527 ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
528 if (ret) {
529 dev_err(tps->dev, "%s: failed to read events\n", __func__);
530 goto err_unlock;
531 }
532 trace_tps6598x_irq(event1, event2);
533
534 if (!(event1 | event2))
535 goto err_unlock;
536
537 if (!tps6598x_read_status(tps, &status))
538 goto err_clear_ints;
539
540 if ((event1 | event2) & TPS_REG_INT_POWER_STATUS_UPDATE)
541 if (!tps6598x_read_power_status(tps))
542 goto err_clear_ints;
543
544 if ((event1 | event2) & TPS_REG_INT_DATA_STATUS_UPDATE)
545 if (!tps6598x_read_data_status(tps))
546 goto err_clear_ints;
547
548 /* Handle plug insert or removal */
549 if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT)
550 tps6598x_handle_plug_event(tps, status);
551
552 err_clear_ints:
553 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
554 tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
555
556 err_unlock:
557 mutex_unlock(&tps->lock);
558
559 if (event1 | event2)
560 return IRQ_HANDLED;
561 return IRQ_NONE;
562 }
563
564 /* Time interval for Polling */
565 #define POLL_INTERVAL 500 /* msecs */
tps6598x_poll_work(struct work_struct * work)566 static void tps6598x_poll_work(struct work_struct *work)
567 {
568 struct tps6598x *tps = container_of(to_delayed_work(work),
569 struct tps6598x, wq_poll);
570
571 tps->irq_handler(0, tps);
572 queue_delayed_work(system_power_efficient_wq,
573 &tps->wq_poll, msecs_to_jiffies(POLL_INTERVAL));
574 }
575
tps6598x_check_mode(struct tps6598x * tps)576 static int tps6598x_check_mode(struct tps6598x *tps)
577 {
578 char mode[5] = { };
579 int ret;
580
581 ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
582 if (ret)
583 return ret;
584
585 switch (match_string(modes, ARRAY_SIZE(modes), mode)) {
586 case TPS_MODE_APP:
587 return 0;
588 case TPS_MODE_BOOT:
589 dev_warn(tps->dev, "dead-battery condition\n");
590 return 0;
591 case TPS_MODE_BIST:
592 case TPS_MODE_DISC:
593 default:
594 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
595 mode);
596 break;
597 }
598
599 return -ENODEV;
600 }
601
602 static const struct regmap_config tps6598x_regmap_config = {
603 .reg_bits = 8,
604 .val_bits = 8,
605 .max_register = 0x7F,
606 };
607
tps6598x_psy_get_online(struct tps6598x * tps,union power_supply_propval * val)608 static int tps6598x_psy_get_online(struct tps6598x *tps,
609 union power_supply_propval *val)
610 {
611 if (TPS_POWER_STATUS_CONNECTION(tps->pwr_status) &&
612 TPS_POWER_STATUS_SOURCESINK(tps->pwr_status)) {
613 val->intval = 1;
614 } else {
615 val->intval = 0;
616 }
617 return 0;
618 }
619
tps6598x_psy_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)620 static int tps6598x_psy_get_prop(struct power_supply *psy,
621 enum power_supply_property psp,
622 union power_supply_propval *val)
623 {
624 struct tps6598x *tps = power_supply_get_drvdata(psy);
625 int ret = 0;
626
627 switch (psp) {
628 case POWER_SUPPLY_PROP_USB_TYPE:
629 if (TPS_POWER_STATUS_PWROPMODE(tps->pwr_status) == TYPEC_PWR_MODE_PD)
630 val->intval = POWER_SUPPLY_USB_TYPE_PD;
631 else
632 val->intval = POWER_SUPPLY_USB_TYPE_C;
633 break;
634 case POWER_SUPPLY_PROP_ONLINE:
635 ret = tps6598x_psy_get_online(tps, val);
636 break;
637 default:
638 ret = -EINVAL;
639 break;
640 }
641
642 return ret;
643 }
644
cd321x_switch_power_state(struct tps6598x * tps,u8 target_state)645 static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state)
646 {
647 u8 state;
648 int ret;
649
650 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
651 if (ret)
652 return ret;
653
654 if (state == target_state)
655 return 0;
656
657 ret = tps6598x_exec_cmd(tps, "SSPS", sizeof(u8), &target_state, 0, NULL);
658 if (ret)
659 return ret;
660
661 ret = tps6598x_read8(tps, TPS_REG_SYSTEM_POWER_STATE, &state);
662 if (ret)
663 return ret;
664
665 if (state != target_state)
666 return -EINVAL;
667
668 return 0;
669 }
670
devm_tps6598_psy_register(struct tps6598x * tps)671 static int devm_tps6598_psy_register(struct tps6598x *tps)
672 {
673 struct power_supply_config psy_cfg = {};
674 const char *port_dev_name = dev_name(tps->dev);
675 char *psy_name;
676
677 psy_cfg.drv_data = tps;
678 psy_cfg.fwnode = dev_fwnode(tps->dev);
679
680 psy_name = devm_kasprintf(tps->dev, GFP_KERNEL, "%s%s", tps6598x_psy_name_prefix,
681 port_dev_name);
682 if (!psy_name)
683 return -ENOMEM;
684
685 tps->psy_desc.name = psy_name;
686 tps->psy_desc.type = POWER_SUPPLY_TYPE_USB;
687 tps->psy_desc.usb_types = tps6598x_psy_usb_types;
688 tps->psy_desc.num_usb_types = ARRAY_SIZE(tps6598x_psy_usb_types);
689 tps->psy_desc.properties = tps6598x_psy_props;
690 tps->psy_desc.num_properties = ARRAY_SIZE(tps6598x_psy_props);
691 tps->psy_desc.get_property = tps6598x_psy_get_prop;
692
693 tps->usb_type = POWER_SUPPLY_USB_TYPE_C;
694
695 tps->psy = devm_power_supply_register(tps->dev, &tps->psy_desc,
696 &psy_cfg);
697 return PTR_ERR_OR_ZERO(tps->psy);
698 }
699
tps6598x_probe(struct i2c_client * client)700 static int tps6598x_probe(struct i2c_client *client)
701 {
702 irq_handler_t irq_handler = tps6598x_interrupt;
703 struct device_node *np = client->dev.of_node;
704 struct typec_capability typec_cap = { };
705 struct tps6598x *tps;
706 struct fwnode_handle *fwnode;
707 u32 status;
708 u32 conf;
709 u32 vid;
710 int ret;
711 u64 mask1;
712
713 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
714 if (!tps)
715 return -ENOMEM;
716
717 mutex_init(&tps->lock);
718 tps->dev = &client->dev;
719
720 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
721 if (IS_ERR(tps->regmap))
722 return PTR_ERR(tps->regmap);
723
724 ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
725 if (ret < 0 || !vid)
726 return -ENODEV;
727
728 /*
729 * Checking can the adapter handle SMBus protocol. If it can not, the
730 * driver needs to take care of block reads separately.
731 */
732 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
733 tps->i2c_protocol = true;
734
735 if (np && of_device_is_compatible(np, "apple,cd321x")) {
736 /* Switch CD321X chips to the correct system power state */
737 ret = cd321x_switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0);
738 if (ret)
739 return ret;
740
741 /* CD321X chips have all interrupts masked initially */
742 mask1 = APPLE_CD_REG_INT_POWER_STATUS_UPDATE |
743 APPLE_CD_REG_INT_DATA_STATUS_UPDATE |
744 APPLE_CD_REG_INT_PLUG_EVENT;
745
746 irq_handler = cd321x_interrupt;
747 } else {
748 /* Enable power status, data status and plug event interrupts */
749 mask1 = TPS_REG_INT_POWER_STATUS_UPDATE |
750 TPS_REG_INT_DATA_STATUS_UPDATE |
751 TPS_REG_INT_PLUG_EVENT;
752 }
753
754 tps->irq_handler = irq_handler;
755 /* Make sure the controller has application firmware running */
756 ret = tps6598x_check_mode(tps);
757 if (ret)
758 return ret;
759
760 ret = tps6598x_write64(tps, TPS_REG_INT_MASK1, mask1);
761 if (ret)
762 return ret;
763
764 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
765 if (ret < 0)
766 goto err_clear_mask;
767 trace_tps6598x_status(status);
768
769 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
770 if (ret < 0)
771 goto err_clear_mask;
772
773 /*
774 * This fwnode has a "compatible" property, but is never populated as a
775 * struct device. Instead we simply parse it to read the properties.
776 * This breaks fw_devlink=on. To maintain backward compatibility
777 * with existing DT files, we work around this by deleting any
778 * fwnode_links to/from this fwnode.
779 */
780 fwnode = device_get_named_child_node(&client->dev, "connector");
781 if (fwnode)
782 fw_devlink_purge_absent_suppliers(fwnode);
783
784 tps->role_sw = fwnode_usb_role_switch_get(fwnode);
785 if (IS_ERR(tps->role_sw)) {
786 ret = PTR_ERR(tps->role_sw);
787 goto err_fwnode_put;
788 }
789
790 typec_cap.revision = USB_TYPEC_REV_1_2;
791 typec_cap.pd_revision = 0x200;
792 typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
793 typec_cap.driver_data = tps;
794 typec_cap.ops = &tps6598x_ops;
795 typec_cap.fwnode = fwnode;
796
797 switch (TPS_SYSCONF_PORTINFO(conf)) {
798 case TPS_PORTINFO_SINK_ACCESSORY:
799 case TPS_PORTINFO_SINK:
800 typec_cap.type = TYPEC_PORT_SNK;
801 typec_cap.data = TYPEC_PORT_UFP;
802 break;
803 case TPS_PORTINFO_DRP_UFP_DRD:
804 case TPS_PORTINFO_DRP_DFP_DRD:
805 typec_cap.type = TYPEC_PORT_DRP;
806 typec_cap.data = TYPEC_PORT_DRD;
807 break;
808 case TPS_PORTINFO_DRP_UFP:
809 typec_cap.type = TYPEC_PORT_DRP;
810 typec_cap.data = TYPEC_PORT_UFP;
811 break;
812 case TPS_PORTINFO_DRP_DFP:
813 typec_cap.type = TYPEC_PORT_DRP;
814 typec_cap.data = TYPEC_PORT_DFP;
815 break;
816 case TPS_PORTINFO_SOURCE:
817 typec_cap.type = TYPEC_PORT_SRC;
818 typec_cap.data = TYPEC_PORT_DFP;
819 break;
820 default:
821 ret = -ENODEV;
822 goto err_role_put;
823 }
824
825 ret = devm_tps6598_psy_register(tps);
826 if (ret)
827 goto err_role_put;
828
829 tps->port = typec_register_port(&client->dev, &typec_cap);
830 if (IS_ERR(tps->port)) {
831 ret = PTR_ERR(tps->port);
832 goto err_role_put;
833 }
834
835 if (status & TPS_STATUS_PLUG_PRESENT) {
836 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &tps->pwr_status);
837 if (ret < 0) {
838 dev_err(tps->dev, "failed to read power status: %d\n", ret);
839 goto err_unregister_port;
840 }
841 ret = tps6598x_connect(tps, status);
842 if (ret)
843 dev_err(&client->dev, "failed to register partner\n");
844 }
845
846 if (client->irq) {
847 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
848 irq_handler,
849 IRQF_SHARED | IRQF_ONESHOT,
850 dev_name(&client->dev), tps);
851 } else {
852 dev_warn(tps->dev, "Unable to find the interrupt, switching to polling\n");
853 INIT_DELAYED_WORK(&tps->wq_poll, tps6598x_poll_work);
854 queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,
855 msecs_to_jiffies(POLL_INTERVAL));
856 }
857
858 if (ret)
859 goto err_disconnect;
860
861 i2c_set_clientdata(client, tps);
862 fwnode_handle_put(fwnode);
863
864 tps->wakeup = device_property_read_bool(tps->dev, "wakeup-source");
865 if (tps->wakeup && client->irq) {
866 device_init_wakeup(&client->dev, true);
867 enable_irq_wake(client->irq);
868 }
869
870 return 0;
871
872 err_disconnect:
873 tps6598x_disconnect(tps, 0);
874 err_unregister_port:
875 typec_unregister_port(tps->port);
876 err_role_put:
877 usb_role_switch_put(tps->role_sw);
878 err_fwnode_put:
879 fwnode_handle_put(fwnode);
880 err_clear_mask:
881 tps6598x_write64(tps, TPS_REG_INT_MASK1, 0);
882 return ret;
883 }
884
tps6598x_remove(struct i2c_client * client)885 static void tps6598x_remove(struct i2c_client *client)
886 {
887 struct tps6598x *tps = i2c_get_clientdata(client);
888
889 if (!client->irq)
890 cancel_delayed_work_sync(&tps->wq_poll);
891
892 tps6598x_disconnect(tps, 0);
893 typec_unregister_port(tps->port);
894 usb_role_switch_put(tps->role_sw);
895 }
896
tps6598x_suspend(struct device * dev)897 static int __maybe_unused tps6598x_suspend(struct device *dev)
898 {
899 struct i2c_client *client = to_i2c_client(dev);
900 struct tps6598x *tps = i2c_get_clientdata(client);
901
902 if (tps->wakeup) {
903 disable_irq(client->irq);
904 enable_irq_wake(client->irq);
905 }
906
907 if (!client->irq)
908 cancel_delayed_work_sync(&tps->wq_poll);
909
910 return 0;
911 }
912
tps6598x_resume(struct device * dev)913 static int __maybe_unused tps6598x_resume(struct device *dev)
914 {
915 struct i2c_client *client = to_i2c_client(dev);
916 struct tps6598x *tps = i2c_get_clientdata(client);
917
918 if (tps->wakeup) {
919 disable_irq_wake(client->irq);
920 enable_irq(client->irq);
921 }
922
923 if (!client->irq)
924 queue_delayed_work(system_power_efficient_wq, &tps->wq_poll,
925 msecs_to_jiffies(POLL_INTERVAL));
926
927 return 0;
928 }
929
930 static const struct dev_pm_ops tps6598x_pm_ops = {
931 SET_SYSTEM_SLEEP_PM_OPS(tps6598x_suspend, tps6598x_resume)
932 };
933
934 static const struct of_device_id tps6598x_of_match[] = {
935 { .compatible = "ti,tps6598x", },
936 { .compatible = "apple,cd321x", },
937 {}
938 };
939 MODULE_DEVICE_TABLE(of, tps6598x_of_match);
940
941 static const struct i2c_device_id tps6598x_id[] = {
942 { "tps6598x" },
943 { }
944 };
945 MODULE_DEVICE_TABLE(i2c, tps6598x_id);
946
947 static struct i2c_driver tps6598x_i2c_driver = {
948 .driver = {
949 .name = "tps6598x",
950 .pm = &tps6598x_pm_ops,
951 .of_match_table = tps6598x_of_match,
952 },
953 .probe = tps6598x_probe,
954 .remove = tps6598x_remove,
955 .id_table = tps6598x_id,
956 };
957 module_i2c_driver(tps6598x_i2c_driver);
958
959 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
960 MODULE_LICENSE("GPL v2");
961 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");
962