USB: typec: Remove remaining redundant license text
[linux/fpc-iii.git] / drivers / usb / typec / tps6598x.c
blob2719f5d382f76505d8c0eaf56a0e047f7e8e128b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Driver for TI TPS6598x USB Power Delivery controller family
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 */
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>
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
31 /* TPS_REG_INT_* bits */
32 #define TPS_REG_INT_PLUG_EVENT BIT(3)
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)))
41 /* TPS_REG_SYSTEM_CONF bits */
42 #define TPS_SYSCONF_PORTINFO(c) ((c) & 3)
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,
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)
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;
65 /* Standard Task return codes */
66 #define TPS_TASK_TIMEOUT 1
67 #define TPS_TASK_REJECTED 3
69 /* Unrecognized commands will be replaced with "!CMD" */
70 #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
72 struct tps6598x {
73 struct device *dev;
74 struct regmap *regmap;
75 struct mutex lock; /* device lock */
77 struct typec_port *port;
78 struct typec_partner *partner;
79 struct usb_pd_identity partner_identity;
80 struct typec_capability typec_cap;
83 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
85 return regmap_raw_read(tps->regmap, reg, val, sizeof(u16));
88 static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
90 return regmap_raw_read(tps->regmap, reg, val, sizeof(u32));
93 static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
95 return regmap_raw_read(tps->regmap, reg, val, sizeof(u64));
98 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
100 return regmap_raw_write(tps->regmap, reg, &val, sizeof(u16));
103 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
105 return regmap_raw_write(tps->regmap, reg, &val, sizeof(u32));
108 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
110 return regmap_raw_write(tps->regmap, reg, &val, sizeof(u64));
113 static inline int
114 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
116 return regmap_raw_write(tps->regmap, reg, &val, sizeof(u32));
119 static int tps6598x_read_partner_identity(struct tps6598x *tps)
121 struct tps6598x_rx_identity_reg id;
122 int ret;
124 ret = regmap_raw_read(tps->regmap, TPS_REG_RX_IDENTITY_SOP,
125 &id, sizeof(id));
126 if (ret)
127 return ret;
129 tps->partner_identity = id.identity;
131 return 0;
134 static int tps6598x_connect(struct tps6598x *tps, u32 status)
136 struct typec_partner_desc desc;
137 enum typec_pwr_opmode mode;
138 u16 pwr_status;
139 int ret;
141 if (tps->partner)
142 return 0;
144 ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
145 if (ret < 0)
146 return ret;
148 mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
150 desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
151 desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
152 desc.identity = NULL;
154 if (desc.usb_pd) {
155 ret = tps6598x_read_partner_identity(tps);
156 if (ret)
157 return ret;
158 desc.identity = &tps->partner_identity;
161 tps->partner = typec_register_partner(tps->port, &desc);
162 if (!tps->partner)
163 return -ENODEV;
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));
170 if (desc.identity)
171 typec_partner_set_identity(tps->partner);
173 return 0;
176 static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
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));
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)
190 unsigned long timeout;
191 u32 val;
192 int ret;
194 ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
195 if (ret)
196 return ret;
197 if (val && !INVALID_CMD(val))
198 return -EBUSY;
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;
207 ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
208 if (ret < 0)
209 return ret;
211 /* XXX: Using 1s for now, but it may not be enough for every command. */
212 timeout = jiffies + msecs_to_jiffies(1000);
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;
221 if (time_is_before_jiffies(timeout))
222 return -ETIMEDOUT;
223 } while (val);
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;
237 switch (val) {
238 case TPS_TASK_TIMEOUT:
239 return -ETIMEDOUT;
240 case TPS_TASK_REJECTED:
241 return -EPERM;
242 default:
243 break;
246 return 0;
249 static int
250 tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
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;
257 mutex_lock(&tps->lock);
259 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
260 if (ret)
261 goto out_unlock;
263 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
264 if (ret)
265 goto out_unlock;
267 if (role != TPS_STATUS_DATAROLE(status)) {
268 ret = -EPROTO;
269 goto out_unlock;
272 typec_set_data_role(tps->port, role);
274 out_unlock:
275 mutex_unlock(&tps->lock);
277 return ret;
280 static int
281 tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
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;
288 mutex_lock(&tps->lock);
290 ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
291 if (ret)
292 goto out_unlock;
294 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
295 if (ret)
296 goto out_unlock;
298 if (role != TPS_STATUS_PORTROLE(status)) {
299 ret = -EPROTO;
300 goto out_unlock;
303 typec_set_pwr_role(tps->port, role);
305 out_unlock:
306 mutex_unlock(&tps->lock);
308 return ret;
311 static irqreturn_t tps6598x_interrupt(int irq, void *data)
313 struct tps6598x *tps = data;
314 u64 event1;
315 u64 event2;
316 u32 status;
317 int ret;
319 mutex_lock(&tps->lock);
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;
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;
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);
346 err_clear_ints:
347 tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
348 tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
350 err_unlock:
351 mutex_unlock(&tps->lock);
353 return IRQ_HANDLED;
356 static const struct regmap_config tps6598x_regmap_config = {
357 .reg_bits = 8,
358 .val_bits = 8,
359 .max_register = 0x7F,
362 static int tps6598x_probe(struct i2c_client *client)
364 struct tps6598x *tps;
365 u32 status;
366 u32 conf;
367 u32 vid;
368 int ret;
370 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
371 if (!tps)
372 return -ENOMEM;
374 mutex_init(&tps->lock);
375 tps->dev = &client->dev;
377 tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
378 if (IS_ERR(tps->regmap))
379 return PTR_ERR(tps->regmap);
381 ret = tps6598x_read32(tps, 0, &vid);
382 if (ret < 0)
383 return ret;
384 if (!vid)
385 return -ENODEV;
387 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
388 if (ret < 0)
389 return ret;
391 ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
392 if (ret < 0)
393 return ret;
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;
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;
420 tps->port = typec_register_port(&client->dev, &tps->typec_cap);
421 if (!tps->port)
422 return -ENODEV;
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");
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;
440 i2c_set_clientdata(client, tps);
442 return 0;
445 static int tps6598x_remove(struct i2c_client *client)
447 struct tps6598x *tps = i2c_get_clientdata(client);
449 tps6598x_disconnect(tps, 0);
450 typec_unregister_port(tps->port);
452 return 0;
455 static const struct acpi_device_id tps6598x_acpi_match[] = {
456 { "INT3515", 0 },
459 MODULE_DEVICE_TABLE(acpi, tps6598x_acpi_match);
461 static struct i2c_driver tps6598x_i2c_driver = {
462 .driver = {
463 .name = "tps6598x",
464 .acpi_match_table = tps6598x_acpi_match,
466 .probe_new = tps6598x_probe,
467 .remove = tps6598x_remove,
469 module_i2c_driver(tps6598x_i2c_driver);
471 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
472 MODULE_LICENSE("GPL v2");
473 MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");