Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.6.
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * drivers/extcon/extcon-tusb320.c - TUSB320 extcon driver
  4 *
  5 * Copyright (C) 2020 National Instruments Corporation
  6 * Author: Michael Auchter <michael.auchter@ni.com>
  7 */
  8
  9#include <linux/bitfield.h>
 10#include <linux/extcon-provider.h>
 11#include <linux/i2c.h>
 12#include <linux/init.h>
 13#include <linux/interrupt.h>
 14#include <linux/kernel.h>
 15#include <linux/module.h>
 16#include <linux/regmap.h>
 17#include <linux/usb/typec.h>
 18
 19#define TUSB320_REG8				0x8
 20#define TUSB320_REG8_CURRENT_MODE_ADVERTISE	GENMASK(7, 6)
 21#define TUSB320_REG8_CURRENT_MODE_ADVERTISE_USB	0x0
 22#define TUSB320_REG8_CURRENT_MODE_ADVERTISE_15A	0x1
 23#define TUSB320_REG8_CURRENT_MODE_ADVERTISE_30A	0x2
 24#define TUSB320_REG8_CURRENT_MODE_DETECT	GENMASK(5, 4)
 25#define TUSB320_REG8_CURRENT_MODE_DETECT_DEF	0x0
 26#define TUSB320_REG8_CURRENT_MODE_DETECT_MED	0x1
 27#define TUSB320_REG8_CURRENT_MODE_DETECT_ACC	0x2
 28#define TUSB320_REG8_CURRENT_MODE_DETECT_HI	0x3
 29#define TUSB320_REG8_ACCESSORY_CONNECTED	GENMASK(3, 2)
 30#define TUSB320_REG8_ACCESSORY_CONNECTED_NONE	0x0
 31#define TUSB320_REG8_ACCESSORY_CONNECTED_AUDIO	0x4
 32#define TUSB320_REG8_ACCESSORY_CONNECTED_ACC	0x5
 33#define TUSB320_REG8_ACCESSORY_CONNECTED_DEBUG	0x6
 34#define TUSB320_REG8_ACTIVE_CABLE_DETECTION	BIT(0)
 35
 36#define TUSB320_REG9				0x9
 37#define TUSB320_REG9_ATTACHED_STATE_SHIFT	6
 38#define TUSB320_REG9_ATTACHED_STATE_MASK	0x3
 39#define TUSB320_REG9_CABLE_DIRECTION		BIT(5)
 40#define TUSB320_REG9_INTERRUPT_STATUS		BIT(4)
 41
 42#define TUSB320_REGA				0xa
 43#define TUSB320L_REGA_DISABLE_TERM		BIT(0)
 44#define TUSB320_REGA_I2C_SOFT_RESET		BIT(3)
 45#define TUSB320_REGA_MODE_SELECT_SHIFT		4
 46#define TUSB320_REGA_MODE_SELECT_MASK		0x3
 47
 48#define TUSB320L_REGA0_REVISION			0xa0
 49
 50enum tusb320_attached_state {
 51	TUSB320_ATTACHED_STATE_NONE,
 52	TUSB320_ATTACHED_STATE_DFP,
 53	TUSB320_ATTACHED_STATE_UFP,
 54	TUSB320_ATTACHED_STATE_ACC,
 55};
 56
 57enum tusb320_mode {
 58	TUSB320_MODE_PORT,
 59	TUSB320_MODE_UFP,
 60	TUSB320_MODE_DFP,
 61	TUSB320_MODE_DRP,
 62};
 63
 64struct tusb320_priv;
 65
 66struct tusb320_ops {
 67	int (*set_mode)(struct tusb320_priv *priv, enum tusb320_mode mode);
 68	int (*get_revision)(struct tusb320_priv *priv, unsigned int *revision);
 69};
 70
 71struct tusb320_priv {
 72	struct device *dev;
 73	struct regmap *regmap;
 74	struct extcon_dev *edev;
 75	struct tusb320_ops *ops;
 76	enum tusb320_attached_state state;
 77	struct typec_port *port;
 78	struct typec_capability	cap;
 79	enum typec_port_type port_type;
 80	enum typec_pwr_opmode pwr_opmode;
 81};
 82
 83static const char * const tusb_attached_states[] = {
 84	[TUSB320_ATTACHED_STATE_NONE] = "not attached",
 85	[TUSB320_ATTACHED_STATE_DFP]  = "downstream facing port",
 86	[TUSB320_ATTACHED_STATE_UFP]  = "upstream facing port",
 87	[TUSB320_ATTACHED_STATE_ACC]  = "accessory",
 88};
 89
 90static const unsigned int tusb320_extcon_cable[] = {
 91	EXTCON_USB,
 92	EXTCON_USB_HOST,
 93	EXTCON_NONE,
 94};
 95
 96static int tusb320_check_signature(struct tusb320_priv *priv)
 97{
 98	static const char sig[] = { '\0', 'T', 'U', 'S', 'B', '3', '2', '0' };
 99	unsigned val;
100	int i, ret;
101
102	for (i = 0; i < sizeof(sig); i++) {
103		ret = regmap_read(priv->regmap, sizeof(sig) - 1 - i, &val);
104		if (ret < 0)
105			return ret;
106		if (val != sig[i]) {
107			dev_err(priv->dev, "signature mismatch!\n");
108			return -ENODEV;
109		}
110	}
111
112	return 0;
113}
114
115static int tusb320_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
116{
117	int ret;
118
119	/* Mode cannot be changed while cable is attached */
120	if (priv->state != TUSB320_ATTACHED_STATE_NONE)
121		return -EBUSY;
122
123	/* Write mode */
124	ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
125		TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT,
126		mode << TUSB320_REGA_MODE_SELECT_SHIFT);
127	if (ret) {
128		dev_err(priv->dev, "failed to write mode: %d\n", ret);
129		return ret;
130	}
131
132	return 0;
133}
134
135static int tusb320l_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
136{
137	int ret;
138
139	/* Disable CC state machine */
140	ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
141		TUSB320L_REGA_DISABLE_TERM, 1);
142	if (ret) {
143		dev_err(priv->dev,
144			"failed to disable CC state machine: %d\n", ret);
145		return ret;
146	}
147
148	/* Write mode */
149	ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
150		TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT,
151		mode << TUSB320_REGA_MODE_SELECT_SHIFT);
152	if (ret) {
153		dev_err(priv->dev, "failed to write mode: %d\n", ret);
154		goto err;
155	}
156
157	msleep(5);
158err:
159	/* Re-enable CC state machine */
160	ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
161		TUSB320L_REGA_DISABLE_TERM, 0);
162	if (ret)
163		dev_err(priv->dev,
164			"failed to re-enable CC state machine: %d\n", ret);
165
166	return ret;
167}
168
169static int tusb320_reset(struct tusb320_priv *priv)
170{
171	int ret;
172
173	/* Set mode to default (follow PORT pin) */
174	ret = priv->ops->set_mode(priv, TUSB320_MODE_PORT);
175	if (ret && ret != -EBUSY) {
176		dev_err(priv->dev,
177			"failed to set mode to PORT: %d\n", ret);
178		return ret;
179	}
180
181	/* Perform soft reset */
182	ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
183			TUSB320_REGA_I2C_SOFT_RESET, 1);
184	if (ret) {
185		dev_err(priv->dev,
186			"failed to write soft reset bit: %d\n", ret);
187		return ret;
188	}
189
190	/* Wait for chip to go through reset */
191	msleep(95);
192
193	return 0;
194}
195
196static int tusb320l_get_revision(struct tusb320_priv *priv, unsigned int *revision)
197{
198	return regmap_read(priv->regmap, TUSB320L_REGA0_REVISION, revision);
199}
200
201static struct tusb320_ops tusb320_ops = {
202	.set_mode = tusb320_set_mode,
203};
204
205static struct tusb320_ops tusb320l_ops = {
206	.set_mode = tusb320l_set_mode,
207	.get_revision = tusb320l_get_revision,
208};
209
210static int tusb320_set_adv_pwr_mode(struct tusb320_priv *priv)
211{
212	u8 mode;
213
214	if (priv->pwr_opmode == TYPEC_PWR_MODE_USB)
215		mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_USB;
216	else if (priv->pwr_opmode == TYPEC_PWR_MODE_1_5A)
217		mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_15A;
218	else if (priv->pwr_opmode == TYPEC_PWR_MODE_3_0A)
219		mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_30A;
220	else	/* No other mode is supported. */
221		return -EINVAL;
222
223	return regmap_write_bits(priv->regmap, TUSB320_REG8,
224				 TUSB320_REG8_CURRENT_MODE_ADVERTISE,
225				 FIELD_PREP(TUSB320_REG8_CURRENT_MODE_ADVERTISE,
226					    mode));
227}
228
229static int tusb320_port_type_set(struct typec_port *port,
230				 enum typec_port_type type)
231{
232	struct tusb320_priv *priv = typec_get_drvdata(port);
233
234	if (type == TYPEC_PORT_SRC)
235		return priv->ops->set_mode(priv, TUSB320_MODE_DFP);
236	else if (type == TYPEC_PORT_SNK)
237		return priv->ops->set_mode(priv, TUSB320_MODE_UFP);
238	else if (type == TYPEC_PORT_DRP)
239		return priv->ops->set_mode(priv, TUSB320_MODE_DRP);
240	else
241		return priv->ops->set_mode(priv, TUSB320_MODE_PORT);
242}
243
244static const struct typec_operations tusb320_typec_ops = {
245	.port_type_set	= tusb320_port_type_set,
246};
247
248static void tusb320_extcon_irq_handler(struct tusb320_priv *priv, u8 reg)
249{
250	int state, polarity;
251
252	state = (reg >> TUSB320_REG9_ATTACHED_STATE_SHIFT) &
253		TUSB320_REG9_ATTACHED_STATE_MASK;
254	polarity = !!(reg & TUSB320_REG9_CABLE_DIRECTION);
255
256	dev_dbg(priv->dev, "attached state: %s, polarity: %d\n",
257		tusb_attached_states[state], polarity);
258
259	extcon_set_state(priv->edev, EXTCON_USB,
260			 state == TUSB320_ATTACHED_STATE_UFP);
261	extcon_set_state(priv->edev, EXTCON_USB_HOST,
262			 state == TUSB320_ATTACHED_STATE_DFP);
263	extcon_set_property(priv->edev, EXTCON_USB,
264			    EXTCON_PROP_USB_TYPEC_POLARITY,
265			    (union extcon_property_value)polarity);
266	extcon_set_property(priv->edev, EXTCON_USB_HOST,
267			    EXTCON_PROP_USB_TYPEC_POLARITY,
268			    (union extcon_property_value)polarity);
269	extcon_sync(priv->edev, EXTCON_USB);
270	extcon_sync(priv->edev, EXTCON_USB_HOST);
271
272	priv->state = state;
273}
274
275static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9)
276{
277	struct typec_port *port = priv->port;
278	struct device *dev = priv->dev;
279	u8 mode, role, state;
280	int ret, reg8;
281	bool ori;
282
283	ori = reg9 & TUSB320_REG9_CABLE_DIRECTION;
284	typec_set_orientation(port, ori ? TYPEC_ORIENTATION_REVERSE :
285					  TYPEC_ORIENTATION_NORMAL);
286
287	state = (reg9 >> TUSB320_REG9_ATTACHED_STATE_SHIFT) &
288		TUSB320_REG9_ATTACHED_STATE_MASK;
289	if (state == TUSB320_ATTACHED_STATE_DFP)
290		role = TYPEC_SOURCE;
291	else
292		role = TYPEC_SINK;
293
294	typec_set_vconn_role(port, role);
295	typec_set_pwr_role(port, role);
296	typec_set_data_role(port, role == TYPEC_SOURCE ?
297				  TYPEC_HOST : TYPEC_DEVICE);
298
299	ret = regmap_read(priv->regmap, TUSB320_REG8, &reg8);
300	if (ret) {
301		dev_err(dev, "error during reg8 i2c read, ret=%d!\n", ret);
302		return;
303	}
304
305	mode = FIELD_GET(TUSB320_REG8_CURRENT_MODE_DETECT, reg8);
306	if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_DEF)
307		typec_set_pwr_opmode(port, TYPEC_PWR_MODE_USB);
308	else if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_MED)
309		typec_set_pwr_opmode(port, TYPEC_PWR_MODE_1_5A);
310	else if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_HI)
311		typec_set_pwr_opmode(port, TYPEC_PWR_MODE_3_0A);
312	else	/* Charge through accessory */
313		typec_set_pwr_opmode(port, TYPEC_PWR_MODE_USB);
314}
315
316static irqreturn_t tusb320_state_update_handler(struct tusb320_priv *priv,
317						bool force_update)
318{
319	unsigned int reg;
320
321	if (regmap_read(priv->regmap, TUSB320_REG9, &reg)) {
322		dev_err(priv->dev, "error during i2c read!\n");
323		return IRQ_NONE;
324	}
325
326	if (!force_update && !(reg & TUSB320_REG9_INTERRUPT_STATUS))
327		return IRQ_NONE;
328
329	tusb320_extcon_irq_handler(priv, reg);
330
331	/*
332	 * Type-C support is optional. Only call the Type-C handler if a
333	 * port had been registered previously.
334	 */
335	if (priv->port)
336		tusb320_typec_irq_handler(priv, reg);
337
338	regmap_write(priv->regmap, TUSB320_REG9, reg);
339
340	return IRQ_HANDLED;
341}
342
343static irqreturn_t tusb320_irq_handler(int irq, void *dev_id)
344{
345	struct tusb320_priv *priv = dev_id;
346
347	return tusb320_state_update_handler(priv, false);
348}
349
350static const struct regmap_config tusb320_regmap_config = {
351	.reg_bits = 8,
352	.val_bits = 8,
353};
354
355static int tusb320_extcon_probe(struct tusb320_priv *priv)
356{
357	int ret;
358
359	priv->edev = devm_extcon_dev_allocate(priv->dev, tusb320_extcon_cable);
360	if (IS_ERR(priv->edev)) {
361		dev_err(priv->dev, "failed to allocate extcon device\n");
362		return PTR_ERR(priv->edev);
363	}
364
365	ret = devm_extcon_dev_register(priv->dev, priv->edev);
366	if (ret < 0) {
367		dev_err(priv->dev, "failed to register extcon device\n");
368		return ret;
369	}
370
371	extcon_set_property_capability(priv->edev, EXTCON_USB,
372				       EXTCON_PROP_USB_TYPEC_POLARITY);
373	extcon_set_property_capability(priv->edev, EXTCON_USB_HOST,
374				       EXTCON_PROP_USB_TYPEC_POLARITY);
375
376	return 0;
377}
378
379static int tusb320_typec_probe(struct i2c_client *client,
380			       struct tusb320_priv *priv)
381{
382	struct fwnode_handle *connector;
383	const char *cap_str;
384	int ret;
385
386	/* The Type-C connector is optional, for backward compatibility. */
387	connector = device_get_named_child_node(&client->dev, "connector");
388	if (!connector)
389		return 0;
390
391	/* Type-C connector found. */
392	ret = typec_get_fw_cap(&priv->cap, connector);
393	if (ret)
394		return ret;
395
396	priv->port_type = priv->cap.type;
397
398	/* This goes into register 0x8 field CURRENT_MODE_ADVERTISE */
399	ret = fwnode_property_read_string(connector, "typec-power-opmode", &cap_str);
400	if (ret)
401		return ret;
402
403	ret = typec_find_pwr_opmode(cap_str);
404	if (ret < 0)
405		return ret;
406	if (ret == TYPEC_PWR_MODE_PD)
407		return -EINVAL;
408
409	priv->pwr_opmode = ret;
410
411	/* Initialize the hardware with the devicetree settings. */
412	ret = tusb320_set_adv_pwr_mode(priv);
413	if (ret)
414		return ret;
415
416	priv->cap.revision		= USB_TYPEC_REV_1_1;
417	priv->cap.accessory[0]		= TYPEC_ACCESSORY_AUDIO;
418	priv->cap.accessory[1]		= TYPEC_ACCESSORY_DEBUG;
419	priv->cap.orientation_aware	= true;
420	priv->cap.driver_data		= priv;
421	priv->cap.ops			= &tusb320_typec_ops;
422	priv->cap.fwnode		= connector;
423
424	priv->port = typec_register_port(&client->dev, &priv->cap);
425	if (IS_ERR(priv->port))
426		return PTR_ERR(priv->port);
427
428	return 0;
429}
430
431static int tusb320_probe(struct i2c_client *client)
432{
433	struct tusb320_priv *priv;
434	const void *match_data;
435	unsigned int revision;
436	int ret;
437
438	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
439	if (!priv)
440		return -ENOMEM;
441	priv->dev = &client->dev;
442
443	priv->regmap = devm_regmap_init_i2c(client, &tusb320_regmap_config);
444	if (IS_ERR(priv->regmap))
445		return PTR_ERR(priv->regmap);
446
447	ret = tusb320_check_signature(priv);
448	if (ret)
449		return ret;
450
451	match_data = device_get_match_data(&client->dev);
452	if (!match_data)
453		return -EINVAL;
454
455	priv->ops = (struct tusb320_ops*)match_data;
456
457	if (priv->ops->get_revision) {
458		ret = priv->ops->get_revision(priv, &revision);
459		if (ret)
460			dev_warn(priv->dev,
461				"failed to read revision register: %d\n", ret);
462		else
463			dev_info(priv->dev, "chip revision %d\n", revision);
464	}
465
466	ret = tusb320_extcon_probe(priv);
467	if (ret)
468		return ret;
469
470	ret = tusb320_typec_probe(client, priv);
471	if (ret)
472		return ret;
473
474	/* update initial state */
475	tusb320_state_update_handler(priv, true);
476
477	/* Reset chip to its default state */
478	ret = tusb320_reset(priv);
479	if (ret)
480		dev_warn(priv->dev, "failed to reset chip: %d\n", ret);
481	else
482		/*
483		 * State and polarity might change after a reset, so update
484		 * them again and make sure the interrupt status bit is cleared.
485		 */
486		tusb320_state_update_handler(priv, true);
487
488	ret = devm_request_threaded_irq(priv->dev, client->irq, NULL,
489					tusb320_irq_handler,
490					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
491					client->name, priv);
492
493	return ret;
494}
495
496static const struct of_device_id tusb320_extcon_dt_match[] = {
497	{ .compatible = "ti,tusb320", .data = &tusb320_ops, },
498	{ .compatible = "ti,tusb320l", .data = &tusb320l_ops, },
499	{ }
500};
501MODULE_DEVICE_TABLE(of, tusb320_extcon_dt_match);
502
503static struct i2c_driver tusb320_extcon_driver = {
504	.probe_new	= tusb320_probe,
505	.driver		= {
506		.name	= "extcon-tusb320",
507		.of_match_table = tusb320_extcon_dt_match,
508	},
509};
510
511static int __init tusb320_init(void)
512{
513	return i2c_add_driver(&tusb320_extcon_driver);
514}
515subsys_initcall(tusb320_init);
516
517static void __exit tusb320_exit(void)
518{
519	i2c_del_driver(&tusb320_extcon_driver);
520}
521module_exit(tusb320_exit);
522
523MODULE_AUTHOR("Michael Auchter <michael.auchter@ni.com>");
524MODULE_DESCRIPTION("TI TUSB320 extcon driver");
525MODULE_LICENSE("GPL v2");