GNU Linux-libre 4.19.286-gnu1
[releases.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 <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/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) & 7)
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         u8 i2c_protocol:1;
77
78         struct typec_port *port;
79         struct typec_partner *partner;
80         struct usb_pd_identity partner_identity;
81         struct typec_capability typec_cap;
82 };
83
84 /*
85  * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
86  * http://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
87  */
88 #define TPS_MAX_LEN     64
89
90 static int
91 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
92 {
93         u8 data[TPS_MAX_LEN + 1];
94         int ret;
95
96         if (len + 1 > sizeof(data))
97                 return -EINVAL;
98
99         if (!tps->i2c_protocol)
100                 return regmap_raw_read(tps->regmap, reg, val, len);
101
102         ret = regmap_raw_read(tps->regmap, reg, data, sizeof(data));
103         if (ret)
104                 return ret;
105
106         if (data[0] < len)
107                 return -EIO;
108
109         memcpy(val, &data[1], len);
110         return 0;
111 }
112
113 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
114                                 const void *val, size_t len)
115 {
116         u8 data[TPS_MAX_LEN + 1];
117
118         if (!tps->i2c_protocol)
119                 return regmap_raw_write(tps->regmap, reg, val, len);
120
121         data[0] = len;
122         memcpy(&data[1], val, len);
123
124         return regmap_raw_write(tps->regmap, reg, data, sizeof(data));
125 }
126
127 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
128 {
129         return tps6598x_block_read(tps, reg, val, sizeof(u16));
130 }
131
132 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
133 {
134         return tps6598x_block_read(tps, reg, val, sizeof(u32));
135 }
136
137 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
138 {
139         return tps6598x_block_read(tps, reg, val, sizeof(u64));
140 }
141
142 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
143 {
144         return tps6598x_block_write(tps, reg, &val, sizeof(u16));
145 }
146
147 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
148 {
149         return tps6598x_block_write(tps, reg, &val, sizeof(u32));
150 }
151
152 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
153 {
154         return tps6598x_block_write(tps, reg, &val, sizeof(u64));
155 }
156
157 static inline int
158 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
159 {
160         return tps6598x_block_write(tps, reg, val, 4);
161 }
162
163 static int tps6598x_read_partner_identity(struct tps6598x *tps)
164 {
165         struct tps6598x_rx_identity_reg id;
166         int ret;
167
168         ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
169                                   &id, sizeof(id));
170         if (ret)
171                 return ret;
172
173         tps->partner_identity = id.identity;
174
175         return 0;
176 }
177
178 static int tps6598x_connect(struct tps6598x *tps, u32 status)
179 {
180         struct typec_partner_desc desc;
181         enum typec_pwr_opmode mode;
182         u16 pwr_status;
183         int ret;
184
185         if (tps->partner)
186                 return 0;
187
188         ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
189         if (ret < 0)
190                 return ret;
191
192         mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
193
194         desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
195         desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
196         desc.identity = NULL;
197
198         if (desc.usb_pd) {
199                 ret = tps6598x_read_partner_identity(tps);
200                 if (ret)
201                         return ret;
202                 desc.identity = &tps->partner_identity;
203         }
204
205         typec_set_pwr_opmode(tps->port, mode);
206         typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
207         typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
208         typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
209
210         tps->partner = typec_register_partner(tps->port, &desc);
211         if (IS_ERR(tps->partner))
212                 return PTR_ERR(tps->partner);
213
214         if (desc.identity)
215                 typec_partner_set_identity(tps->partner);
216
217         return 0;
218 }
219
220 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
221 {
222         if (!IS_ERR(tps->partner))
223                 typec_unregister_partner(tps->partner);
224         tps->partner = NULL;
225         typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
226         typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
227         typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
228         typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
229 }
230
231 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
232                              size_t in_len, u8 *in_data,
233                              size_t out_len, u8 *out_data)
234 {
235         unsigned long timeout;
236         u32 val;
237         int ret;
238
239         ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
240         if (ret)
241                 return ret;
242         if (val && !INVALID_CMD(val))
243                 return -EBUSY;
244
245         if (in_len) {
246                 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
247                                            in_data, in_len);
248                 if (ret)
249                         return ret;
250         }
251
252         ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
253         if (ret < 0)
254                 return ret;
255
256         /* XXX: Using 1s for now, but it may not be enough for every command. */
257         timeout = jiffies + msecs_to_jiffies(1000);
258
259         do {
260                 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
261                 if (ret)
262                         return ret;
263                 if (INVALID_CMD(val))
264                         return -EINVAL;
265
266                 if (time_is_before_jiffies(timeout))
267                         return -ETIMEDOUT;
268         } while (val);
269
270         if (out_len) {
271                 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
272                                           out_data, out_len);
273                 if (ret)
274                         return ret;
275                 val = out_data[0];
276         } else {
277                 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
278                 if (ret)
279                         return ret;
280         }
281
282         switch (val) {
283         case TPS_TASK_TIMEOUT:
284                 return -ETIMEDOUT;
285         case TPS_TASK_REJECTED:
286                 return -EPERM;
287         default:
288                 break;
289         }
290
291         return 0;
292 }
293
294 static int
295 tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
296 {
297         struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
298         const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
299         u32 status;
300         int ret;
301
302         mutex_lock(&tps->lock);
303
304         ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
305         if (ret)
306                 goto out_unlock;
307
308         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
309         if (ret)
310                 goto out_unlock;
311
312         if (role != TPS_STATUS_DATAROLE(status)) {
313                 ret = -EPROTO;
314                 goto out_unlock;
315         }
316
317         typec_set_data_role(tps->port, role);
318
319 out_unlock:
320         mutex_unlock(&tps->lock);
321
322         return ret;
323 }
324
325 static int
326 tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
327 {
328         struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
329         const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
330         u32 status;
331         int ret;
332
333         mutex_lock(&tps->lock);
334
335         ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
336         if (ret)
337                 goto out_unlock;
338
339         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
340         if (ret)
341                 goto out_unlock;
342
343         if (role != TPS_STATUS_PORTROLE(status)) {
344                 ret = -EPROTO;
345                 goto out_unlock;
346         }
347
348         typec_set_pwr_role(tps->port, role);
349
350 out_unlock:
351         mutex_unlock(&tps->lock);
352
353         return ret;
354 }
355
356 static irqreturn_t tps6598x_interrupt(int irq, void *data)
357 {
358         struct tps6598x *tps = data;
359         u64 event1;
360         u64 event2;
361         u32 status;
362         int ret;
363
364         mutex_lock(&tps->lock);
365
366         ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
367         ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
368         if (ret) {
369                 dev_err(tps->dev, "%s: failed to read events\n", __func__);
370                 goto err_unlock;
371         }
372
373         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
374         if (ret) {
375                 dev_err(tps->dev, "%s: failed to read status\n", __func__);
376                 goto err_clear_ints;
377         }
378
379         /* Handle plug insert or removal */
380         if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) {
381                 if (status & TPS_STATUS_PLUG_PRESENT) {
382                         ret = tps6598x_connect(tps, status);
383                         if (ret)
384                                 dev_err(tps->dev,
385                                         "failed to register partner\n");
386                 } else {
387                         tps6598x_disconnect(tps, status);
388                 }
389         }
390
391 err_clear_ints:
392         tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
393         tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
394
395 err_unlock:
396         mutex_unlock(&tps->lock);
397
398         return IRQ_HANDLED;
399 }
400
401 static const struct regmap_config tps6598x_regmap_config = {
402         .reg_bits = 8,
403         .val_bits = 8,
404         .max_register = 0x7F,
405 };
406
407 static int tps6598x_probe(struct i2c_client *client)
408 {
409         struct tps6598x *tps;
410         u32 status;
411         u32 conf;
412         u32 vid;
413         int ret;
414
415         tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
416         if (!tps)
417                 return -ENOMEM;
418
419         mutex_init(&tps->lock);
420         tps->dev = &client->dev;
421
422         tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
423         if (IS_ERR(tps->regmap))
424                 return PTR_ERR(tps->regmap);
425
426         ret = tps6598x_read32(tps, 0, &vid);
427         if (ret < 0)
428                 return ret;
429         if (!vid)
430                 return -ENODEV;
431
432         /*
433          * Checking can the adapter handle SMBus protocol. If it can not, the
434          * driver needs to take care of block reads separately.
435          *
436          * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
437          * unconditionally if the adapter has I2C_FUNC_I2C set.
438          */
439         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
440                 tps->i2c_protocol = true;
441
442         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
443         if (ret < 0)
444                 return ret;
445
446         ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
447         if (ret < 0)
448                 return ret;
449
450         tps->typec_cap.revision = USB_TYPEC_REV_1_2;
451         tps->typec_cap.pd_revision = 0x200;
452         tps->typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
453         tps->typec_cap.pr_set = tps6598x_pr_set;
454         tps->typec_cap.dr_set = tps6598x_dr_set;
455
456         switch (TPS_SYSCONF_PORTINFO(conf)) {
457         case TPS_PORTINFO_SINK_ACCESSORY:
458         case TPS_PORTINFO_SINK:
459                 tps->typec_cap.type = TYPEC_PORT_SNK;
460                 tps->typec_cap.data = TYPEC_PORT_UFP;
461                 break;
462         case TPS_PORTINFO_DRP_UFP_DRD:
463         case TPS_PORTINFO_DRP_DFP_DRD:
464                 tps->typec_cap.type = TYPEC_PORT_DRP;
465                 tps->typec_cap.data = TYPEC_PORT_DRD;
466                 break;
467         case TPS_PORTINFO_DRP_UFP:
468                 tps->typec_cap.type = TYPEC_PORT_DRP;
469                 tps->typec_cap.data = TYPEC_PORT_UFP;
470                 break;
471         case TPS_PORTINFO_DRP_DFP:
472                 tps->typec_cap.type = TYPEC_PORT_DRP;
473                 tps->typec_cap.data = TYPEC_PORT_DFP;
474                 break;
475         case TPS_PORTINFO_SOURCE:
476                 tps->typec_cap.type = TYPEC_PORT_SRC;
477                 tps->typec_cap.data = TYPEC_PORT_DFP;
478                 break;
479         default:
480                 return -ENODEV;
481         }
482
483         tps->port = typec_register_port(&client->dev, &tps->typec_cap);
484         if (IS_ERR(tps->port))
485                 return PTR_ERR(tps->port);
486
487         if (status & TPS_STATUS_PLUG_PRESENT) {
488                 ret = tps6598x_connect(tps, status);
489                 if (ret)
490                         dev_err(&client->dev, "failed to register partner\n");
491         }
492
493         ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
494                                         tps6598x_interrupt,
495                                         IRQF_SHARED | IRQF_ONESHOT,
496                                         dev_name(&client->dev), tps);
497         if (ret) {
498                 tps6598x_disconnect(tps, 0);
499                 typec_unregister_port(tps->port);
500                 return ret;
501         }
502
503         i2c_set_clientdata(client, tps);
504
505         return 0;
506 }
507
508 static int tps6598x_remove(struct i2c_client *client)
509 {
510         struct tps6598x *tps = i2c_get_clientdata(client);
511
512         tps6598x_disconnect(tps, 0);
513         typec_unregister_port(tps->port);
514
515         return 0;
516 }
517
518 static const struct acpi_device_id tps6598x_acpi_match[] = {
519         { "INT3515", 0 },
520         { }
521 };
522 MODULE_DEVICE_TABLE(acpi, tps6598x_acpi_match);
523
524 static struct i2c_driver tps6598x_i2c_driver = {
525         .driver = {
526                 .name = "tps6598x",
527                 .acpi_match_table = tps6598x_acpi_match,
528         },
529         .probe_new = tps6598x_probe,
530         .remove = tps6598x_remove,
531 };
532 module_i2c_driver(tps6598x_i2c_driver);
533
534 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
535 MODULE_LICENSE("GPL v2");
536 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");