1
2
3
4
5
6
7
8
9#include <linux/bitfield.h>
10#include <linux/i2c.h>
11#include <linux/interrupt.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/regmap.h>
15#include <linux/regulator/consumer.h>
16#include <linux/usb/role.h>
17#include <linux/usb/typec.h>
18
19#define STUSB160X_ALERT_STATUS 0x0B
20#define STUSB160X_ALERT_STATUS_MASK_CTRL 0x0C
21#define STUSB160X_CC_CONNECTION_STATUS_TRANS 0x0D
22#define STUSB160X_CC_CONNECTION_STATUS 0x0E
23#define STUSB160X_MONITORING_STATUS_TRANS 0x0F
24#define STUSB160X_MONITORING_STATUS 0x10
25#define STUSB160X_CC_OPERATION_STATUS 0x11
26#define STUSB160X_HW_FAULT_STATUS_TRANS 0x12
27#define STUSB160X_HW_FAULT_STATUS 0x13
28#define STUSB160X_CC_CAPABILITY_CTRL 0x18
29#define STUSB160X_CC_VCONN_SWITCH_CTRL 0x1E
30#define STUSB160X_VCONN_MONITORING_CTRL 0x20
31#define STUSB160X_VBUS_MONITORING_RANGE_CTRL 0x22
32#define STUSB160X_RESET_CTRL 0x23
33#define STUSB160X_VBUS_DISCHARGE_TIME_CTRL 0x25
34#define STUSB160X_VBUS_DISCHARGE_STATUS 0x26
35#define STUSB160X_VBUS_ENABLE_STATUS 0x27
36#define STUSB160X_CC_POWER_MODE_CTRL 0x28
37#define STUSB160X_VBUS_MONITORING_CTRL 0x2E
38#define STUSB1600_REG_MAX 0x2F
39
40
41#define STUSB160X_HW_FAULT BIT(4)
42#define STUSB160X_MONITORING BIT(5)
43#define STUSB160X_CC_CONNECTION BIT(6)
44#define STUSB160X_ALL_ALERTS GENMASK(6, 4)
45
46
47#define STUSB160X_CC_ATTACH_TRANS BIT(0)
48
49
50#define STUSB160X_CC_ATTACH BIT(0)
51#define STUSB160X_CC_VCONN_SUPPLY BIT(1)
52#define STUSB160X_CC_DATA_ROLE(s) (!!((s) & BIT(2)))
53#define STUSB160X_CC_POWER_ROLE(s) (!!((s) & BIT(3)))
54#define STUSB160X_CC_ATTACHED_MODE GENMASK(7, 5)
55
56
57#define STUSB160X_VCONN_PRESENCE_TRANS BIT(0)
58#define STUSB160X_VBUS_PRESENCE_TRANS BIT(1)
59#define STUSB160X_VBUS_VSAFE0V_TRANS BIT(2)
60#define STUSB160X_VBUS_VALID_TRANS BIT(3)
61
62
63#define STUSB160X_VCONN_PRESENCE BIT(0)
64#define STUSB160X_VBUS_PRESENCE BIT(1)
65#define STUSB160X_VBUS_VSAFE0V BIT(2)
66#define STUSB160X_VBUS_VALID BIT(3)
67
68
69#define STUSB160X_TYPEC_FSM_STATE GENMASK(4, 0)
70#define STUSB160X_SINK_POWER_STATE GENMASK(6, 5)
71#define STUSB160X_CC_ATTACHED BIT(7)
72
73
74#define STUSB160X_VCONN_SW_OVP_FAULT_TRANS BIT(0)
75#define STUSB160X_VCONN_SW_OCP_FAULT_TRANS BIT(1)
76#define STUSB160X_VCONN_SW_RVP_FAULT_TRANS BIT(2)
77#define STUSB160X_VPU_VALID_TRANS BIT(4)
78#define STUSB160X_VPU_OVP_FAULT_TRANS BIT(5)
79#define STUSB160X_THERMAL_FAULT BIT(7)
80
81
82#define STUSB160X_VCONN_SW_OVP_FAULT_CC2 BIT(0)
83#define STUSB160X_VCONN_SW_OVP_FAULT_CC1 BIT(1)
84#define STUSB160X_VCONN_SW_OCP_FAULT_CC2 BIT(2)
85#define STUSB160X_VCONN_SW_OCP_FAULT_CC1 BIT(3)
86#define STUSB160X_VCONN_SW_RVP_FAULT_CC2 BIT(4)
87#define STUSB160X_VCONN_SW_RVP_FAULT_CC1 BIT(5)
88#define STUSB160X_VPU_VALID BIT(6)
89#define STUSB160X_VPU_OVP_FAULT BIT(7)
90
91
92#define STUSB160X_CC_VCONN_SUPPLY_EN BIT(0)
93#define STUSB160X_CC_VCONN_DISCHARGE_EN BIT(4)
94#define STUSB160X_CC_CURRENT_ADVERTISED GENMASK(7, 6)
95
96
97#define STUSB160X_CC_VCONN_SWITCH_ILIM GENMASK(3, 0)
98
99
100#define STUSB160X_VCONN_UVLO_THRESHOLD BIT(6)
101#define STUSB160X_VCONN_MONITORING_EN BIT(7)
102
103
104#define STUSB160X_SHIFT_LOW_VBUS_LIMIT GENMASK(3, 0)
105#define STUSB160X_SHIFT_HIGH_VBUS_LIMIT GENMASK(7, 4)
106
107
108#define STUSB160X_SW_RESET_EN BIT(0)
109
110
111#define STUSBXX02_VBUS_DISCHARGE_TIME_TO_PDO GENMASK(3, 0)
112#define STUSB160X_VBUS_DISCHARGE_TIME_TO_0V GENMASK(7, 4)
113
114
115#define STUSB160X_VBUS_DISCHARGE_EN BIT(7)
116
117
118#define STUSB160X_VBUS_SOURCE_EN BIT(0)
119#define STUSB160X_VBUS_SINK_EN BIT(1)
120
121
122#define STUSB160X_CC_POWER_MODE GENMASK(2, 0)
123
124
125#define STUSB160X_VDD_UVLO_DISABLE BIT(0)
126#define STUSB160X_VBUS_VSAFE0V_THRESHOLD GENMASK(2, 1)
127#define STUSB160X_VBUS_RANGE_DISABLE BIT(4)
128#define STUSB160X_VDD_OVLO_DISABLE BIT(6)
129
130enum stusb160x_pwr_mode {
131 SOURCE_WITH_ACCESSORY,
132 SINK_WITH_ACCESSORY,
133 SINK_WITHOUT_ACCESSORY,
134 DUAL_WITH_ACCESSORY,
135 DUAL_WITH_ACCESSORY_AND_TRY_SRC,
136 DUAL_WITH_ACCESSORY_AND_TRY_SNK,
137};
138
139enum stusb160x_attached_mode {
140 NO_DEVICE_ATTACHED,
141 SINK_ATTACHED,
142 SOURCE_ATTACHED,
143 DEBUG_ACCESSORY_ATTACHED,
144 AUDIO_ACCESSORY_ATTACHED,
145};
146
147struct stusb160x {
148 struct device *dev;
149 struct regmap *regmap;
150 struct regulator *vdd_supply;
151 struct regulator *vsys_supply;
152 struct regulator *vconn_supply;
153 struct regulator *main_supply;
154
155 struct typec_port *port;
156 struct typec_capability capability;
157 struct typec_partner *partner;
158
159 enum typec_port_type port_type;
160 enum typec_pwr_opmode pwr_opmode;
161 bool vbus_on;
162
163 struct usb_role_switch *role_sw;
164};
165
166static bool stusb160x_reg_writeable(struct device *dev, unsigned int reg)
167{
168 switch (reg) {
169 case STUSB160X_ALERT_STATUS_MASK_CTRL:
170 case STUSB160X_CC_CAPABILITY_CTRL:
171 case STUSB160X_CC_VCONN_SWITCH_CTRL:
172 case STUSB160X_VCONN_MONITORING_CTRL:
173 case STUSB160X_VBUS_MONITORING_RANGE_CTRL:
174 case STUSB160X_RESET_CTRL:
175 case STUSB160X_VBUS_DISCHARGE_TIME_CTRL:
176 case STUSB160X_CC_POWER_MODE_CTRL:
177 case STUSB160X_VBUS_MONITORING_CTRL:
178 return true;
179 default:
180 return false;
181 }
182}
183
184static bool stusb160x_reg_readable(struct device *dev, unsigned int reg)
185{
186 if (reg <= 0x0A ||
187 (reg >= 0x14 && reg <= 0x17) ||
188 (reg >= 0x19 && reg <= 0x1D) ||
189 (reg >= 0x29 && reg <= 0x2D) ||
190 (reg == 0x1F || reg == 0x21 || reg == 0x24 || reg == 0x2F))
191 return false;
192 else
193 return true;
194}
195
196static bool stusb160x_reg_volatile(struct device *dev, unsigned int reg)
197{
198 switch (reg) {
199 case STUSB160X_ALERT_STATUS:
200 case STUSB160X_CC_CONNECTION_STATUS_TRANS:
201 case STUSB160X_CC_CONNECTION_STATUS:
202 case STUSB160X_MONITORING_STATUS_TRANS:
203 case STUSB160X_MONITORING_STATUS:
204 case STUSB160X_CC_OPERATION_STATUS:
205 case STUSB160X_HW_FAULT_STATUS_TRANS:
206 case STUSB160X_HW_FAULT_STATUS:
207 case STUSB160X_VBUS_DISCHARGE_STATUS:
208 case STUSB160X_VBUS_ENABLE_STATUS:
209 return true;
210 default:
211 return false;
212 }
213}
214
215static bool stusb160x_reg_precious(struct device *dev, unsigned int reg)
216{
217 switch (reg) {
218 case STUSB160X_ALERT_STATUS:
219 case STUSB160X_CC_CONNECTION_STATUS_TRANS:
220 case STUSB160X_MONITORING_STATUS_TRANS:
221 case STUSB160X_HW_FAULT_STATUS_TRANS:
222 return true;
223 default:
224 return false;
225 }
226}
227
228static const struct regmap_config stusb1600_regmap_config = {
229 .reg_bits = 8,
230 .reg_stride = 1,
231 .val_bits = 8,
232 .max_register = STUSB1600_REG_MAX,
233 .writeable_reg = stusb160x_reg_writeable,
234 .readable_reg = stusb160x_reg_readable,
235 .volatile_reg = stusb160x_reg_volatile,
236 .precious_reg = stusb160x_reg_precious,
237 .cache_type = REGCACHE_RBTREE,
238};
239
240static bool stusb160x_get_vconn(struct stusb160x *chip)
241{
242 u32 val;
243 int ret;
244
245 ret = regmap_read(chip->regmap, STUSB160X_CC_CAPABILITY_CTRL, &val);
246 if (ret) {
247 dev_err(chip->dev, "Unable to get Vconn status: %d\n", ret);
248 return false;
249 }
250
251 return !!FIELD_GET(STUSB160X_CC_VCONN_SUPPLY_EN, val);
252}
253
254static int stusb160x_set_vconn(struct stusb160x *chip, bool on)
255{
256 int ret;
257
258
259 if (chip->vconn_supply) {
260 if (on) {
261 ret = regulator_enable(chip->vconn_supply);
262 if (ret) {
263 dev_err(chip->dev,
264 "failed to enable vconn supply: %d\n",
265 ret);
266 return ret;
267 }
268 } else {
269 regulator_disable(chip->vconn_supply);
270 }
271 }
272
273
274 ret = regmap_update_bits(chip->regmap, STUSB160X_VCONN_MONITORING_CTRL,
275 STUSB160X_VCONN_MONITORING_EN,
276 on ? STUSB160X_VCONN_MONITORING_EN : 0);
277 if (ret)
278 goto vconn_reg_disable;
279
280 return 0;
281
282vconn_reg_disable:
283 if (chip->vconn_supply && on)
284 regulator_disable(chip->vconn_supply);
285
286 return ret;
287}
288
289static enum typec_pwr_opmode stusb160x_get_pwr_opmode(struct stusb160x *chip)
290{
291 u32 val;
292 int ret;
293
294 ret = regmap_read(chip->regmap, STUSB160X_CC_CAPABILITY_CTRL, &val);
295 if (ret) {
296 dev_err(chip->dev, "Unable to get pwr opmode: %d\n", ret);
297 return TYPEC_PWR_MODE_USB;
298 }
299
300 return FIELD_GET(STUSB160X_CC_CURRENT_ADVERTISED, val);
301}
302
303static enum typec_accessory stusb160x_get_accessory(u32 status)
304{
305 enum stusb160x_attached_mode mode;
306
307 mode = FIELD_GET(STUSB160X_CC_ATTACHED_MODE, status);
308
309 switch (mode) {
310 case DEBUG_ACCESSORY_ATTACHED:
311 return TYPEC_ACCESSORY_DEBUG;
312 case AUDIO_ACCESSORY_ATTACHED:
313 return TYPEC_ACCESSORY_AUDIO;
314 default:
315 return TYPEC_ACCESSORY_NONE;
316 }
317}
318
319static enum typec_role stusb160x_get_vconn_role(u32 status)
320{
321 if (FIELD_GET(STUSB160X_CC_VCONN_SUPPLY, status))
322 return TYPEC_SOURCE;
323
324 return TYPEC_SINK;
325}
326
327static void stusb160x_set_data_role(struct stusb160x *chip,
328 enum typec_data_role data_role,
329 bool attached)
330{
331 enum usb_role usb_role = USB_ROLE_NONE;
332
333 if (attached) {
334 if (data_role == TYPEC_HOST)
335 usb_role = USB_ROLE_HOST;
336 else
337 usb_role = USB_ROLE_DEVICE;
338 }
339
340 usb_role_switch_set_role(chip->role_sw, usb_role);
341 typec_set_data_role(chip->port, data_role);
342}
343
344static int stusb160x_attach(struct stusb160x *chip, u32 status)
345{
346 struct typec_partner_desc desc;
347 int ret;
348
349 if ((STUSB160X_CC_POWER_ROLE(status) == TYPEC_SOURCE) &&
350 chip->vdd_supply) {
351 ret = regulator_enable(chip->vdd_supply);
352 if (ret) {
353 dev_err(chip->dev,
354 "Failed to enable Vbus supply: %d\n", ret);
355 return ret;
356 }
357 chip->vbus_on = true;
358 }
359
360 desc.usb_pd = false;
361 desc.accessory = stusb160x_get_accessory(status);
362 desc.identity = NULL;
363
364 chip->partner = typec_register_partner(chip->port, &desc);
365 if (IS_ERR(chip->partner)) {
366 ret = PTR_ERR(chip->partner);
367 goto vbus_disable;
368 }
369
370 typec_set_pwr_role(chip->port, STUSB160X_CC_POWER_ROLE(status));
371 typec_set_pwr_opmode(chip->port, stusb160x_get_pwr_opmode(chip));
372 typec_set_vconn_role(chip->port, stusb160x_get_vconn_role(status));
373 stusb160x_set_data_role(chip, STUSB160X_CC_DATA_ROLE(status), true);
374
375 return 0;
376
377vbus_disable:
378 if (chip->vbus_on) {
379 regulator_disable(chip->vdd_supply);
380 chip->vbus_on = false;
381 }
382
383 return ret;
384}
385
386static void stusb160x_detach(struct stusb160x *chip, u32 status)
387{
388 typec_unregister_partner(chip->partner);
389 chip->partner = NULL;
390
391 typec_set_pwr_role(chip->port, STUSB160X_CC_POWER_ROLE(status));
392 typec_set_pwr_opmode(chip->port, TYPEC_PWR_MODE_USB);
393 typec_set_vconn_role(chip->port, stusb160x_get_vconn_role(status));
394 stusb160x_set_data_role(chip, STUSB160X_CC_DATA_ROLE(status), false);
395
396 if (chip->vbus_on) {
397 regulator_disable(chip->vdd_supply);
398 chip->vbus_on = false;
399 }
400}
401
402static irqreturn_t stusb160x_irq_handler(int irq, void *data)
403{
404 struct stusb160x *chip = data;
405 u32 pending, trans, status;
406 int ret;
407
408 ret = regmap_read(chip->regmap, STUSB160X_ALERT_STATUS, &pending);
409 if (ret)
410 goto err;
411
412 if (pending & STUSB160X_CC_CONNECTION) {
413 ret = regmap_read(chip->regmap,
414 STUSB160X_CC_CONNECTION_STATUS_TRANS, &trans);
415 if (ret)
416 goto err;
417 ret = regmap_read(chip->regmap,
418 STUSB160X_CC_CONNECTION_STATUS, &status);
419 if (ret)
420 goto err;
421
422 if (trans & STUSB160X_CC_ATTACH_TRANS) {
423 if (status & STUSB160X_CC_ATTACH) {
424 ret = stusb160x_attach(chip, status);
425 if (ret)
426 goto err;
427 } else {
428 stusb160x_detach(chip, status);
429 }
430 }
431 }
432err:
433 return IRQ_HANDLED;
434}
435
436static int stusb160x_irq_init(struct stusb160x *chip, int irq)
437{
438 u32 status;
439 int ret;
440
441 ret = regmap_read(chip->regmap,
442 STUSB160X_CC_CONNECTION_STATUS, &status);
443 if (ret)
444 return ret;
445
446 if (status & STUSB160X_CC_ATTACH) {
447 ret = stusb160x_attach(chip, status);
448 if (ret)
449 dev_err(chip->dev, "attach failed: %d\n", ret);
450 }
451
452 ret = devm_request_threaded_irq(chip->dev, irq, NULL,
453 stusb160x_irq_handler, IRQF_ONESHOT,
454 dev_name(chip->dev), chip);
455 if (ret)
456 goto partner_unregister;
457
458
459 ret = regmap_write_bits(chip->regmap, STUSB160X_ALERT_STATUS_MASK_CTRL,
460 STUSB160X_CC_CONNECTION, 0);
461 if (ret)
462 goto partner_unregister;
463
464 return 0;
465
466partner_unregister:
467 if (chip->partner) {
468 typec_unregister_partner(chip->partner);
469 chip->partner = NULL;
470 }
471
472 return ret;
473}
474
475static int stusb160x_chip_init(struct stusb160x *chip)
476{
477 u32 val;
478 int ret;
479
480
481 if (chip->port_type == TYPEC_PORT_SRC)
482 ret = regmap_update_bits(chip->regmap,
483 STUSB160X_CC_POWER_MODE_CTRL,
484 STUSB160X_CC_POWER_MODE,
485 SOURCE_WITH_ACCESSORY);
486 else if (chip->port_type == TYPEC_PORT_SNK)
487 ret = regmap_update_bits(chip->regmap,
488 STUSB160X_CC_POWER_MODE_CTRL,
489 STUSB160X_CC_POWER_MODE,
490 SINK_WITH_ACCESSORY);
491 else
492 ret = regmap_update_bits(chip->regmap,
493 STUSB160X_CC_POWER_MODE_CTRL,
494 STUSB160X_CC_POWER_MODE,
495 DUAL_WITH_ACCESSORY);
496 if (ret)
497 return ret;
498
499 if (chip->port_type == TYPEC_PORT_SNK)
500 goto skip_src;
501
502
503 ret = regmap_update_bits(chip->regmap, STUSB160X_CC_CAPABILITY_CTRL,
504 STUSB160X_CC_CURRENT_ADVERTISED,
505 FIELD_PREP(STUSB160X_CC_CURRENT_ADVERTISED,
506 chip->pwr_opmode));
507 if (ret)
508 return ret;
509
510
511 if (stusb160x_get_vconn(chip)) {
512 ret = stusb160x_set_vconn(chip, true);
513 if (ret)
514 return ret;
515 }
516
517skip_src:
518
519 ret = regmap_update_bits(chip->regmap, STUSB160X_ALERT_STATUS_MASK_CTRL,
520 STUSB160X_ALL_ALERTS, STUSB160X_ALL_ALERTS);
521 if (ret)
522 return ret;
523
524
525 regmap_read(chip->regmap, STUSB160X_ALERT_STATUS, &val);
526 regmap_read(chip->regmap, STUSB160X_CC_CONNECTION_STATUS_TRANS, &val);
527 regmap_read(chip->regmap, STUSB160X_MONITORING_STATUS_TRANS, &val);
528 regmap_read(chip->regmap, STUSB160X_HW_FAULT_STATUS_TRANS, &val);
529
530 return 0;
531}
532
533static int stusb160x_get_fw_caps(struct stusb160x *chip,
534 struct fwnode_handle *fwnode)
535{
536 const char *cap_str;
537 int ret;
538
539 chip->capability.fwnode = fwnode;
540
541
542
543
544
545 ret = fwnode_property_read_string(fwnode, "power-role", &cap_str);
546 if (!ret) {
547 ret = typec_find_port_power_role(cap_str);
548 if (ret < 0)
549 return ret;
550 chip->port_type = ret;
551 }
552 chip->capability.type = chip->port_type;
553
554
555 if (chip->port_type == TYPEC_PORT_SNK)
556 return 0;
557
558 if (chip->port_type == TYPEC_PORT_DRP)
559 chip->capability.prefer_role = TYPEC_SINK;
560
561
562
563
564
565 ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &cap_str);
566 if (!ret) {
567 ret = typec_find_pwr_opmode(cap_str);
568
569 if (ret < 0 || ret == TYPEC_PWR_MODE_PD) {
570 dev_err(chip->dev, "bad power operation mode: %d\n", ret);
571 return -EINVAL;
572 }
573 chip->pwr_opmode = ret;
574 }
575
576 return 0;
577}
578
579static int stusb160x_get_caps(struct stusb160x *chip)
580{
581 enum typec_port_type *type = &chip->capability.type;
582 enum typec_port_data *data = &chip->capability.data;
583 enum typec_accessory *accessory = chip->capability.accessory;
584 u32 val;
585 int ret;
586
587 chip->capability.revision = USB_TYPEC_REV_1_2;
588
589 ret = regmap_read(chip->regmap, STUSB160X_CC_POWER_MODE_CTRL, &val);
590 if (ret)
591 return ret;
592
593 switch (FIELD_GET(STUSB160X_CC_POWER_MODE, val)) {
594 case SOURCE_WITH_ACCESSORY:
595 *type = TYPEC_PORT_SRC;
596 *data = TYPEC_PORT_DFP;
597 *accessory++ = TYPEC_ACCESSORY_AUDIO;
598 *accessory++ = TYPEC_ACCESSORY_DEBUG;
599 break;
600 case SINK_WITH_ACCESSORY:
601 *type = TYPEC_PORT_SNK;
602 *data = TYPEC_PORT_UFP;
603 *accessory++ = TYPEC_ACCESSORY_AUDIO;
604 *accessory++ = TYPEC_ACCESSORY_DEBUG;
605 break;
606 case SINK_WITHOUT_ACCESSORY:
607 *type = TYPEC_PORT_SNK;
608 *data = TYPEC_PORT_UFP;
609 break;
610 case DUAL_WITH_ACCESSORY:
611 case DUAL_WITH_ACCESSORY_AND_TRY_SRC:
612 case DUAL_WITH_ACCESSORY_AND_TRY_SNK:
613 *type = TYPEC_PORT_DRP;
614 *data = TYPEC_PORT_DRD;
615 *accessory++ = TYPEC_ACCESSORY_AUDIO;
616 *accessory++ = TYPEC_ACCESSORY_DEBUG;
617 break;
618 default:
619 return -EINVAL;
620 }
621
622 chip->port_type = *type;
623 chip->pwr_opmode = stusb160x_get_pwr_opmode(chip);
624
625 return 0;
626}
627
628static const struct of_device_id stusb160x_of_match[] = {
629 { .compatible = "st,stusb1600", .data = &stusb1600_regmap_config},
630 {},
631};
632MODULE_DEVICE_TABLE(of, stusb160x_of_match);
633
634static int stusb160x_probe(struct i2c_client *client)
635{
636 struct stusb160x *chip;
637 const struct of_device_id *match;
638 struct regmap_config *regmap_config;
639 struct fwnode_handle *fwnode;
640 int ret;
641
642 chip = devm_kzalloc(&client->dev, sizeof(struct stusb160x), GFP_KERNEL);
643 if (!chip)
644 return -ENOMEM;
645
646 i2c_set_clientdata(client, chip);
647
648 match = i2c_of_match_device(stusb160x_of_match, client);
649 regmap_config = (struct regmap_config *)match->data;
650 chip->regmap = devm_regmap_init_i2c(client, regmap_config);
651 if (IS_ERR(chip->regmap)) {
652 ret = PTR_ERR(chip->regmap);
653 dev_err(&client->dev,
654 "Failed to allocate register map:%d\n", ret);
655 return ret;
656 }
657
658 chip->dev = &client->dev;
659
660 chip->vsys_supply = devm_regulator_get_optional(chip->dev, "vsys");
661 if (IS_ERR(chip->vsys_supply)) {
662 ret = PTR_ERR(chip->vsys_supply);
663 if (ret != -ENODEV)
664 return ret;
665 chip->vsys_supply = NULL;
666 }
667
668 chip->vdd_supply = devm_regulator_get_optional(chip->dev, "vdd");
669 if (IS_ERR(chip->vdd_supply)) {
670 ret = PTR_ERR(chip->vdd_supply);
671 if (ret != -ENODEV)
672 return ret;
673 chip->vdd_supply = NULL;
674 }
675
676 chip->vconn_supply = devm_regulator_get_optional(chip->dev, "vconn");
677 if (IS_ERR(chip->vconn_supply)) {
678 ret = PTR_ERR(chip->vconn_supply);
679 if (ret != -ENODEV)
680 return ret;
681 chip->vconn_supply = NULL;
682 }
683
684 fwnode = device_get_named_child_node(chip->dev, "connector");
685 if (!fwnode)
686 return -ENODEV;
687
688
689
690
691
692
693
694
695 fw_devlink_purge_absent_suppliers(fwnode);
696
697
698
699
700
701
702 if (chip->vdd_supply &&
703 (!chip->vsys_supply ||
704 (regulator_get_voltage(chip->vsys_supply) <= 3100000)))
705 chip->main_supply = chip->vdd_supply;
706 else
707 chip->main_supply = chip->vsys_supply;
708
709 if (chip->main_supply) {
710 ret = regulator_enable(chip->main_supply);
711 if (ret) {
712 dev_err(chip->dev,
713 "Failed to enable main supply: %d\n", ret);
714 goto fwnode_put;
715 }
716 }
717
718
719 ret = stusb160x_get_caps(chip);
720 if (ret) {
721 dev_err(chip->dev, "Failed to get port caps: %d\n", ret);
722 goto main_reg_disable;
723 }
724
725
726 ret = stusb160x_get_fw_caps(chip, fwnode);
727 if (ret) {
728 dev_err(chip->dev, "Failed to get connector caps: %d\n", ret);
729 goto main_reg_disable;
730 }
731
732 ret = stusb160x_chip_init(chip);
733 if (ret) {
734 dev_err(chip->dev, "Failed to init port: %d\n", ret);
735 goto main_reg_disable;
736 }
737
738 chip->port = typec_register_port(chip->dev, &chip->capability);
739 if (IS_ERR(chip->port)) {
740 ret = PTR_ERR(chip->port);
741 goto all_reg_disable;
742 }
743
744
745
746
747
748 typec_set_pwr_opmode(chip->port, chip->pwr_opmode);
749
750 if (client->irq) {
751 chip->role_sw = fwnode_usb_role_switch_get(fwnode);
752 if (IS_ERR(chip->role_sw)) {
753 ret = PTR_ERR(chip->role_sw);
754 if (ret != -EPROBE_DEFER)
755 dev_err(chip->dev,
756 "Failed to get usb role switch: %d\n",
757 ret);
758 goto port_unregister;
759 }
760
761 ret = stusb160x_irq_init(chip, client->irq);
762 if (ret)
763 goto role_sw_put;
764 } else {
765
766
767
768
769
770
771 if (chip->port_type != TYPEC_PORT_SNK && chip->vdd_supply) {
772 ret = regulator_enable(chip->vdd_supply);
773 if (ret) {
774 dev_err(chip->dev,
775 "Failed to enable VDD supply: %d\n",
776 ret);
777 goto port_unregister;
778 }
779 chip->vbus_on = true;
780 }
781 }
782
783 fwnode_handle_put(fwnode);
784
785 return 0;
786
787role_sw_put:
788 if (chip->role_sw)
789 usb_role_switch_put(chip->role_sw);
790port_unregister:
791 typec_unregister_port(chip->port);
792all_reg_disable:
793 if (stusb160x_get_vconn(chip))
794 stusb160x_set_vconn(chip, false);
795main_reg_disable:
796 if (chip->main_supply)
797 regulator_disable(chip->main_supply);
798fwnode_put:
799 fwnode_handle_put(fwnode);
800
801 return ret;
802}
803
804static int stusb160x_remove(struct i2c_client *client)
805{
806 struct stusb160x *chip = i2c_get_clientdata(client);
807
808 if (chip->partner) {
809 typec_unregister_partner(chip->partner);
810 chip->partner = NULL;
811 }
812
813 if (chip->vbus_on)
814 regulator_disable(chip->vdd_supply);
815
816 if (chip->role_sw)
817 usb_role_switch_put(chip->role_sw);
818
819 typec_unregister_port(chip->port);
820
821 if (stusb160x_get_vconn(chip))
822 stusb160x_set_vconn(chip, false);
823
824 if (chip->main_supply)
825 regulator_disable(chip->main_supply);
826
827 return 0;
828}
829
830static int __maybe_unused stusb160x_suspend(struct device *dev)
831{
832 struct stusb160x *chip = dev_get_drvdata(dev);
833
834
835 return regmap_update_bits(chip->regmap,
836 STUSB160X_ALERT_STATUS_MASK_CTRL,
837 STUSB160X_ALL_ALERTS, STUSB160X_ALL_ALERTS);
838}
839
840static int __maybe_unused stusb160x_resume(struct device *dev)
841{
842 struct stusb160x *chip = dev_get_drvdata(dev);
843 u32 status;
844 int ret;
845
846 ret = regcache_sync(chip->regmap);
847 if (ret)
848 return ret;
849
850
851 ret = regmap_read(chip->regmap,
852 STUSB160X_CC_CONNECTION_STATUS, &status);
853 if (ret)
854 return ret;
855
856 if (chip->partner && !(status & STUSB160X_CC_ATTACH))
857 stusb160x_detach(chip, status);
858
859 if (!chip->partner && (status & STUSB160X_CC_ATTACH)) {
860 ret = stusb160x_attach(chip, status);
861 if (ret)
862 dev_err(chip->dev, "attach failed: %d\n", ret);
863 }
864
865
866 return regmap_write_bits(chip->regmap, STUSB160X_ALERT_STATUS_MASK_CTRL,
867 STUSB160X_CC_CONNECTION, 0);
868}
869
870static SIMPLE_DEV_PM_OPS(stusb160x_pm_ops, stusb160x_suspend, stusb160x_resume);
871
872static struct i2c_driver stusb160x_driver = {
873 .driver = {
874 .name = "stusb160x",
875 .pm = &stusb160x_pm_ops,
876 .of_match_table = stusb160x_of_match,
877 },
878 .probe_new = stusb160x_probe,
879 .remove = stusb160x_remove,
880};
881module_i2c_driver(stusb160x_driver);
882
883MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
884MODULE_DESCRIPTION("STMicroelectronics STUSB160x Type-C controller driver");
885MODULE_LICENSE("GPL v2");
886