Linux Audio

Check our new training course

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