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 struct fwnode_handle *connector_fwnode;
82 };
83
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",
89 };
90
91 static const unsigned int tusb320_extcon_cable[] = {
92 EXTCON_USB,
93 EXTCON_USB_HOST,
94 EXTCON_NONE,
95 };
96
tusb320_check_signature(struct tusb320_priv * priv)97 static int tusb320_check_signature(struct tusb320_priv *priv)
98 {
99 static const char sig[] = { '\0', 'T', 'U', 'S', 'B', '3', '2', '0' };
100 unsigned val;
101 int i, ret;
102
103 for (i = 0; i < sizeof(sig); i++) {
104 ret = regmap_read(priv->regmap, sizeof(sig) - 1 - i, &val);
105 if (ret < 0)
106 return ret;
107 if (val != sig[i]) {
108 dev_err(priv->dev, "signature mismatch!\n");
109 return -ENODEV;
110 }
111 }
112
113 return 0;
114 }
115
tusb320_set_mode(struct tusb320_priv * priv,enum tusb320_mode mode)116 static int tusb320_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
117 {
118 int ret;
119
120 /* Mode cannot be changed while cable is attached */
121 if (priv->state != TUSB320_ATTACHED_STATE_NONE)
122 return -EBUSY;
123
124 /* Write mode */
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);
128 if (ret) {
129 dev_err(priv->dev, "failed to write mode: %d\n", ret);
130 return ret;
131 }
132
133 return 0;
134 }
135
tusb320l_set_mode(struct tusb320_priv * priv,enum tusb320_mode mode)136 static int tusb320l_set_mode(struct tusb320_priv *priv, enum tusb320_mode mode)
137 {
138 int ret;
139
140 /* Disable CC state machine */
141 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
142 TUSB320L_REGA_DISABLE_TERM, 1);
143 if (ret) {
144 dev_err(priv->dev,
145 "failed to disable CC state machine: %d\n", ret);
146 return ret;
147 }
148
149 /* Write mode */
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);
153 if (ret) {
154 dev_err(priv->dev, "failed to write mode: %d\n", ret);
155 goto err;
156 }
157
158 msleep(5);
159 err:
160 /* Re-enable CC state machine */
161 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
162 TUSB320L_REGA_DISABLE_TERM, 0);
163 if (ret)
164 dev_err(priv->dev,
165 "failed to re-enable CC state machine: %d\n", ret);
166
167 return ret;
168 }
169
tusb320_reset(struct tusb320_priv * priv)170 static int tusb320_reset(struct tusb320_priv *priv)
171 {
172 int ret;
173
174 /* Set mode to default (follow PORT pin) */
175 ret = priv->ops->set_mode(priv, TUSB320_MODE_PORT);
176 if (ret && ret != -EBUSY) {
177 dev_err(priv->dev,
178 "failed to set mode to PORT: %d\n", ret);
179 return ret;
180 }
181
182 /* Perform soft reset */
183 ret = regmap_write_bits(priv->regmap, TUSB320_REGA,
184 TUSB320_REGA_I2C_SOFT_RESET, 1);
185 if (ret) {
186 dev_err(priv->dev,
187 "failed to write soft reset bit: %d\n", ret);
188 return ret;
189 }
190
191 /* Wait for chip to go through reset */
192 msleep(95);
193
194 return 0;
195 }
196
tusb320l_get_revision(struct tusb320_priv * priv,unsigned int * revision)197 static int tusb320l_get_revision(struct tusb320_priv *priv, unsigned int *revision)
198 {
199 return regmap_read(priv->regmap, TUSB320L_REGA0_REVISION, revision);
200 }
201
202 static struct tusb320_ops tusb320_ops = {
203 .set_mode = tusb320_set_mode,
204 };
205
206 static struct tusb320_ops tusb320l_ops = {
207 .set_mode = tusb320l_set_mode,
208 .get_revision = tusb320l_get_revision,
209 };
210
tusb320_set_adv_pwr_mode(struct tusb320_priv * priv)211 static int tusb320_set_adv_pwr_mode(struct tusb320_priv *priv)
212 {
213 u8 mode;
214
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. */
222 return -EINVAL;
223
224 return regmap_write_bits(priv->regmap, TUSB320_REG8,
225 TUSB320_REG8_CURRENT_MODE_ADVERTISE,
226 FIELD_PREP(TUSB320_REG8_CURRENT_MODE_ADVERTISE,
227 mode));
228 }
229
tusb320_port_type_set(struct typec_port * port,enum typec_port_type type)230 static int tusb320_port_type_set(struct typec_port *port,
231 enum typec_port_type type)
232 {
233 struct tusb320_priv *priv = typec_get_drvdata(port);
234
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);
241 else
242 return priv->ops->set_mode(priv, TUSB320_MODE_PORT);
243 }
244
245 static const struct typec_operations tusb320_typec_ops = {
246 .port_type_set = tusb320_port_type_set,
247 };
248
tusb320_extcon_irq_handler(struct tusb320_priv * priv,u8 reg)249 static void tusb320_extcon_irq_handler(struct tusb320_priv *priv, u8 reg)
250 {
251 int state, polarity;
252
253 state = (reg >> TUSB320_REG9_ATTACHED_STATE_SHIFT) &
254 TUSB320_REG9_ATTACHED_STATE_MASK;
255 polarity = !!(reg & TUSB320_REG9_CABLE_DIRECTION);
256
257 dev_dbg(priv->dev, "attached state: %s, polarity: %d\n",
258 tusb_attached_states[state], polarity);
259
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);
272
273 priv->state = state;
274 }
275
tusb320_typec_irq_handler(struct tusb320_priv * priv,u8 reg9)276 static void tusb320_typec_irq_handler(struct tusb320_priv *priv, u8 reg9)
277 {
278 struct typec_port *port = priv->port;
279 struct device *dev = priv->dev;
280 u8 mode, role, state;
281 int ret, reg8;
282 bool ori;
283
284 ori = reg9 & TUSB320_REG9_CABLE_DIRECTION;
285 typec_set_orientation(port, ori ? TYPEC_ORIENTATION_REVERSE :
286 TYPEC_ORIENTATION_NORMAL);
287
288 state = (reg9 >> TUSB320_REG9_ATTACHED_STATE_SHIFT) &
289 TUSB320_REG9_ATTACHED_STATE_MASK;
290 if (state == TUSB320_ATTACHED_STATE_DFP)
291 role = TYPEC_SOURCE;
292 else
293 role = TYPEC_SINK;
294
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);
299
300 ret = regmap_read(priv->regmap, TUSB320_REG8, ®8);
301 if (ret) {
302 dev_err(dev, "error during reg8 i2c read, ret=%d!\n", ret);
303 return;
304 }
305
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);
315 }
316
tusb320_state_update_handler(struct tusb320_priv * priv,bool force_update)317 static irqreturn_t tusb320_state_update_handler(struct tusb320_priv *priv,
318 bool force_update)
319 {
320 unsigned int reg;
321
322 if (regmap_read(priv->regmap, TUSB320_REG9, ®)) {
323 dev_err(priv->dev, "error during i2c read!\n");
324 return IRQ_NONE;
325 }
326
327 if (!force_update && !(reg & TUSB320_REG9_INTERRUPT_STATUS))
328 return IRQ_NONE;
329
330 tusb320_extcon_irq_handler(priv, reg);
331
332 /*
333 * Type-C support is optional. Only call the Type-C handler if a
334 * port had been registered previously.
335 */
336 if (priv->port)
337 tusb320_typec_irq_handler(priv, reg);
338
339 regmap_write(priv->regmap, TUSB320_REG9, reg);
340
341 return IRQ_HANDLED;
342 }
343
tusb320_irq_handler(int irq,void * dev_id)344 static irqreturn_t tusb320_irq_handler(int irq, void *dev_id)
345 {
346 struct tusb320_priv *priv = dev_id;
347
348 return tusb320_state_update_handler(priv, false);
349 }
350
351 static const struct regmap_config tusb320_regmap_config = {
352 .reg_bits = 8,
353 .val_bits = 8,
354 };
355
tusb320_extcon_probe(struct tusb320_priv * priv)356 static int tusb320_extcon_probe(struct tusb320_priv *priv)
357 {
358 int ret;
359
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);
364 }
365
366 ret = devm_extcon_dev_register(priv->dev, priv->edev);
367 if (ret < 0) {
368 dev_err(priv->dev, "failed to register extcon device\n");
369 return ret;
370 }
371
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);
376
377 return 0;
378 }
379
tusb320_typec_probe(struct i2c_client * client,struct tusb320_priv * priv)380 static int tusb320_typec_probe(struct i2c_client *client,
381 struct tusb320_priv *priv)
382 {
383 struct fwnode_handle *connector;
384 const char *cap_str;
385 int ret;
386
387 /* The Type-C connector is optional, for backward compatibility. */
388 connector = device_get_named_child_node(&client->dev, "connector");
389 if (!connector)
390 return 0;
391
392 /* Type-C connector found. */
393 ret = typec_get_fw_cap(&priv->cap, connector);
394 if (ret)
395 goto err_put;
396
397 priv->port_type = priv->cap.type;
398
399 /* This goes into register 0x8 field CURRENT_MODE_ADVERTISE */
400 ret = fwnode_property_read_string(connector, "typec-power-opmode", &cap_str);
401 if (ret)
402 goto err_put;
403
404 ret = typec_find_pwr_opmode(cap_str);
405 if (ret < 0)
406 goto err_put;
407
408 priv->pwr_opmode = ret;
409
410 /* Initialize the hardware with the devicetree settings. */
411 ret = tusb320_set_adv_pwr_mode(priv);
412 if (ret)
413 goto err_put;
414
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;
422
423 priv->port = typec_register_port(&client->dev, &priv->cap);
424 if (IS_ERR(priv->port)) {
425 ret = PTR_ERR(priv->port);
426 goto err_put;
427 }
428
429 priv->connector_fwnode = connector;
430
431 return 0;
432
433 err_put:
434 fwnode_handle_put(connector);
435
436 return ret;
437 }
438
tusb320_typec_remove(struct tusb320_priv * priv)439 static void tusb320_typec_remove(struct tusb320_priv *priv)
440 {
441 typec_unregister_port(priv->port);
442 fwnode_handle_put(priv->connector_fwnode);
443 }
444
tusb320_probe(struct i2c_client * client)445 static int tusb320_probe(struct i2c_client *client)
446 {
447 struct tusb320_priv *priv;
448 const void *match_data;
449 unsigned int revision;
450 int ret;
451
452 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
453 if (!priv)
454 return -ENOMEM;
455
456 priv->dev = &client->dev;
457 i2c_set_clientdata(client, priv);
458
459 priv->regmap = devm_regmap_init_i2c(client, &tusb320_regmap_config);
460 if (IS_ERR(priv->regmap))
461 return PTR_ERR(priv->regmap);
462
463 ret = tusb320_check_signature(priv);
464 if (ret)
465 return ret;
466
467 match_data = device_get_match_data(&client->dev);
468 if (!match_data)
469 return -EINVAL;
470
471 priv->ops = (struct tusb320_ops*)match_data;
472
473 if (priv->ops->get_revision) {
474 ret = priv->ops->get_revision(priv, &revision);
475 if (ret)
476 dev_warn(priv->dev,
477 "failed to read revision register: %d\n", ret);
478 else
479 dev_info(priv->dev, "chip revision %d\n", revision);
480 }
481
482 ret = tusb320_extcon_probe(priv);
483 if (ret)
484 return ret;
485
486 ret = tusb320_typec_probe(client, priv);
487 if (ret)
488 return ret;
489
490 /* update initial state */
491 tusb320_state_update_handler(priv, true);
492
493 /* Reset chip to its default state */
494 ret = tusb320_reset(priv);
495 if (ret)
496 dev_warn(priv->dev, "failed to reset chip: %d\n", ret);
497 else
498 /*
499 * State and polarity might change after a reset, so update
500 * them again and make sure the interrupt status bit is cleared.
501 */
502 tusb320_state_update_handler(priv, true);
503
504 ret = devm_request_threaded_irq(priv->dev, client->irq, NULL,
505 tusb320_irq_handler,
506 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
507 client->name, priv);
508 if (ret)
509 tusb320_typec_remove(priv);
510
511 return ret;
512 }
513
tusb320_remove(struct i2c_client * client)514 static void tusb320_remove(struct i2c_client *client)
515 {
516 struct tusb320_priv *priv = i2c_get_clientdata(client);
517
518 tusb320_typec_remove(priv);
519 }
520
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, },
524 { }
525 };
526 MODULE_DEVICE_TABLE(of, tusb320_extcon_dt_match);
527
528 static struct i2c_driver tusb320_extcon_driver = {
529 .probe_new = tusb320_probe,
530 .remove = tusb320_remove,
531 .driver = {
532 .name = "extcon-tusb320",
533 .of_match_table = tusb320_extcon_dt_match,
534 },
535 };
536
tusb320_init(void)537 static int __init tusb320_init(void)
538 {
539 return i2c_add_driver(&tusb320_extcon_driver);
540 }
541 subsys_initcall(tusb320_init);
542
tusb320_exit(void)543 static void __exit tusb320_exit(void)
544 {
545 i2c_del_driver(&tusb320_extcon_driver);
546 }
547 module_exit(tusb320_exit);
548
549 MODULE_AUTHOR("Michael Auchter <michael.auchter@ni.com>");
550 MODULE_DESCRIPTION("TI TUSB320 extcon driver");
551 MODULE_LICENSE("GPL v2");
552