Linux Audio

Check our new training course

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