1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/extcon/extcon-tusb320.c - TUSB320 extcon driver
5 * Copyright (C) 2020 National Instruments Corporation
6 * Author: Michael Auchter <michael.auchter@ni.com>
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 #include <linux/usb/typec_altmode.h>
19 #include <linux/usb/role.h>
21 #define TUSB320_REG8 0x8
22 #define TUSB320_REG8_CURRENT_MODE_ADVERTISE GENMASK(7, 6)
23 #define TUSB320_REG8_CURRENT_MODE_ADVERTISE_USB 0x0
24 #define TUSB320_REG8_CURRENT_MODE_ADVERTISE_15A 0x1
25 #define TUSB320_REG8_CURRENT_MODE_ADVERTISE_30A 0x2
26 #define TUSB320_REG8_CURRENT_MODE_DETECT GENMASK(5, 4)
27 #define TUSB320_REG8_CURRENT_MODE_DETECT_DEF 0x0
28 #define TUSB320_REG8_CURRENT_MODE_DETECT_MED 0x1
29 #define TUSB320_REG8_CURRENT_MODE_DETECT_ACC 0x2
30 #define TUSB320_REG8_CURRENT_MODE_DETECT_HI 0x3
31 #define TUSB320_REG8_ACCESSORY_CONNECTED GENMASK(3, 1)
32 #define TUSB320_REG8_ACCESSORY_CONNECTED_NONE 0x0
33 #define TUSB320_REG8_ACCESSORY_CONNECTED_AUDIO 0x4
34 #define TUSB320_REG8_ACCESSORY_CONNECTED_ACHRG 0x5
35 #define TUSB320_REG8_ACCESSORY_CONNECTED_DBGDFP 0x6
36 #define TUSB320_REG8_ACCESSORY_CONNECTED_DBGUFP 0x7
37 #define TUSB320_REG8_ACTIVE_CABLE_DETECTION BIT(0)
39 #define TUSB320_REG9 0x9
40 #define TUSB320_REG9_ATTACHED_STATE GENMASK(7, 6)
41 #define TUSB320_REG9_CABLE_DIRECTION BIT(5)
42 #define TUSB320_REG9_INTERRUPT_STATUS BIT(4)
44 #define TUSB320_REGA 0xa
45 #define TUSB320L_REGA_DISABLE_TERM BIT(0)
46 #define TUSB320_REGA_I2C_SOFT_RESET BIT(3)
47 #define TUSB320_REGA_MODE_SELECT_SHIFT 4
48 #define TUSB320_REGA_MODE_SELECT_MASK 0x3
50 #define TUSB320L_REGA0_REVISION 0xa0
52 enum tusb320_attached_state {
53 TUSB320_ATTACHED_STATE_NONE,
54 TUSB320_ATTACHED_STATE_DFP,
55 TUSB320_ATTACHED_STATE_UFP,
56 TUSB320_ATTACHED_STATE_ACC,
69 int (*set_mode)(struct tusb320_priv *priv, enum tusb320_mode mode);
70 int (*get_revision)(struct tusb320_priv *priv, unsigned int *revision);
75 struct regmap *regmap;
76 struct extcon_dev *edev;
77 struct tusb320_ops *ops;
78 enum tusb320_attached_state state;
79 struct typec_port *port;
80 struct typec_capability cap;
81 enum typec_port_type port_type;
82 enum typec_pwr_opmode pwr_opmode;
83 struct fwnode_handle *connector_fwnode;
84 struct usb_role_switch *role_sw;
87 static const char * const tusb_attached_states[] = {
88 [TUSB320_ATTACHED_STATE_NONE] = "not attached",
89 [TUSB320_ATTACHED_STATE_DFP] = "downstream facing port",
90 [TUSB320_ATTACHED_STATE_UFP] = "upstream facing port",
91 [TUSB320_ATTACHED_STATE_ACC] = "accessory",
94 static const unsigned int tusb320_extcon_cable[] = {
100 static int tusb320_check_signature(struct tusb320_priv *priv)
102 static const char sig[] = { '\0', 'T', 'U', 'S', 'B', '3', '2', '0' };
106 for (i = 0; i < sizeof(sig); i++) {
107 ret = regmap_read(priv->regmap, sizeof(sig) - 1 - i, &val);
111 dev_err(priv->dev, "signature mismatch!\n");
119 static int tusb320_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
123 /* Mode cannot be changed while cable is attached */
124 if (priv->state != TUSB320_ATTACHED_STATE_NONE)
128 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
129 TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT,
130 mode << TUSB320_REGA_MODE_SELECT_SHIFT);
132 dev_err(priv->dev, "failed to write mode: %d\n", ret);
139 static int tusb320l_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
143 /* Disable CC state machine */
144 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
145 TUSB320L_REGA_DISABLE_TERM, 1);
148 "failed to disable CC state machine: %d\n", ret);
153 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
154 TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT,
155 mode << TUSB320_REGA_MODE_SELECT_SHIFT);
157 dev_err(priv->dev, "failed to write mode: %d\n", ret);
163 /* Re-enable CC state machine */
164 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
165 TUSB320L_REGA_DISABLE_TERM, 0);
168 "failed to re-enable CC state machine: %d\n", ret);
173 static int tusb320_reset(struct tusb320_priv *priv)
177 /* Set mode to default (follow PORT pin) */
178 ret = priv->ops->set_mode(priv, TUSB320_MODE_PORT);
179 if (ret && ret != -EBUSY) {
181 "failed to set mode to PORT: %d\n", ret);
185 /* Perform soft reset */
186 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
187 TUSB320_REGA_I2C_SOFT_RESET, 1);
190 "failed to write soft reset bit: %d\n", ret);
194 /* Wait for chip to go through reset */
200 static int tusb320l_get_revision(struct tusb320_priv *priv, unsigned int *revision)
202 return regmap_read(priv->regmap, TUSB320L_REGA0_REVISION, revision);
205 static struct tusb320_ops tusb320_ops = {
206 .set_mode = tusb320_set_mode,
209 static struct tusb320_ops tusb320l_ops = {
210 .set_mode = tusb320l_set_mode,
211 .get_revision = tusb320l_get_revision,
214 static int tusb320_set_adv_pwr_mode(struct tusb320_priv *priv)
218 if (priv->pwr_opmode == TYPEC_PWR_MODE_USB)
219 mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_USB;
220 else if (priv->pwr_opmode == TYPEC_PWR_MODE_1_5A)
221 mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_15A;
222 else if (priv->pwr_opmode == TYPEC_PWR_MODE_3_0A)
223 mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_30A;
224 else /* No other mode is supported. */
227 return regmap_write_bits(priv->regmap, TUSB320_REG8,
228 TUSB320_REG8_CURRENT_MODE_ADVERTISE,
229 FIELD_PREP(TUSB320_REG8_CURRENT_MODE_ADVERTISE,
233 static int tusb320_port_type_set(struct typec_port *port,
234 enum typec_port_type type)
236 struct tusb320_priv *priv = typec_get_drvdata(port);
238 if (type == TYPEC_PORT_SRC)
239 return priv->ops->set_mode(priv, TUSB320_MODE_DFP);
240 else if (type == TYPEC_PORT_SNK)
241 return priv->ops->set_mode(priv, TUSB320_MODE_UFP);
242 else if (type == TYPEC_PORT_DRP)
243 return priv->ops->set_mode(priv, TUSB320_MODE_DRP);
245 return priv->ops->set_mode(priv, TUSB320_MODE_PORT);
248 static const struct typec_operations tusb320_typec_ops = {
249 .port_type_set = tusb320_port_type_set,
252 static void tusb320_extcon_irq_handler(struct tusb320_priv *priv, u8 reg)
256 state = FIELD_GET(TUSB320_REG9_ATTACHED_STATE, reg);
257 polarity = !!(reg & TUSB320_REG9_CABLE_DIRECTION);
259 dev_dbg(priv->dev, "attached state: %s, polarity: %d\n",
260 tusb_attached_states[state], polarity);
262 extcon_set_state(priv->edev, EXTCON_USB,
263 state == TUSB320_ATTACHED_STATE_UFP);
264 extcon_set_state(priv->edev, EXTCON_USB_HOST,
265 state == TUSB320_ATTACHED_STATE_DFP);
266 extcon_set_property(priv->edev, EXTCON_USB,
267 EXTCON_PROP_USB_TYPEC_POLARITY,
268 (union extcon_property_value)polarity);
269 extcon_set_property(priv->edev, EXTCON_USB_HOST,
270 EXTCON_PROP_USB_TYPEC_POLARITY,
271 (union extcon_property_value)polarity);
272 extcon_sync(priv->edev, EXTCON_USB);
273 extcon_sync(priv->edev, EXTCON_USB_HOST);
278 static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9)
280 struct typec_port *port = priv->port;
281 struct device *dev = priv->dev;
283 enum usb_role usb_role;
284 enum typec_role pwr_role;
285 enum typec_data_role data_role;
286 u8 state, mode, accessory;
290 ret = regmap_read(priv->regmap, TUSB320_REG8, ®8);
292 dev_err(dev, "error during reg8 i2c read, ret=%d!\n", ret);
296 ori = reg9 & TUSB320_REG9_CABLE_DIRECTION;
297 typec_set_orientation(port, ori ? TYPEC_ORIENTATION_REVERSE :
298 TYPEC_ORIENTATION_NORMAL);
300 state = FIELD_GET(TUSB320_REG9_ATTACHED_STATE, reg9);
301 accessory = FIELD_GET(TUSB320_REG8_ACCESSORY_CONNECTED, reg8);
304 case TUSB320_ATTACHED_STATE_DFP:
305 typec_mode = TYPEC_MODE_USB2;
306 usb_role = USB_ROLE_HOST;
307 pwr_role = TYPEC_SOURCE;
308 data_role = TYPEC_HOST;
310 case TUSB320_ATTACHED_STATE_UFP:
311 typec_mode = TYPEC_MODE_USB2;
312 usb_role = USB_ROLE_DEVICE;
313 pwr_role = TYPEC_SINK;
314 data_role = TYPEC_DEVICE;
316 case TUSB320_ATTACHED_STATE_ACC:
318 * Accessory detected. For debug accessories, just make some
319 * qualified guesses as to the role for lack of a better option.
321 if (accessory == TUSB320_REG8_ACCESSORY_CONNECTED_AUDIO ||
322 accessory == TUSB320_REG8_ACCESSORY_CONNECTED_ACHRG) {
323 typec_mode = TYPEC_MODE_AUDIO;
324 usb_role = USB_ROLE_NONE;
325 pwr_role = TYPEC_SINK;
326 data_role = TYPEC_DEVICE;
328 } else if (accessory ==
329 TUSB320_REG8_ACCESSORY_CONNECTED_DBGDFP) {
330 typec_mode = TYPEC_MODE_DEBUG;
331 pwr_role = TYPEC_SOURCE;
332 usb_role = USB_ROLE_HOST;
333 data_role = TYPEC_HOST;
335 } else if (accessory ==
336 TUSB320_REG8_ACCESSORY_CONNECTED_DBGUFP) {
337 typec_mode = TYPEC_MODE_DEBUG;
338 pwr_role = TYPEC_SINK;
339 usb_role = USB_ROLE_DEVICE;
340 data_role = TYPEC_DEVICE;
344 dev_warn(priv->dev, "unexpected ACCESSORY_CONNECTED state %d\n",
349 typec_mode = TYPEC_MODE_USB2;
350 usb_role = USB_ROLE_NONE;
351 pwr_role = TYPEC_SINK;
352 data_role = TYPEC_DEVICE;
356 typec_set_vconn_role(port, pwr_role);
357 typec_set_pwr_role(port, pwr_role);
358 typec_set_data_role(port, data_role);
359 typec_set_mode(port, typec_mode);
360 usb_role_switch_set_role(priv->role_sw, usb_role);
362 mode = FIELD_GET(TUSB320_REG8_CURRENT_MODE_DETECT, reg8);
363 if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_DEF)
364 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_USB);
365 else if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_MED)
366 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_1_5A);
367 else if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_HI)
368 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_3_0A);
369 else /* Charge through accessory */
370 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_USB);
373 static irqreturn_t tusb320_state_update_handler(struct tusb320_priv *priv,
378 if (regmap_read(priv->regmap, TUSB320_REG9, ®)) {
379 dev_err(priv->dev, "error during i2c read!\n");
383 if (!force_update && !(reg & TUSB320_REG9_INTERRUPT_STATUS))
386 tusb320_extcon_irq_handler(priv, reg);
389 * Type-C support is optional. Only call the Type-C handler if a
390 * port had been registered previously.
393 tusb320_typec_irq_handler(priv, reg);
395 regmap_write(priv->regmap, TUSB320_REG9, reg);
400 static irqreturn_t tusb320_irq_handler(int irq, void *dev_id)
402 struct tusb320_priv *priv = dev_id;
404 return tusb320_state_update_handler(priv, false);
407 static const struct regmap_config tusb320_regmap_config = {
412 static int tusb320_extcon_probe(struct tusb320_priv *priv)
416 priv->edev = devm_extcon_dev_allocate(priv->dev, tusb320_extcon_cable);
417 if (IS_ERR(priv->edev)) {
418 dev_err(priv->dev, "failed to allocate extcon device\n");
419 return PTR_ERR(priv->edev);
422 ret = devm_extcon_dev_register(priv->dev, priv->edev);
424 dev_err(priv->dev, "failed to register extcon device\n");
428 extcon_set_property_capability(priv->edev, EXTCON_USB,
429 EXTCON_PROP_USB_TYPEC_POLARITY);
430 extcon_set_property_capability(priv->edev, EXTCON_USB_HOST,
431 EXTCON_PROP_USB_TYPEC_POLARITY);
436 static int tusb320_typec_probe(struct i2c_client *client,
437 struct tusb320_priv *priv)
439 struct fwnode_handle *connector;
443 /* The Type-C connector is optional, for backward compatibility. */
444 connector = device_get_named_child_node(&client->dev, "connector");
448 /* Type-C connector found. */
449 ret = typec_get_fw_cap(&priv->cap, connector);
453 priv->port_type = priv->cap.type;
455 /* This goes into register 0x8 field CURRENT_MODE_ADVERTISE */
456 ret = fwnode_property_read_string(connector, "typec-power-opmode", &cap_str);
460 ret = typec_find_pwr_opmode(cap_str);
464 priv->pwr_opmode = ret;
466 /* Initialize the hardware with the devicetree settings. */
467 ret = tusb320_set_adv_pwr_mode(priv);
471 priv->cap.revision = USB_TYPEC_REV_1_1;
472 priv->cap.accessory[0] = TYPEC_ACCESSORY_AUDIO;
473 priv->cap.accessory[1] = TYPEC_ACCESSORY_DEBUG;
474 priv->cap.orientation_aware = true;
475 priv->cap.driver_data = priv;
476 priv->cap.ops = &tusb320_typec_ops;
477 priv->cap.fwnode = connector;
479 priv->port = typec_register_port(&client->dev, &priv->cap);
480 if (IS_ERR(priv->port)) {
481 ret = PTR_ERR(priv->port);
485 /* Find any optional USB role switch that needs reporting to */
486 priv->role_sw = fwnode_usb_role_switch_get(connector);
487 if (IS_ERR(priv->role_sw)) {
488 ret = PTR_ERR(priv->role_sw);
492 priv->connector_fwnode = connector;
497 typec_unregister_port(priv->port);
500 fwnode_handle_put(connector);
505 static void tusb320_typec_remove(struct tusb320_priv *priv)
507 usb_role_switch_put(priv->role_sw);
508 typec_unregister_port(priv->port);
509 fwnode_handle_put(priv->connector_fwnode);
512 static int tusb320_probe(struct i2c_client *client)
514 struct tusb320_priv *priv;
515 const void *match_data;
516 unsigned int revision;
519 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
523 priv->dev = &client->dev;
524 i2c_set_clientdata(client, priv);
526 priv->regmap = devm_regmap_init_i2c(client, &tusb320_regmap_config);
527 if (IS_ERR(priv->regmap))
528 return PTR_ERR(priv->regmap);
530 ret = tusb320_check_signature(priv);
534 match_data = device_get_match_data(&client->dev);
538 priv->ops = (struct tusb320_ops*)match_data;
540 if (priv->ops->get_revision) {
541 ret = priv->ops->get_revision(priv, &revision);
544 "failed to read revision register: %d\n", ret);
546 dev_info(priv->dev, "chip revision %d\n", revision);
549 ret = tusb320_extcon_probe(priv);
553 ret = tusb320_typec_probe(client, priv);
557 /* update initial state */
558 tusb320_state_update_handler(priv, true);
560 /* Reset chip to its default state */
561 ret = tusb320_reset(priv);
563 dev_warn(priv->dev, "failed to reset chip: %d\n", ret);
566 * State and polarity might change after a reset, so update
567 * them again and make sure the interrupt status bit is cleared.
569 tusb320_state_update_handler(priv, true);
571 ret = devm_request_threaded_irq(priv->dev, client->irq, NULL,
573 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
576 tusb320_typec_remove(priv);
581 static void tusb320_remove(struct i2c_client *client)
583 struct tusb320_priv *priv = i2c_get_clientdata(client);
585 tusb320_typec_remove(priv);
588 static const struct of_device_id tusb320_extcon_dt_match[] = {
589 { .compatible = "ti,tusb320", .data = &tusb320_ops, },
590 { .compatible = "ti,tusb320l", .data = &tusb320l_ops, },
593 MODULE_DEVICE_TABLE(of, tusb320_extcon_dt_match);
595 static struct i2c_driver tusb320_extcon_driver = {
596 .probe = tusb320_probe,
597 .remove = tusb320_remove,
599 .name = "extcon-tusb320",
600 .of_match_table = tusb320_extcon_dt_match,
604 static int __init tusb320_init(void)
606 return i2c_add_driver(&tusb320_extcon_driver);
608 subsys_initcall(tusb320_init);
610 static void __exit tusb320_exit(void)
612 i2c_del_driver(&tusb320_extcon_driver);
614 module_exit(tusb320_exit);
616 MODULE_AUTHOR("Michael Auchter <michael.auchter@ni.com>");
617 MODULE_DESCRIPTION("TI TUSB320 extcon driver");
618 MODULE_LICENSE("GPL v2");