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