1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
// SPDX-License-Identifier: GPL-2.0
/*
* Driver for TI TPS6598x USB Power Delivery controller family
*
* Copyright (C) 2017, Intel Corporation
* Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
*/
#include <linux/i2c.h>
#include <linux/acpi.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/interrupt.h>
#include <linux/usb/typec.h>
/* Register offsets */
#define TPS_REG_CMD1 0x08
#define TPS_REG_DATA1 0x09
#define TPS_REG_INT_EVENT1 0x14
#define TPS_REG_INT_EVENT2 0x15
#define TPS_REG_INT_MASK1 0x16
#define TPS_REG_INT_MASK2 0x17
#define TPS_REG_INT_CLEAR1 0x18
#define TPS_REG_INT_CLEAR2 0x19
#define TPS_REG_STATUS 0x1a
#define TPS_REG_SYSTEM_CONF 0x28
#define TPS_REG_CTRL_CONF 0x29
#define TPS_REG_POWER_STATUS 0x3f
#define TPS_REG_RX_IDENTITY_SOP 0x48
/* TPS_REG_INT_* bits */
#define TPS_REG_INT_PLUG_EVENT BIT(3)
/* TPS_REG_STATUS bits */
#define TPS_STATUS_PLUG_PRESENT BIT(0)
#define TPS_STATUS_ORIENTATION BIT(4)
#define TPS_STATUS_PORTROLE(s) (!!((s) & BIT(5)))
#define TPS_STATUS_DATAROLE(s) (!!((s) & BIT(6)))
#define TPS_STATUS_VCONN(s) (!!((s) & BIT(7)))
/* TPS_REG_SYSTEM_CONF bits */
#define TPS_SYSCONF_PORTINFO(c) ((c) & 3)
enum {
TPS_PORTINFO_SINK,
TPS_PORTINFO_SINK_ACCESSORY,
TPS_PORTINFO_DRP_UFP,
TPS_PORTINFO_DRP_UFP_DRD,
TPS_PORTINFO_DRP_DFP,
TPS_PORTINFO_DRP_DFP_DRD,
TPS_PORTINFO_SOURCE,
};
/* TPS_REG_POWER_STATUS bits */
#define TPS_POWER_STATUS_SOURCESINK BIT(1)
#define TPS_POWER_STATUS_PWROPMODE(p) (((p) & GENMASK(3, 2)) >> 2)
/* TPS_REG_RX_IDENTITY_SOP */
struct tps6598x_rx_identity_reg {
u8 status;
struct usb_pd_identity identity;
u32 vdo[3];
} __packed;
/* Standard Task return codes */
#define TPS_TASK_TIMEOUT 1
#define TPS_TASK_REJECTED 3
/* Unrecognized commands will be replaced with "!CMD" */
#define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
struct tps6598x {
struct device *dev;
struct regmap *regmap;
struct mutex lock; /* device lock */
struct typec_port *port;
struct typec_partner *partner;
struct usb_pd_identity partner_identity;
struct typec_capability typec_cap;
};
static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
{
return regmap_raw_read(tps->regmap, reg, val, sizeof(u16));
}
static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
{
return regmap_raw_read(tps->regmap, reg, val, sizeof(u32));
}
static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
{
return regmap_raw_read(tps->regmap, reg, val, sizeof(u64));
}
static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
{
return regmap_raw_write(tps->regmap, reg, &val, sizeof(u16));
}
static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
{
return regmap_raw_write(tps->regmap, reg, &val, sizeof(u32));
}
static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
{
return regmap_raw_write(tps->regmap, reg, &val, sizeof(u64));
}
static inline int
tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
{
return regmap_raw_write(tps->regmap, reg, &val, sizeof(u32));
}
static int tps6598x_read_partner_identity(struct tps6598x *tps)
{
struct tps6598x_rx_identity_reg id;
int ret;
ret = regmap_raw_read(tps->regmap, TPS_REG_RX_IDENTITY_SOP,
&id, sizeof(id));
if (ret)
return ret;
tps->partner_identity = id.identity;
return 0;
}
static int tps6598x_connect(struct tps6598x *tps, u32 status)
{
struct typec_partner_desc desc;
enum typec_pwr_opmode mode;
u16 pwr_status;
int ret;
if (tps->partner)
return 0;
ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
if (ret < 0)
return ret;
mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
desc.identity = NULL;
if (desc.usb_pd) {
ret = tps6598x_read_partner_identity(tps);
if (ret)
return ret;
desc.identity = &tps->partner_identity;
}
typec_set_pwr_opmode(tps->port, mode);
typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
tps->partner = typec_register_partner(tps->port, &desc);
if (IS_ERR(tps->partner))
return PTR_ERR(tps->partner);
if (desc.identity)
typec_partner_set_identity(tps->partner);
return 0;
}
static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
{
if (!IS_ERR(tps->partner))
typec_unregister_partner(tps->partner);
tps->partner = NULL;
typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
}
static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
size_t in_len, u8 *in_data,
size_t out_len, u8 *out_data)
{
unsigned long timeout;
u32 val;
int ret;
ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
if (ret)
return ret;
if (val && !INVALID_CMD(val))
return -EBUSY;
if (in_len) {
ret = regmap_raw_write(tps->regmap, TPS_REG_DATA1,
in_data, in_len);
if (ret)
return ret;
}
ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
if (ret < 0)
return ret;
/* XXX: Using 1s for now, but it may not be enough for every command. */
timeout = jiffies + msecs_to_jiffies(1000);
do {
ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
if (ret)
return ret;
if (INVALID_CMD(val))
return -EINVAL;
if (time_is_before_jiffies(timeout))
return -ETIMEDOUT;
} while (val);
if (out_len) {
ret = regmap_raw_read(tps->regmap, TPS_REG_DATA1,
out_data, out_len);
if (ret)
return ret;
val = out_data[0];
} else {
ret = regmap_read(tps->regmap, TPS_REG_DATA1, &val);
if (ret)
return ret;
}
switch (val) {
case TPS_TASK_TIMEOUT:
return -ETIMEDOUT;
case TPS_TASK_REJECTED:
return -EPERM;
default:
break;
}
return 0;
}
static int
tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
{
struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
u32 status;
int ret;
mutex_lock(&tps->lock);
ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
if (ret)
goto out_unlock
|