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>
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)
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)
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
48 #define TUSB320L_REGA0_REVISION 0xa0
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,
67 int (*set_mode)(struct tusb320_priv *priv, enum tusb320_mode mode);
68 int (*get_revision)(struct tusb320_priv *priv, unsigned int *revision);
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 struct fwnode_handle *connector_fwnode;
84 static const char * const tusb_attached_states[] = {
85 [TUSB320_ATTACHED_STATE_NONE] = "not attached",
86 [TUSB320_ATTACHED_STATE_DFP] = "downstream facing port",
87 [TUSB320_ATTACHED_STATE_UFP] = "upstream facing port",
88 [TUSB320_ATTACHED_STATE_ACC] = "accessory",
91 static const unsigned int tusb320_extcon_cable[] = {
97 static int tusb320_check_signature(struct tusb320_priv *priv)
99 static const char sig[] = { '\0', 'T', 'U', 'S', 'B', '3', '2', '0' };
103 for (i = 0; i < sizeof(sig); i++) {
104 ret = regmap_read(priv->regmap, sizeof(sig) - 1 - i, &val);
108 dev_err(priv->dev, "signature mismatch!\n");
116 static int tusb320_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
120 /* Mode cannot be changed while cable is attached */
121 if (priv->state != TUSB320_ATTACHED_STATE_NONE)
125 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
126 TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT,
127 mode << TUSB320_REGA_MODE_SELECT_SHIFT);
129 dev_err(priv->dev, "failed to write mode: %d\n", ret);
136 static int tusb320l_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
140 /* Disable CC state machine */
141 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
142 TUSB320L_REGA_DISABLE_TERM, 1);
145 "failed to disable CC state machine: %d\n", ret);
150 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
151 TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT,
152 mode << TUSB320_REGA_MODE_SELECT_SHIFT);
154 dev_err(priv->dev, "failed to write mode: %d\n", ret);
160 /* Re-enable CC state machine */
161 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
162 TUSB320L_REGA_DISABLE_TERM, 0);
165 "failed to re-enable CC state machine: %d\n", ret);
170 static int tusb320_reset(struct tusb320_priv *priv)
174 /* Set mode to default (follow PORT pin) */
175 ret = priv->ops->set_mode(priv, TUSB320_MODE_PORT);
176 if (ret && ret != -EBUSY) {
178 "failed to set mode to PORT: %d\n", ret);
182 /* Perform soft reset */
183 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
184 TUSB320_REGA_I2C_SOFT_RESET, 1);
187 "failed to write soft reset bit: %d\n", ret);
191 /* Wait for chip to go through reset */
197 static int tusb320l_get_revision(struct tusb320_priv *priv, unsigned int *revision)
199 return regmap_read(priv->regmap, TUSB320L_REGA0_REVISION, revision);
202 static struct tusb320_ops tusb320_ops = {
203 .set_mode = tusb320_set_mode,
206 static struct tusb320_ops tusb320l_ops = {
207 .set_mode = tusb320l_set_mode,
208 .get_revision = tusb320l_get_revision,
211 static int tusb320_set_adv_pwr_mode(struct tusb320_priv *priv)
215 if (priv->pwr_opmode == TYPEC_PWR_MODE_USB)
216 mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_USB;
217 else if (priv->pwr_opmode == TYPEC_PWR_MODE_1_5A)
218 mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_15A;
219 else if (priv->pwr_opmode == TYPEC_PWR_MODE_3_0A)
220 mode = TUSB320_REG8_CURRENT_MODE_ADVERTISE_30A;
221 else /* No other mode is supported. */
224 return regmap_write_bits(priv->regmap, TUSB320_REG8,
225 TUSB320_REG8_CURRENT_MODE_ADVERTISE,
226 FIELD_PREP(TUSB320_REG8_CURRENT_MODE_ADVERTISE,
230 static int tusb320_port_type_set(struct typec_port *port,
231 enum typec_port_type type)
233 struct tusb320_priv *priv = typec_get_drvdata(port);
235 if (type == TYPEC_PORT_SRC)
236 return priv->ops->set_mode(priv, TUSB320_MODE_DFP);
237 else if (type == TYPEC_PORT_SNK)
238 return priv->ops->set_mode(priv, TUSB320_MODE_UFP);
239 else if (type == TYPEC_PORT_DRP)
240 return priv->ops->set_mode(priv, TUSB320_MODE_DRP);
242 return priv->ops->set_mode(priv, TUSB320_MODE_PORT);
245 static const struct typec_operations tusb320_typec_ops = {
246 .port_type_set = tusb320_port_type_set,
249 static void tusb320_extcon_irq_handler(struct tusb320_priv *priv, u8 reg)
253 state = (reg >> TUSB320_REG9_ATTACHED_STATE_SHIFT) &
254 TUSB320_REG9_ATTACHED_STATE_MASK;
255 polarity = !!(reg & TUSB320_REG9_CABLE_DIRECTION);
257 dev_dbg(priv->dev, "attached state: %s, polarity: %d\n",
258 tusb_attached_states[state], polarity);
260 extcon_set_state(priv->edev, EXTCON_USB,
261 state == TUSB320_ATTACHED_STATE_UFP);
262 extcon_set_state(priv->edev, EXTCON_USB_HOST,
263 state == TUSB320_ATTACHED_STATE_DFP);
264 extcon_set_property(priv->edev, EXTCON_USB,
265 EXTCON_PROP_USB_TYPEC_POLARITY,
266 (union extcon_property_value)polarity);
267 extcon_set_property(priv->edev, EXTCON_USB_HOST,
268 EXTCON_PROP_USB_TYPEC_POLARITY,
269 (union extcon_property_value)polarity);
270 extcon_sync(priv->edev, EXTCON_USB);
271 extcon_sync(priv->edev, EXTCON_USB_HOST);
276 static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9)
278 struct typec_port *port = priv->port;
279 struct device *dev = priv->dev;
280 u8 mode, role, state;
284 ori = reg9 & TUSB320_REG9_CABLE_DIRECTION;
285 typec_set_orientation(port, ori ? TYPEC_ORIENTATION_REVERSE :
286 TYPEC_ORIENTATION_NORMAL);
288 state = (reg9 >> TUSB320_REG9_ATTACHED_STATE_SHIFT) &
289 TUSB320_REG9_ATTACHED_STATE_MASK;
290 if (state == TUSB320_ATTACHED_STATE_DFP)
295 typec_set_vconn_role(port, role);
296 typec_set_pwr_role(port, role);
297 typec_set_data_role(port, role == TYPEC_SOURCE ?
298 TYPEC_HOST : TYPEC_DEVICE);
300 ret = regmap_read(priv->regmap, TUSB320_REG8, ®8);
302 dev_err(dev, "error during reg8 i2c read, ret=%d!\n", ret);
306 mode = FIELD_GET(TUSB320_REG8_CURRENT_MODE_DETECT, reg8);
307 if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_DEF)
308 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_USB);
309 else if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_MED)
310 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_1_5A);
311 else if (mode == TUSB320_REG8_CURRENT_MODE_DETECT_HI)
312 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_3_0A);
313 else /* Charge through accessory */
314 typec_set_pwr_opmode(port, TYPEC_PWR_MODE_USB);
317 static irqreturn_t tusb320_state_update_handler(struct tusb320_priv *priv,
322 if (regmap_read(priv->regmap, TUSB320_REG9, ®)) {
323 dev_err(priv->dev, "error during i2c read!\n");
327 if (!force_update && !(reg & TUSB320_REG9_INTERRUPT_STATUS))
330 tusb320_extcon_irq_handler(priv, reg);
333 * Type-C support is optional. Only call the Type-C handler if a
334 * port had been registered previously.
337 tusb320_typec_irq_handler(priv, reg);
339 regmap_write(priv->regmap, TUSB320_REG9, reg);
344 static irqreturn_t tusb320_irq_handler(int irq, void *dev_id)
346 struct tusb320_priv *priv = dev_id;
348 return tusb320_state_update_handler(priv, false);
351 static const struct regmap_config tusb320_regmap_config = {
356 static int tusb320_extcon_probe(struct tusb320_priv *priv)
360 priv->edev = devm_extcon_dev_allocate(priv->dev, tusb320_extcon_cable);
361 if (IS_ERR(priv->edev)) {
362 dev_err(priv->dev, "failed to allocate extcon device\n");
363 return PTR_ERR(priv->edev);
366 ret = devm_extcon_dev_register(priv->dev, priv->edev);
368 dev_err(priv->dev, "failed to register extcon device\n");
372 extcon_set_property_capability(priv->edev, EXTCON_USB,
373 EXTCON_PROP_USB_TYPEC_POLARITY);
374 extcon_set_property_capability(priv->edev, EXTCON_USB_HOST,
375 EXTCON_PROP_USB_TYPEC_POLARITY);
380 static int tusb320_typec_probe(struct i2c_client *client,
381 struct tusb320_priv *priv)
383 struct fwnode_handle *connector;
387 /* The Type-C connector is optional, for backward compatibility. */
388 connector = device_get_named_child_node(&client->dev, "connector");
392 /* Type-C connector found. */
393 ret = typec_get_fw_cap(&priv->cap, connector);
397 priv->port_type = priv->cap.type;
399 /* This goes into register 0x8 field CURRENT_MODE_ADVERTISE */
400 ret = fwnode_property_read_string(connector, "typec-power-opmode", &cap_str);
404 ret = typec_find_pwr_opmode(cap_str);
408 priv->pwr_opmode = ret;
410 /* Initialize the hardware with the devicetree settings. */
411 ret = tusb320_set_adv_pwr_mode(priv);
415 priv->cap.revision = USB_TYPEC_REV_1_1;
416 priv->cap.accessory[0] = TYPEC_ACCESSORY_AUDIO;
417 priv->cap.accessory[1] = TYPEC_ACCESSORY_DEBUG;
418 priv->cap.orientation_aware = true;
419 priv->cap.driver_data = priv;
420 priv->cap.ops = &tusb320_typec_ops;
421 priv->cap.fwnode = connector;
423 priv->port = typec_register_port(&client->dev, &priv->cap);
424 if (IS_ERR(priv->port)) {
425 ret = PTR_ERR(priv->port);
429 priv->connector_fwnode = connector;
434 fwnode_handle_put(connector);
439 static void tusb320_typec_remove(struct tusb320_priv *priv)
441 typec_unregister_port(priv->port);
442 fwnode_handle_put(priv->connector_fwnode);
445 static int tusb320_probe(struct i2c_client *client)
447 struct tusb320_priv *priv;
448 const void *match_data;
449 unsigned int revision;
452 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
456 priv->dev = &client->dev;
457 i2c_set_clientdata(client, priv);
459 priv->regmap = devm_regmap_init_i2c(client, &tusb320_regmap_config);
460 if (IS_ERR(priv->regmap))
461 return PTR_ERR(priv->regmap);
463 ret = tusb320_check_signature(priv);
467 match_data = device_get_match_data(&client->dev);
471 priv->ops = (struct tusb320_ops*)match_data;
473 if (priv->ops->get_revision) {
474 ret = priv->ops->get_revision(priv, &revision);
477 "failed to read revision register: %d\n", ret);
479 dev_info(priv->dev, "chip revision %d\n", revision);
482 ret = tusb320_extcon_probe(priv);
486 ret = tusb320_typec_probe(client, priv);
490 /* update initial state */
491 tusb320_state_update_handler(priv, true);
493 /* Reset chip to its default state */
494 ret = tusb320_reset(priv);
496 dev_warn(priv->dev, "failed to reset chip: %d\n", ret);
499 * State and polarity might change after a reset, so update
500 * them again and make sure the interrupt status bit is cleared.
502 tusb320_state_update_handler(priv, true);
504 ret = devm_request_threaded_irq(priv->dev, client->irq, NULL,
506 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
509 tusb320_typec_remove(priv);
514 static void tusb320_remove(struct i2c_client *client)
516 struct tusb320_priv *priv = i2c_get_clientdata(client);
518 tusb320_typec_remove(priv);
521 static const struct of_device_id tusb320_extcon_dt_match[] = {
522 { .compatible = "ti,tusb320", .data = &tusb320_ops, },
523 { .compatible = "ti,tusb320l", .data = &tusb320l_ops, },
526 MODULE_DEVICE_TABLE(of, tusb320_extcon_dt_match);
528 static struct i2c_driver tusb320_extcon_driver = {
529 .probe_new = tusb320_probe,
530 .remove = tusb320_remove,
532 .name = "extcon-tusb320",
533 .of_match_table = tusb320_extcon_dt_match,
537 static int __init tusb320_init(void)
539 return i2c_add_driver(&tusb320_extcon_driver);
541 subsys_initcall(tusb320_init);
543 static void __exit tusb320_exit(void)
545 i2c_del_driver(&tusb320_extcon_driver);
547 module_exit(tusb320_exit);
549 MODULE_AUTHOR("Michael Auchter <michael.auchter@ni.com>");
550 MODULE_DESCRIPTION("TI TUSB320 extcon driver");
551 MODULE_LICENSE("GPL v2");