]> Git Repo - linux.git/blob - drivers/usb/typec/tps6598x.c
Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target...
[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         tps->partner = typec_register_partner(tps->port, &desc);
162         if (!tps->partner)
163                 return -ENODEV;
164
165         typec_set_pwr_opmode(tps->port, mode);
166         typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
167         typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
168         typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
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         typec_unregister_partner(tps->partner);
179         tps->partner = NULL;
180         typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
181         typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
182         typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
183         typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
184 }
185
186 static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
187                              size_t in_len, u8 *in_data,
188                              size_t out_len, u8 *out_data)
189 {
190         unsigned long timeout;
191         u32 val;
192         int ret;
193
194         ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
195         if (ret)
196                 return ret;
197         if (val && !INVALID_CMD(val))
198                 return -EBUSY;
199
200         if (in_len) {
201                 ret = regmap_raw_write(tps->regmap, TPS_REG_DATA1,
202                                        in_data, in_len);
203                 if (ret)
204                         return ret;
205         }
206
207         ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
208         if (ret < 0)
209                 return ret;
210
211         /* XXX: Using 1s for now, but it may not be enough for every command. */
212         timeout = jiffies + msecs_to_jiffies(1000);
213
214         do {
215                 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
216                 if (ret)
217                         return ret;
218                 if (INVALID_CMD(val))
219                         return -EINVAL;
220
221                 if (time_is_before_jiffies(timeout))
222                         return -ETIMEDOUT;
223         } while (val);
224
225         if (out_len) {
226                 ret = regmap_raw_read(tps->regmap, TPS_REG_DATA1,
227                                       out_data, out_len);
228                 if (ret)
229                         return ret;
230                 val = out_data[0];
231         } else {
232                 ret = regmap_read(tps->regmap, TPS_REG_DATA1, &val);
233                 if (ret)
234                         return ret;
235         }
236
237         switch (val) {
238         case TPS_TASK_TIMEOUT:
239                 return -ETIMEDOUT;
240         case TPS_TASK_REJECTED:
241                 return -EPERM;
242         default:
243                 break;
244         }
245
246         return 0;
247 }
248
249 static int
250 tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
251 {
252         struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
253         const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
254         u32 status;
255         int ret;
256
257         mutex_lock(&tps->lock);
258
259         ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
260         if (ret)
261                 goto out_unlock;
262
263         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
264         if (ret)
265                 goto out_unlock;
266
267         if (role != TPS_STATUS_DATAROLE(status)) {
268                 ret = -EPROTO;
269                 goto out_unlock;
270         }
271
272         typec_set_data_role(tps->port, role);
273
274 out_unlock:
275         mutex_unlock(&tps->lock);
276
277         return ret;
278 }
279
280 static int
281 tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
282 {
283         struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
284         const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
285         u32 status;
286         int ret;
287
288         mutex_lock(&tps->lock);
289
290         ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
291         if (ret)
292                 goto out_unlock;
293
294         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
295         if (ret)
296                 goto out_unlock;
297
298         if (role != TPS_STATUS_PORTROLE(status)) {
299                 ret = -EPROTO;
300                 goto out_unlock;
301         }
302
303         typec_set_pwr_role(tps->port, role);
304
305 out_unlock:
306         mutex_unlock(&tps->lock);
307
308         return ret;
309 }
310
311 static irqreturn_t tps6598x_interrupt(int irq, void *data)
312 {
313         struct tps6598x *tps = data;
314         u64 event1;
315         u64 event2;
316         u32 status;
317         int ret;
318
319         mutex_lock(&tps->lock);
320
321         ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
322         ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
323         if (ret) {
324                 dev_err(tps->dev, "%s: failed to read events\n", __func__);
325                 goto err_unlock;
326         }
327
328         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
329         if (ret) {
330                 dev_err(tps->dev, "%s: failed to read status\n", __func__);
331                 goto err_clear_ints;
332         }
333
334         /* Handle plug insert or removal */
335         if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) {
336                 if (status & TPS_STATUS_PLUG_PRESENT) {
337                         ret = tps6598x_connect(tps, status);
338                         if (ret)
339                                 dev_err(tps->dev,
340                                         "failed to register partner\n");
341                 } else {
342                         tps6598x_disconnect(tps, status);
343                 }
344         }
345
346 err_clear_ints:
347         tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
348         tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
349
350 err_unlock:
351         mutex_unlock(&tps->lock);
352
353         return IRQ_HANDLED;
354 }
355
356 static const struct regmap_config tps6598x_regmap_config = {
357         .reg_bits = 8,
358         .val_bits = 8,
359         .max_register = 0x7F,
360 };
361
362 static int tps6598x_probe(struct i2c_client *client)
363 {
364         struct tps6598x *tps;
365         u32 status;
366         u32 conf;
367         u32 vid;
368         int ret;
369
370         tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
371         if (!tps)
372                 return -ENOMEM;
373
374         mutex_init(&tps->lock);
375         tps->dev = &client->dev;
376
377         tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
378         if (IS_ERR(tps->regmap))
379                 return PTR_ERR(tps->regmap);
380
381         ret = tps6598x_read32(tps, 0, &vid);
382         if (ret < 0)
383                 return ret;
384         if (!vid)
385                 return -ENODEV;
386
387         ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
388         if (ret < 0)
389                 return ret;
390
391         ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
392         if (ret < 0)
393                 return ret;
394
395         switch (TPS_SYSCONF_PORTINFO(conf)) {
396         case TPS_PORTINFO_SINK_ACCESSORY:
397         case TPS_PORTINFO_SINK:
398                 tps->typec_cap.type = TYPEC_PORT_UFP;
399                 break;
400         case TPS_PORTINFO_DRP_UFP_DRD:
401         case TPS_PORTINFO_DRP_DFP_DRD:
402                 tps->typec_cap.dr_set = tps6598x_dr_set;
403                 /* fall through */
404         case TPS_PORTINFO_DRP_UFP:
405         case TPS_PORTINFO_DRP_DFP:
406                 tps->typec_cap.pr_set = tps6598x_pr_set;
407                 tps->typec_cap.type = TYPEC_PORT_DRP;
408                 break;
409         case TPS_PORTINFO_SOURCE:
410                 tps->typec_cap.type = TYPEC_PORT_DFP;
411                 break;
412         default:
413                 return -ENODEV;
414         }
415
416         tps->typec_cap.revision = USB_TYPEC_REV_1_2;
417         tps->typec_cap.pd_revision = 0x200;
418         tps->typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
419
420         tps->port = typec_register_port(&client->dev, &tps->typec_cap);
421         if (!tps->port)
422                 return -ENODEV;
423
424         if (status & TPS_STATUS_PLUG_PRESENT) {
425                 ret = tps6598x_connect(tps, status);
426                 if (ret)
427                         dev_err(&client->dev, "failed to register partner\n");
428         }
429
430         ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
431                                         tps6598x_interrupt,
432                                         IRQF_SHARED | IRQF_ONESHOT,
433                                         dev_name(&client->dev), tps);
434         if (ret) {
435                 tps6598x_disconnect(tps, 0);
436                 typec_unregister_port(tps->port);
437                 return ret;
438         }
439
440         i2c_set_clientdata(client, tps);
441
442         return 0;
443 }
444
445 static int tps6598x_remove(struct i2c_client *client)
446 {
447         struct tps6598x *tps = i2c_get_clientdata(client);
448
449         tps6598x_disconnect(tps, 0);
450         typec_unregister_port(tps->port);
451
452         return 0;
453 }
454
455 static const struct acpi_device_id tps6598x_acpi_match[] = {
456         { "INT3515", 0 },
457         { }
458 };
459 MODULE_DEVICE_TABLE(acpi, tps6598x_acpi_match);
460
461 static struct i2c_driver tps6598x_i2c_driver = {
462         .driver = {
463                 .name = "tps6598x",
464                 .acpi_match_table = tps6598x_acpi_match,
465         },
466         .probe_new = tps6598x_probe,
467         .remove = tps6598x_remove,
468 };
469 module_i2c_driver(tps6598x_i2c_driver);
470
471 MODULE_AUTHOR("Heikki Krogerus <[email protected]>");
472 MODULE_LICENSE("GPL v2");
473 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");
This page took 0.05865 seconds and 4 git commands to generate.