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