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/extcon-provider.h>
10 #include <linux/i2c.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/regmap.h>
17 #define TUSB320_REG9 0x9
18 #define TUSB320_REG9_ATTACHED_STATE_SHIFT 6
19 #define TUSB320_REG9_ATTACHED_STATE_MASK 0x3
20 #define TUSB320_REG9_CABLE_DIRECTION BIT(5)
21 #define TUSB320_REG9_INTERRUPT_STATUS BIT(4)
23 #define TUSB320_REGA 0xa
24 #define TUSB320L_REGA_DISABLE_TERM BIT(0)
25 #define TUSB320_REGA_I2C_SOFT_RESET BIT(3)
26 #define TUSB320_REGA_MODE_SELECT_SHIFT 4
27 #define TUSB320_REGA_MODE_SELECT_MASK 0x3
29 #define TUSB320L_REGA0_REVISION 0xa0
31 enum tusb320_attached_state {
32 TUSB320_ATTACHED_STATE_NONE,
33 TUSB320_ATTACHED_STATE_DFP,
34 TUSB320_ATTACHED_STATE_UFP,
35 TUSB320_ATTACHED_STATE_ACC,
48 int (*set_mode)(struct tusb320_priv *priv, enum tusb320_mode mode);
49 int (*get_revision)(struct tusb320_priv *priv, unsigned int *revision);
54 struct regmap *regmap;
55 struct extcon_dev *edev;
56 struct tusb320_ops *ops;
57 enum tusb320_attached_state state;
60 static const char * const tusb_attached_states[] = {
61 [TUSB320_ATTACHED_STATE_NONE] = "not attached",
62 [TUSB320_ATTACHED_STATE_DFP] = "downstream facing port",
63 [TUSB320_ATTACHED_STATE_UFP] = "upstream facing port",
64 [TUSB320_ATTACHED_STATE_ACC] = "accessory",
67 static const unsigned int tusb320_extcon_cable[] = {
73 static int tusb320_check_signature(struct tusb320_priv *priv)
75 static const char sig[] = { '\0', 'T', 'U', 'S', 'B', '3', '2', '0' };
79 for (i = 0; i < sizeof(sig); i++) {
80 ret = regmap_read(priv->regmap, sizeof(sig) - 1 - i, &val);
84 dev_err(priv->dev, "signature mismatch!\n");
92 static int tusb320_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
96 /* Mode cannot be changed while cable is attached */
97 if (priv->state != TUSB320_ATTACHED_STATE_NONE)
101 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
102 TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT,
103 mode << TUSB320_REGA_MODE_SELECT_SHIFT);
105 dev_err(priv->dev, "failed to write mode: %d\n", ret);
112 static int tusb320l_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
116 /* Disable CC state machine */
117 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
118 TUSB320L_REGA_DISABLE_TERM, 1);
121 "failed to disable CC state machine: %d\n", ret);
126 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
127 TUSB320_REGA_MODE_SELECT_MASK << TUSB320_REGA_MODE_SELECT_SHIFT,
128 mode << TUSB320_REGA_MODE_SELECT_SHIFT);
130 dev_err(priv->dev, "failed to write mode: %d\n", ret);
136 /* Re-enable CC state machine */
137 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
138 TUSB320L_REGA_DISABLE_TERM, 0);
141 "failed to re-enable CC state machine: %d\n", ret);
146 static int tusb320_reset(struct tusb320_priv *priv)
150 /* Set mode to default (follow PORT pin) */
151 ret = priv->ops->set_mode(priv, TUSB320_MODE_PORT);
152 if (ret && ret != -EBUSY) {
154 "failed to set mode to PORT: %d\n", ret);
158 /* Perform soft reset */
159 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
160 TUSB320_REGA_I2C_SOFT_RESET, 1);
163 "failed to write soft reset bit: %d\n", ret);
167 /* Wait for chip to go through reset */
173 static int tusb320l_get_revision(struct tusb320_priv *priv, unsigned int *revision)
175 return regmap_read(priv->regmap, TUSB320L_REGA0_REVISION, revision);
178 static struct tusb320_ops tusb320_ops = {
179 .set_mode = tusb320_set_mode,
182 static struct tusb320_ops tusb320l_ops = {
183 .set_mode = tusb320l_set_mode,
184 .get_revision = tusb320l_get_revision,
187 static irqreturn_t tusb320_irq_handler(int irq, void *dev_id)
189 struct tusb320_priv *priv = dev_id;
193 if (regmap_read(priv->regmap, TUSB320_REG9, ®)) {
194 dev_err(priv->dev, "error during i2c read!\n");
198 if (!(reg & TUSB320_REG9_INTERRUPT_STATUS))
201 state = (reg >> TUSB320_REG9_ATTACHED_STATE_SHIFT) &
202 TUSB320_REG9_ATTACHED_STATE_MASK;
203 polarity = !!(reg & TUSB320_REG9_CABLE_DIRECTION);
205 dev_dbg(priv->dev, "attached state: %s, polarity: %d\n",
206 tusb_attached_states[state], polarity);
208 extcon_set_state(priv->edev, EXTCON_USB,
209 state == TUSB320_ATTACHED_STATE_UFP);
210 extcon_set_state(priv->edev, EXTCON_USB_HOST,
211 state == TUSB320_ATTACHED_STATE_DFP);
212 extcon_set_property(priv->edev, EXTCON_USB,
213 EXTCON_PROP_USB_TYPEC_POLARITY,
214 (union extcon_property_value)polarity);
215 extcon_set_property(priv->edev, EXTCON_USB_HOST,
216 EXTCON_PROP_USB_TYPEC_POLARITY,
217 (union extcon_property_value)polarity);
218 extcon_sync(priv->edev, EXTCON_USB);
219 extcon_sync(priv->edev, EXTCON_USB_HOST);
223 regmap_write(priv->regmap, TUSB320_REG9, reg);
228 static const struct regmap_config tusb320_regmap_config = {
233 static int tusb320_extcon_probe(struct i2c_client *client,
234 const struct i2c_device_id *id)
236 struct tusb320_priv *priv;
237 const void *match_data;
238 unsigned int revision;
241 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
244 priv->dev = &client->dev;
246 priv->regmap = devm_regmap_init_i2c(client, &tusb320_regmap_config);
247 if (IS_ERR(priv->regmap))
248 return PTR_ERR(priv->regmap);
250 ret = tusb320_check_signature(priv);
254 match_data = device_get_match_data(&client->dev);
258 priv->ops = (struct tusb320_ops*)match_data;
260 priv->edev = devm_extcon_dev_allocate(priv->dev, tusb320_extcon_cable);
261 if (IS_ERR(priv->edev)) {
262 dev_err(priv->dev, "failed to allocate extcon device\n");
263 return PTR_ERR(priv->edev);
266 if (priv->ops->get_revision) {
267 ret = priv->ops->get_revision(priv, &revision);
270 "failed to read revision register: %d\n", ret);
272 dev_info(priv->dev, "chip revision %d\n", revision);
275 ret = devm_extcon_dev_register(priv->dev, priv->edev);
277 dev_err(priv->dev, "failed to register extcon device\n");
281 extcon_set_property_capability(priv->edev, EXTCON_USB,
282 EXTCON_PROP_USB_TYPEC_POLARITY);
283 extcon_set_property_capability(priv->edev, EXTCON_USB_HOST,
284 EXTCON_PROP_USB_TYPEC_POLARITY);
286 /* update initial state */
287 tusb320_irq_handler(client->irq, priv);
289 /* Reset chip to its default state */
290 ret = tusb320_reset(priv);
292 dev_warn(priv->dev, "failed to reset chip: %d\n", ret);
295 * State and polarity might change after a reset, so update
296 * them again and make sure the interrupt status bit is cleared.
298 tusb320_irq_handler(client->irq, priv);
300 ret = devm_request_threaded_irq(priv->dev, client->irq, NULL,
302 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
308 static const struct of_device_id tusb320_extcon_dt_match[] = {
309 { .compatible = "ti,tusb320", .data = &tusb320_ops, },
310 { .compatible = "ti,tusb320l", .data = &tusb320l_ops, },
313 MODULE_DEVICE_TABLE(of, tusb320_extcon_dt_match);
315 static struct i2c_driver tusb320_extcon_driver = {
316 .probe = tusb320_extcon_probe,
318 .name = "extcon-tusb320",
319 .of_match_table = tusb320_extcon_dt_match,
323 static int __init tusb320_init(void)
325 return i2c_add_driver(&tusb320_extcon_driver);
327 subsys_initcall(tusb320_init);
329 static void __exit tusb320_exit(void)
331 i2c_del_driver(&tusb320_extcon_driver);
333 module_exit(tusb320_exit);
335 MODULE_AUTHOR("Michael Auchter <michael.auchter@ni.com>");
336 MODULE_DESCRIPTION("TI TUSB320 extcon driver");
337 MODULE_LICENSE("GPL v2");