GNU Linux-libre 5.10.153-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 #include <linux/usb/role.h>
16
17 /* Register offsets */
18 #define TPS_REG_VID                     0x00
19 #define TPS_REG_MODE                    0x03
20 #define TPS_REG_CMD1                    0x08
21 #define TPS_REG_DATA1                   0x09
22 #define TPS_REG_INT_EVENT1              0x14
23 #define TPS_REG_INT_EVENT2              0x15
24 #define TPS_REG_INT_MASK1               0x16
25 #define TPS_REG_INT_MASK2               0x17
26 #define TPS_REG_INT_CLEAR1              0x18
27 #define TPS_REG_INT_CLEAR2              0x19
28 #define TPS_REG_STATUS                  0x1a
29 #define TPS_REG_SYSTEM_CONF             0x28
30 #define TPS_REG_CTRL_CONF               0x29
31 #define TPS_REG_POWER_STATUS            0x3f
32 #define TPS_REG_RX_IDENTITY_SOP         0x48
33
34 /* TPS_REG_INT_* bits */
35 #define TPS_REG_INT_PLUG_EVENT          BIT(3)
36
37 /* TPS_REG_STATUS bits */
38 #define TPS_STATUS_PLUG_PRESENT         BIT(0)
39 #define TPS_STATUS_ORIENTATION          BIT(4)
40 #define TPS_STATUS_PORTROLE(s)          (!!((s) & BIT(5)))
41 #define TPS_STATUS_DATAROLE(s)          (!!((s) & BIT(6)))
42 #define TPS_STATUS_VCONN(s)             (!!((s) & BIT(7)))
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_POWER_STATUS bits */
58 #define TPS_POWER_STATUS_SOURCESINK     BIT(1)
59 #define TPS_POWER_STATUS_PWROPMODE(p)   (((p) & GENMASK(3, 2)) >> 2)
60
61 /* TPS_REG_RX_IDENTITY_SOP */
62 struct tps6598x_rx_identity_reg {
63         u8 status;
64         struct usb_pd_identity identity;
65 } __packed;
66
67 /* Standard Task return codes */
68 #define TPS_TASK_TIMEOUT                1
69 #define TPS_TASK_REJECTED               3
70
71 enum {
72         TPS_MODE_APP,
73         TPS_MODE_BOOT,
74         TPS_MODE_BIST,
75         TPS_MODE_DISC,
76 };
77
78 static const char *const modes[] = {
79         [TPS_MODE_APP]  = "APP ",
80         [TPS_MODE_BOOT] = "BOOT",
81         [TPS_MODE_BIST] = "BIST",
82         [TPS_MODE_DISC] = "DISC",
83 };
84
85 /* Unrecognized commands will be replaced with "!CMD" */
86 #define INVALID_CMD(_cmd_)              (_cmd_ == 0x444d4321)
87
88 struct tps6598x {
89         struct device *dev;
90         struct regmap *regmap;
91         struct mutex lock; /* device lock */
92         u8 i2c_protocol:1;
93
94         struct typec_port *port;
95         struct typec_partner *partner;
96         struct usb_pd_identity partner_identity;
97         struct usb_role_switch *role_sw;
98 };
99
100 /*
101  * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
102  * https://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
103  */
104 #define TPS_MAX_LEN     64
105
106 static int
107 tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
108 {
109         u8 data[TPS_MAX_LEN + 1];
110         int ret;
111
112         if (len + 1 > sizeof(data))
113                 return -EINVAL;
114
115         if (!tps->i2c_protocol)
116                 return regmap_raw_read(tps->regmap, reg, val, len);
117
118         ret = regmap_raw_read(tps->regmap, reg, data, sizeof(data));
119         if (ret)
120                 return ret;
121
122         if (data[0] < len)
123                 return -EIO;
124
125         memcpy(val, &data[1], len);
126         return 0;
127 }
128
129 static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
130                                 const void *val, size_t len)
131 {
132         u8 data[TPS_MAX_LEN + 1];
133
134         if (!tps->i2c_protocol)
135                 return regmap_raw_write(tps->regmap, reg, val, len);
136
137         data[0] = len;
138         memcpy(&data[1], val, len);
139
140         return regmap_raw_write(tps->regmap, reg, data, sizeof(data));
141 }
142
143 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
144 {
145         return tps6598x_block_read(tps, reg, val, sizeof(u16));
146 }
147
148 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
149 {
150         return tps6598x_block_read(tps, reg, val, sizeof(u32));
151 }
152
153 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
154 {
155         return tps6598x_block_read(tps, reg, val, sizeof(u64));
156 }
157
158 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
159 {
160         return tps6598x_block_write(tps, reg, &val, sizeof(u16));
161 }
162
163 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
164 {
165         return tps6598x_block_write(tps, reg, &val, sizeof(u32));
166 }
167
168 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
169 {
170         return tps6598x_block_write(tps, reg, &val, sizeof(u64));
171 }
172
173 static inline int
174 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
175 {
176         return tps6598x_block_write(tps, reg, val, 4);
177 }
178
179 static int tps6598x_read_partner_identity(struct tps6598x *tps)
180 {
181         struct tps6598x_rx_identity_reg id;
182         int ret;
183
184         ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
185                                   &id, sizeof(id));
186         if (ret)
187                 return ret;
188
189         tps->partner_identity = id.identity;
190
191         return 0;
192 }
193
194 static void tps6598x_set_data_role(struct tps6598x *tps,
195                                    enum typec_data_role role, bool connected)
196 {
197         enum usb_role role_val;
198
199         if (role == TYPEC_HOST)
200                 role_val = USB_ROLE_HOST;
201         else
202                 role_val = USB_ROLE_DEVICE;
203
204         if (!connected)
205                 role_val = USB_ROLE_NONE;
206
207         usb_role_switch_set_role(tps->role_sw, role_val);
208         typec_set_data_role(tps->port, role);
209 }
210
211 static int tps6598x_connect(struct tps6598x *tps, u32 status)
212 {
213         struct typec_partner_desc desc;
214         enum typec_pwr_opmode mode;
215         u16 pwr_status;
216         int ret;
217
218         if (tps->partner)
219                 return 0;
220
221         ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
222         if (ret < 0)
223                 return ret;
224
225         mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
226
227         desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
228         desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
229         desc.identity = NULL;
230
231         if (desc.usb_pd) {
232                 ret = tps6598x_read_partner_identity(tps);
233                 if (ret)
234                         return ret;
235                 desc.identity = &tps->partner_identity;
236         }
237
238         typec_set_pwr_opmode(tps->port, mode);
239         typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
240         typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
241         tps6598x_set_data_role(tps, TPS_STATUS_DATAROLE(status), true);
242
243         tps->partner = typec_register_partner(tps->port, &desc);
244         if (IS_ERR(tps->partner))
245                 return PTR_ERR(tps->partner);
246
247         if (desc.identity)
248                 typec_partner_set_identity(tps->partner);
249
250         return 0;
251 }
252
253 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
254 {
255         if (!IS_ERR(tps->partner))
256                 typec_unregister_partner(tps->partner);
257         tps->partner = NULL;
258         typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
259         typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
260         typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
261         tps6598x_set_data_role(tps, TPS_STATUS_DATAROLE(status), false);
262 }
263
264 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
265                              size_t in_len, u8 *in_data,
266                              size_t out_len, u8 *out_data)
267 {
268         unsigned long timeout;
269         u32 val;
270         int ret;
271
272         ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
273         if (ret)
274                 return ret;
275         if (val && !INVALID_CMD(val))
276                 return -EBUSY;
277
278         if (in_len) {
279                 ret = tps6598x_block_write(tps, TPS_REG_DATA1,
280                                            in_data, in_len);
281                 if (ret)
282                         return ret;
283         }
284
285         ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
286         if (ret < 0)
287                 return ret;
288
289         /* XXX: Using 1s for now, but it may not be enough for every command. */
290         timeout = jiffies + msecs_to_jiffies(1000);
291
292         do {
293                 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
294                 if (ret)
295                         return ret;
296                 if (INVALID_CMD(val))
297                         return -EINVAL;
298
299                 if (time_is_before_jiffies(timeout))
300                         return -ETIMEDOUT;
301         } while (val);
302
303         if (out_len) {
304                 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
305                                           out_data, out_len);
306                 if (ret)
307                         return ret;
308                 val = out_data[0];
309         } else {
310                 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
311                 if (ret)
312                         return ret;
313         }
314
315         switch (val) {
316         case TPS_TASK_TIMEOUT:
317                 return -ETIMEDOUT;
318         case TPS_TASK_REJECTED:
319                 return -EPERM;
320         default:
321                 break;
322         }
323
324         return 0;
325 }
326
327 static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
328 {
329         const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
330         struct tps6598x *tps = typec_get_drvdata(port);
331         u32 status;
332         int ret;
333
334         mutex_lock(&tps->lock);
335
336         ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
337         if (ret)
338                 goto out_unlock;
339
340         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
341         if (ret)
342                 goto out_unlock;
343
344         if (role != TPS_STATUS_DATAROLE(status)) {
345                 ret = -EPROTO;
346                 goto out_unlock;
347         }
348
349         tps6598x_set_data_role(tps, role, true);
350
351 out_unlock:
352         mutex_unlock(&tps->lock);
353
354         return ret;
355 }
356
357 static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
358 {
359         const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
360         struct tps6598x *tps = typec_get_drvdata(port);
361         u32 status;
362         int ret;
363
364         mutex_lock(&tps->lock);
365
366         ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
367         if (ret)
368                 goto out_unlock;
369
370         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
371         if (ret)
372                 goto out_unlock;
373
374         if (role != TPS_STATUS_PORTROLE(status)) {
375                 ret = -EPROTO;
376                 goto out_unlock;
377         }
378
379         typec_set_pwr_role(tps->port, role);
380
381 out_unlock:
382         mutex_unlock(&tps->lock);
383
384         return ret;
385 }
386
387 static const struct typec_operations tps6598x_ops = {
388         .dr_set = tps6598x_dr_set,
389         .pr_set = tps6598x_pr_set,
390 };
391
392 static irqreturn_t tps6598x_interrupt(int irq, void *data)
393 {
394         struct tps6598x *tps = data;
395         u64 event1;
396         u64 event2;
397         u32 status;
398         int ret;
399
400         mutex_lock(&tps->lock);
401
402         ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
403         ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
404         if (ret) {
405                 dev_err(tps->dev, "%s: failed to read events\n", __func__);
406                 goto err_unlock;
407         }
408
409         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
410         if (ret) {
411                 dev_err(tps->dev, "%s: failed to read status\n", __func__);
412                 goto err_clear_ints;
413         }
414
415         /* Handle plug insert or removal */
416         if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) {
417                 if (status & TPS_STATUS_PLUG_PRESENT) {
418                         ret = tps6598x_connect(tps, status);
419                         if (ret)
420                                 dev_err(tps->dev,
421                                         "failed to register partner\n");
422                 } else {
423                         tps6598x_disconnect(tps, status);
424                 }
425         }
426
427 err_clear_ints:
428         tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
429         tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
430
431 err_unlock:
432         mutex_unlock(&tps->lock);
433
434         return IRQ_HANDLED;
435 }
436
437 static int tps6598x_check_mode(struct tps6598x *tps)
438 {
439         char mode[5] = { };
440         int ret;
441
442         ret = tps6598x_read32(tps, TPS_REG_MODE, (void *)mode);
443         if (ret)
444                 return ret;
445
446         switch (match_string(modes, ARRAY_SIZE(modes), mode)) {
447         case TPS_MODE_APP:
448                 return 0;
449         case TPS_MODE_BOOT:
450                 dev_warn(tps->dev, "dead-battery condition\n");
451                 return 0;
452         case TPS_MODE_BIST:
453         case TPS_MODE_DISC:
454         default:
455                 dev_err(tps->dev, "controller in unsupported mode \"%s\"\n",
456                         mode);
457                 break;
458         }
459
460         return -ENODEV;
461 }
462
463 static const struct regmap_config tps6598x_regmap_config = {
464         .reg_bits = 8,
465         .val_bits = 8,
466         .max_register = 0x7F,
467 };
468
469 static int tps6598x_probe(struct i2c_client *client)
470 {
471         struct typec_capability typec_cap = { };
472         struct tps6598x *tps;
473         struct fwnode_handle *fwnode;
474         u32 status;
475         u32 conf;
476         u32 vid;
477         int ret;
478
479         tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
480         if (!tps)
481                 return -ENOMEM;
482
483         mutex_init(&tps->lock);
484         tps->dev = &client->dev;
485
486         tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
487         if (IS_ERR(tps->regmap))
488                 return PTR_ERR(tps->regmap);
489
490         ret = tps6598x_read32(tps, TPS_REG_VID, &vid);
491         if (ret < 0 || !vid)
492                 return -ENODEV;
493
494         /*
495          * Checking can the adapter handle SMBus protocol. If it can not, the
496          * driver needs to take care of block reads separately.
497          *
498          * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
499          * unconditionally if the adapter has I2C_FUNC_I2C set.
500          */
501         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
502                 tps->i2c_protocol = true;
503
504         /* Make sure the controller has application firmware running */
505         ret = tps6598x_check_mode(tps);
506         if (ret)
507                 return ret;
508
509         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
510         if (ret < 0)
511                 return ret;
512
513         ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
514         if (ret < 0)
515                 return ret;
516
517         fwnode = device_get_named_child_node(&client->dev, "connector");
518         if (!fwnode)
519                 return -ENODEV;
520
521         tps->role_sw = fwnode_usb_role_switch_get(fwnode);
522         if (IS_ERR(tps->role_sw)) {
523                 ret = PTR_ERR(tps->role_sw);
524                 goto err_fwnode_put;
525         }
526
527         typec_cap.revision = USB_TYPEC_REV_1_2;
528         typec_cap.pd_revision = 0x200;
529         typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
530         typec_cap.driver_data = tps;
531         typec_cap.ops = &tps6598x_ops;
532         typec_cap.fwnode = fwnode;
533
534         switch (TPS_SYSCONF_PORTINFO(conf)) {
535         case TPS_PORTINFO_SINK_ACCESSORY:
536         case TPS_PORTINFO_SINK:
537                 typec_cap.type = TYPEC_PORT_SNK;
538                 typec_cap.data = TYPEC_PORT_UFP;
539                 break;
540         case TPS_PORTINFO_DRP_UFP_DRD:
541         case TPS_PORTINFO_DRP_DFP_DRD:
542                 typec_cap.type = TYPEC_PORT_DRP;
543                 typec_cap.data = TYPEC_PORT_DRD;
544                 break;
545         case TPS_PORTINFO_DRP_UFP:
546                 typec_cap.type = TYPEC_PORT_DRP;
547                 typec_cap.data = TYPEC_PORT_UFP;
548                 break;
549         case TPS_PORTINFO_DRP_DFP:
550                 typec_cap.type = TYPEC_PORT_DRP;
551                 typec_cap.data = TYPEC_PORT_DFP;
552                 break;
553         case TPS_PORTINFO_SOURCE:
554                 typec_cap.type = TYPEC_PORT_SRC;
555                 typec_cap.data = TYPEC_PORT_DFP;
556                 break;
557         default:
558                 ret = -ENODEV;
559                 goto err_role_put;
560         }
561
562         tps->port = typec_register_port(&client->dev, &typec_cap);
563         if (IS_ERR(tps->port)) {
564                 ret = PTR_ERR(tps->port);
565                 goto err_role_put;
566         }
567         fwnode_handle_put(fwnode);
568
569         if (status & TPS_STATUS_PLUG_PRESENT) {
570                 ret = tps6598x_connect(tps, status);
571                 if (ret)
572                         dev_err(&client->dev, "failed to register partner\n");
573         }
574
575         ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
576                                         tps6598x_interrupt,
577                                         IRQF_SHARED | IRQF_ONESHOT,
578                                         dev_name(&client->dev), tps);
579         if (ret) {
580                 tps6598x_disconnect(tps, 0);
581                 typec_unregister_port(tps->port);
582                 goto err_role_put;
583         }
584
585         i2c_set_clientdata(client, tps);
586
587         return 0;
588
589 err_role_put:
590         usb_role_switch_put(tps->role_sw);
591 err_fwnode_put:
592         fwnode_handle_put(fwnode);
593
594         return ret;
595 }
596
597 static int tps6598x_remove(struct i2c_client *client)
598 {
599         struct tps6598x *tps = i2c_get_clientdata(client);
600
601         tps6598x_disconnect(tps, 0);
602         typec_unregister_port(tps->port);
603         usb_role_switch_put(tps->role_sw);
604
605         return 0;
606 }
607
608 static const struct of_device_id tps6598x_of_match[] = {
609         { .compatible = "ti,tps6598x", },
610         {}
611 };
612 MODULE_DEVICE_TABLE(of, tps6598x_of_match);
613
614 static const struct i2c_device_id tps6598x_id[] = {
615         { "tps6598x" },
616         { }
617 };
618 MODULE_DEVICE_TABLE(i2c, tps6598x_id);
619
620 static struct i2c_driver tps6598x_i2c_driver = {
621         .driver = {
622                 .name = "tps6598x",
623                 .of_match_table = tps6598x_of_match,
624         },
625         .probe_new = tps6598x_probe,
626         .remove = tps6598x_remove,
627         .id_table = tps6598x_id,
628 };
629 module_i2c_driver(tps6598x_i2c_driver);
630
631 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
632 MODULE_LICENSE("GPL v2");
633 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");