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