Merge tag 's390-6.1-5' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[platform/kernel/linux-starfive.git] / drivers / extcon / extcon-usbc-tusb320.c
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
331         /*
332          * Type-C support is optional. Only call the Type-C handler if a
333          * port had been registered previously.
334          */
335         if (priv->port)
336                 tusb320_typec_irq_handler(priv, reg);
337
338         regmap_write(priv->regmap, TUSB320_REG9, reg);
339
340         return IRQ_HANDLED;
341 }
342
343 static const struct regmap_config tusb320_regmap_config = {
344         .reg_bits = 8,
345         .val_bits = 8,
346 };
347
348 static int tusb320_extcon_probe(struct tusb320_priv *priv)
349 {
350         int ret;
351
352         priv->edev = devm_extcon_dev_allocate(priv->dev, tusb320_extcon_cable);
353         if (IS_ERR(priv->edev)) {
354                 dev_err(priv->dev, "failed to allocate extcon device\n");
355                 return PTR_ERR(priv->edev);
356         }
357
358         ret = devm_extcon_dev_register(priv->dev, priv->edev);
359         if (ret < 0) {
360                 dev_err(priv->dev, "failed to register extcon device\n");
361                 return ret;
362         }
363
364         extcon_set_property_capability(priv->edev, EXTCON_USB,
365                                        EXTCON_PROP_USB_TYPEC_POLARITY);
366         extcon_set_property_capability(priv->edev, EXTCON_USB_HOST,
367                                        EXTCON_PROP_USB_TYPEC_POLARITY);
368
369         return 0;
370 }
371
372 static int tusb320_typec_probe(struct i2c_client *client,
373                                struct tusb320_priv *priv)
374 {
375         struct fwnode_handle *connector;
376         const char *cap_str;
377         int ret;
378
379         /* The Type-C connector is optional, for backward compatibility. */
380         connector = device_get_named_child_node(&client->dev, "connector");
381         if (!connector)
382                 return 0;
383
384         /* Type-C connector found. */
385         ret = typec_get_fw_cap(&priv->cap, connector);
386         if (ret)
387                 return ret;
388
389         priv->port_type = priv->cap.type;
390
391         /* This goes into register 0x8 field CURRENT_MODE_ADVERTISE */
392         ret = fwnode_property_read_string(connector, "typec-power-opmode", &cap_str);
393         if (ret)
394                 return ret;
395
396         ret = typec_find_pwr_opmode(cap_str);
397         if (ret < 0)
398                 return ret;
399         if (ret == TYPEC_PWR_MODE_PD)
400                 return -EINVAL;
401
402         priv->pwr_opmode = ret;
403
404         /* Initialize the hardware with the devicetree settings. */
405         ret = tusb320_set_adv_pwr_mode(priv);
406         if (ret)
407                 return ret;
408
409         priv->cap.revision              = USB_TYPEC_REV_1_1;
410         priv->cap.accessory[0]          = TYPEC_ACCESSORY_AUDIO;
411         priv->cap.accessory[1]          = TYPEC_ACCESSORY_DEBUG;
412         priv->cap.orientation_aware     = true;
413         priv->cap.driver_data           = priv;
414         priv->cap.ops                   = &tusb320_typec_ops;
415         priv->cap.fwnode                = connector;
416
417         priv->port = typec_register_port(&client->dev, &priv->cap);
418         if (IS_ERR(priv->port))
419                 return PTR_ERR(priv->port);
420
421         return 0;
422 }
423
424 static int tusb320_probe(struct i2c_client *client,
425                          const struct i2c_device_id *id)
426 {
427         struct tusb320_priv *priv;
428         const void *match_data;
429         unsigned int revision;
430         int ret;
431
432         priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
433         if (!priv)
434                 return -ENOMEM;
435         priv->dev = &client->dev;
436
437         priv->regmap = devm_regmap_init_i2c(client, &tusb320_regmap_config);
438         if (IS_ERR(priv->regmap))
439                 return PTR_ERR(priv->regmap);
440
441         ret = tusb320_check_signature(priv);
442         if (ret)
443                 return ret;
444
445         match_data = device_get_match_data(&client->dev);
446         if (!match_data)
447                 return -EINVAL;
448
449         priv->ops = (struct tusb320_ops*)match_data;
450
451         if (priv->ops->get_revision) {
452                 ret = priv->ops->get_revision(priv, &revision);
453                 if (ret)
454                         dev_warn(priv->dev,
455                                 "failed to read revision register: %d\n", ret);
456                 else
457                         dev_info(priv->dev, "chip revision %d\n", revision);
458         }
459
460         ret = tusb320_extcon_probe(priv);
461         if (ret)
462                 return ret;
463
464         ret = tusb320_typec_probe(client, priv);
465         if (ret)
466                 return ret;
467
468         /* update initial state */
469         tusb320_irq_handler(client->irq, priv);
470
471         /* Reset chip to its default state */
472         ret = tusb320_reset(priv);
473         if (ret)
474                 dev_warn(priv->dev, "failed to reset chip: %d\n", ret);
475         else
476                 /*
477                  * State and polarity might change after a reset, so update
478                  * them again and make sure the interrupt status bit is cleared.
479                  */
480                 tusb320_irq_handler(client->irq, priv);
481
482         ret = devm_request_threaded_irq(priv->dev, client->irq, NULL,
483                                         tusb320_irq_handler,
484                                         IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
485                                         client->name, priv);
486
487         return ret;
488 }
489
490 static const struct of_device_id tusb320_extcon_dt_match[] = {
491         { .compatible = "ti,tusb320", .data = &tusb320_ops, },
492         { .compatible = "ti,tusb320l", .data = &tusb320l_ops, },
493         { }
494 };
495 MODULE_DEVICE_TABLE(of, tusb320_extcon_dt_match);
496
497 static struct i2c_driver tusb320_extcon_driver = {
498         .probe          = tusb320_probe,
499         .driver         = {
500                 .name   = "extcon-tusb320",
501                 .of_match_table = tusb320_extcon_dt_match,
502         },
503 };
504
505 static int __init tusb320_init(void)
506 {
507         return i2c_add_driver(&tusb320_extcon_driver);
508 }
509 subsys_initcall(tusb320_init);
510
511 static void __exit tusb320_exit(void)
512 {
513         i2c_del_driver(&tusb320_extcon_driver);
514 }
515 module_exit(tusb320_exit);
516
517 MODULE_AUTHOR("Michael Auchter <michael.auchter@ni.com>");
518 MODULE_DESCRIPTION("TI TUSB320 extcon driver");
519 MODULE_LICENSE("GPL v2");