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 
50 enum 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 
57 enum tusb320_mode {
58 	TUSB320_MODE_PORT,
59 	TUSB320_MODE_UFP,
60 	TUSB320_MODE_DFP,
61 	TUSB320_MODE_DRP,
62 };
63 
64 struct tusb320_priv;
65 
66 struct 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 
71 struct 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 
83 static 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 
90 static const unsigned int tusb320_extcon_cable[] = {
91 	EXTCON_USB,
92 	EXTCON_USB_HOST,
93 	EXTCON_NONE,
94 };
95 
96 static 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 
115 static 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 
135 static 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);
158 err:
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 
169 static 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 
196 static int tusb320l_get_revision(struct tusb320_priv *priv, unsigned int *revision)
197 {
198 	return regmap_read(priv->regmap, TUSB320L_REGA0_REVISION, revision);
199 }
200 
201 static struct tusb320_ops tusb320_ops = {
202 	.set_mode = tusb320_set_mode,
203 };
204 
205 static struct tusb320_ops tusb320l_ops = {
206 	.set_mode = tusb320l_set_mode,
207 	.get_revision = tusb320l_get_revision,
208 };
209 
210 static 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 
229 static 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 
244 static const struct typec_operations tusb320_typec_ops = {
245 	.port_type_set	= tusb320_port_type_set,
246 };
247 
248 static 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 
275 static 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 
316 static irqreturn_t tusb320_irq_handler(int irq, void *dev_id)
317 {
318 	struct tusb320_priv *priv = dev_id;
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 (!(reg & TUSB320_REG9_INTERRUPT_STATUS))
327 		return IRQ_NONE;
328 
329 	tusb320_extcon_irq_handler(priv, reg);
330 	tusb320_typec_irq_handler(priv, reg);
331 
332 	regmap_write(priv->regmap, TUSB320_REG9, reg);
333 
334 	return IRQ_HANDLED;
335 }
336 
337 static const struct regmap_config tusb320_regmap_config = {
338 	.reg_bits = 8,
339 	.val_bits = 8,
340 };
341 
342 static int tusb320_extcon_probe(struct tusb320_priv *priv)
343 {
344 	int ret;
345 
346 	priv->edev = devm_extcon_dev_allocate(priv->dev, tusb320_extcon_cable);
347 	if (IS_ERR(priv->edev)) {
348 		dev_err(priv->dev, "failed to allocate extcon device\n");
349 		return PTR_ERR(priv->edev);
350 	}
351 
352 	ret = devm_extcon_dev_register(priv->dev, priv->edev);
353 	if (ret < 0) {
354 		dev_err(priv->dev, "failed to register extcon device\n");
355 		return ret;
356 	}
357 
358 	extcon_set_property_capability(priv->edev, EXTCON_USB,
359 				       EXTCON_PROP_USB_TYPEC_POLARITY);
360 	extcon_set_property_capability(priv->edev, EXTCON_USB_HOST,
361 				       EXTCON_PROP_USB_TYPEC_POLARITY);
362 
363 	return 0;
364 }
365 
366 static int tusb320_typec_probe(struct i2c_client *client,
367 			       struct tusb320_priv *priv)
368 {
369 	struct fwnode_handle *connector;
370 	const char *cap_str;
371 	int ret;
372 
373 	/* The Type-C connector is optional, for backward compatibility. */
374 	connector = device_get_named_child_node(&client->dev, "connector");
375 	if (!connector)
376 		return 0;
377 
378 	/* Type-C connector found. */
379 	ret = typec_get_fw_cap(&priv->cap, connector);
380 	if (ret)
381 		return ret;
382 
383 	priv->port_type = priv->cap.type;
384 
385 	/* This goes into register 0x8 field CURRENT_MODE_ADVERTISE */
386 	ret = fwnode_property_read_string(connector, "typec-power-opmode", &cap_str);
387 	if (ret)
388 		return ret;
389 
390 	ret = typec_find_pwr_opmode(cap_str);
391 	if (ret < 0)
392 		return ret;
393 	if (ret == TYPEC_PWR_MODE_PD)
394 		return -EINVAL;
395 
396 	priv->pwr_opmode = ret;
397 
398 	/* Initialize the hardware with the devicetree settings. */
399 	ret = tusb320_set_adv_pwr_mode(priv);
400 	if (ret)
401 		return ret;
402 
403 	priv->cap.revision		= USB_TYPEC_REV_1_1;
404 	priv->cap.accessory[0]		= TYPEC_ACCESSORY_AUDIO;
405 	priv->cap.accessory[1]		= TYPEC_ACCESSORY_DEBUG;
406 	priv->cap.orientation_aware	= true;
407 	priv->cap.driver_data		= priv;
408 	priv->cap.ops			= &tusb320_typec_ops;
409 	priv->cap.fwnode		= connector;
410 
411 	priv->port = typec_register_port(&client->dev, &priv->cap);
412 	if (IS_ERR(priv->port))
413 		return PTR_ERR(priv->port);
414 
415 	return 0;
416 }
417 
418 static int tusb320_probe(struct i2c_client *client,
419 			 const struct i2c_device_id *id)
420 {
421 	struct tusb320_priv *priv;
422 	const void *match_data;
423 	unsigned int revision;
424 	int ret;
425 
426 	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
427 	if (!priv)
428 		return -ENOMEM;
429 	priv->dev = &client->dev;
430 
431 	priv->regmap = devm_regmap_init_i2c(client, &tusb320_regmap_config);
432 	if (IS_ERR(priv->regmap))
433 		return PTR_ERR(priv->regmap);
434 
435 	ret = tusb320_check_signature(priv);
436 	if (ret)
437 		return ret;
438 
439 	match_data = device_get_match_data(&client->dev);
440 	if (!match_data)
441 		return -EINVAL;
442 
443 	priv->ops = (struct tusb320_ops*)match_data;
444 
445 	if (priv->ops->get_revision) {
446 		ret = priv->ops->get_revision(priv, &revision);
447 		if (ret)
448 			dev_warn(priv->dev,
449 				"failed to read revision register: %d\n", ret);
450 		else
451 			dev_info(priv->dev, "chip revision %d\n", revision);
452 	}
453 
454 	ret = tusb320_extcon_probe(priv);
455 	if (ret)
456 		return ret;
457 
458 	ret = tusb320_typec_probe(client, priv);
459 	if (ret)
460 		return ret;
461 
462 	/* update initial state */
463 	tusb320_irq_handler(client->irq, priv);
464 
465 	/* Reset chip to its default state */
466 	ret = tusb320_reset(priv);
467 	if (ret)
468 		dev_warn(priv->dev, "failed to reset chip: %d\n", ret);
469 	else
470 		/*
471 		 * State and polarity might change after a reset, so update
472 		 * them again and make sure the interrupt status bit is cleared.
473 		 */
474 		tusb320_irq_handler(client->irq, priv);
475 
476 	ret = devm_request_threaded_irq(priv->dev, client->irq, NULL,
477 					tusb320_irq_handler,
478 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
479 					client->name, priv);
480 
481 	return ret;
482 }
483 
484 static const struct of_device_id tusb320_extcon_dt_match[] = {
485 	{ .compatible = "ti,tusb320", .data = &tusb320_ops, },
486 	{ .compatible = "ti,tusb320l", .data = &tusb320l_ops, },
487 	{ }
488 };
489 MODULE_DEVICE_TABLE(of, tusb320_extcon_dt_match);
490 
491 static struct i2c_driver tusb320_extcon_driver = {
492 	.probe		= tusb320_probe,
493 	.driver		= {
494 		.name	= "extcon-tusb320",
495 		.of_match_table = tusb320_extcon_dt_match,
496 	},
497 };
498 
499 static int __init tusb320_init(void)
500 {
501 	return i2c_add_driver(&tusb320_extcon_driver);
502 }
503 subsys_initcall(tusb320_init);
504 
505 static void __exit tusb320_exit(void)
506 {
507 	i2c_del_driver(&tusb320_extcon_driver);
508 }
509 module_exit(tusb320_exit);
510 
511 MODULE_AUTHOR("Michael Auchter <michael.auchter@ni.com>");
512 MODULE_DESCRIPTION("TI TUSB320 extcon driver");
513 MODULE_LICENSE("GPL v2");
514