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 (IS_ERR(fwnode))
686 return PTR_ERR(fwnode);
687
688
689
690
691
692
693 if (chip->vdd_supply &&
694 (!chip->vsys_supply ||
695 (regulator_get_voltage(chip->vsys_supply) <= 3100000)))
696 chip->main_supply = chip->vdd_supply;
697 else
698 chip->main_supply = chip->vsys_supply;
699
700 if (chip->main_supply) {
701 ret = regulator_enable(chip->main_supply);
702 if (ret) {
703 dev_err(chip->dev,
704 "Failed to enable main supply: %d\n", ret);
705 goto fwnode_put;
706 }
707 }
708
709
710 ret = stusb160x_get_caps(chip);
711 if (ret) {
712 dev_err(chip->dev, "Failed to get port caps: %d\n", ret);
713 goto main_reg_disable;
714 }
715
716
717 ret = stusb160x_get_fw_caps(chip, fwnode);
718 if (ret) {
719 dev_err(chip->dev, "Failed to get connector caps: %d\n", ret);
720 goto main_reg_disable;
721 }
722
723 ret = stusb160x_chip_init(chip);
724 if (ret) {
725 dev_err(chip->dev, "Failed to init port: %d\n", ret);
726 goto main_reg_disable;
727 }
728
729 chip->port = typec_register_port(chip->dev, &chip->capability);
730 if (IS_ERR(chip->port)) {
731 ret = PTR_ERR(chip->port);
732 goto all_reg_disable;
733 }
734
735
736
737
738
739 typec_set_pwr_opmode(chip->port, chip->pwr_opmode);
740
741 if (client->irq) {
742 ret = stusb160x_irq_init(chip, client->irq);
743 if (ret)
744 goto port_unregister;
745
746 chip->role_sw = fwnode_usb_role_switch_get(fwnode);
747 if (IS_ERR(chip->role_sw)) {
748 ret = PTR_ERR(chip->role_sw);
749 if (ret != -EPROBE_DEFER)
750 dev_err(chip->dev,
751 "Failed to get usb role switch: %d\n",
752 ret);
753 goto port_unregister;
754 }
755 } else {
756
757
758
759
760
761
762 if (chip->port_type != TYPEC_PORT_SNK && chip->vdd_supply) {
763 ret = regulator_enable(chip->vdd_supply);
764 if (ret) {
765 dev_err(chip->dev,
766 "Failed to enable VDD supply: %d\n",
767 ret);
768 goto port_unregister;
769 }
770 chip->vbus_on = true;
771 }
772 }
773
774 fwnode_handle_put(fwnode);
775
776 return 0;
777
778port_unregister:
779 typec_unregister_port(chip->port);
780all_reg_disable:
781 if (stusb160x_get_vconn(chip))
782 stusb160x_set_vconn(chip, false);
783main_reg_disable:
784 if (chip->main_supply)
785 regulator_disable(chip->main_supply);
786fwnode_put:
787 fwnode_handle_put(fwnode);
788
789 return ret;
790}
791
792static int stusb160x_remove(struct i2c_client *client)
793{
794 struct stusb160x *chip = i2c_get_clientdata(client);
795
796 if (chip->partner) {
797 typec_unregister_partner(chip->partner);
798 chip->partner = NULL;
799 }
800
801 if (chip->vbus_on)
802 regulator_disable(chip->vdd_supply);
803
804 if (chip->role_sw)
805 usb_role_switch_put(chip->role_sw);
806
807 typec_unregister_port(chip->port);
808
809 if (stusb160x_get_vconn(chip))
810 stusb160x_set_vconn(chip, false);
811
812 if (chip->main_supply)
813 regulator_disable(chip->main_supply);
814
815 return 0;
816}
817
818static int __maybe_unused stusb160x_suspend(struct device *dev)
819{
820 struct stusb160x *chip = dev_get_drvdata(dev);
821
822
823 return regmap_update_bits(chip->regmap,
824 STUSB160X_ALERT_STATUS_MASK_CTRL,
825 STUSB160X_ALL_ALERTS, STUSB160X_ALL_ALERTS);
826}
827
828static int __maybe_unused stusb160x_resume(struct device *dev)
829{
830 struct stusb160x *chip = dev_get_drvdata(dev);
831 u32 status;
832 int ret;
833
834 ret = regcache_sync(chip->regmap);
835 if (ret)
836 return ret;
837
838
839 ret = regmap_read(chip->regmap,
840 STUSB160X_CC_CONNECTION_STATUS, &status);
841 if (ret)
842 return ret;
843
844 if (chip->partner && !(status & STUSB160X_CC_ATTACH))
845 stusb160x_detach(chip, status);
846
847 if (!chip->partner && (status & STUSB160X_CC_ATTACH)) {
848 ret = stusb160x_attach(chip, status);
849 if (ret)
850 dev_err(chip->dev, "attach failed: %d\n", ret);
851 }
852
853
854 return regmap_write_bits(chip->regmap, STUSB160X_ALERT_STATUS_MASK_CTRL,
855 STUSB160X_CC_CONNECTION, 0);
856}
857
858static SIMPLE_DEV_PM_OPS(stusb160x_pm_ops, stusb160x_suspend, stusb160x_resume);
859
860static struct i2c_driver stusb160x_driver = {
861 .driver = {
862 .name = "stusb160x",
863 .pm = &stusb160x_pm_ops,
864 .of_match_table = stusb160x_of_match,
865 },
866 .probe_new = stusb160x_probe,
867 .remove = stusb160x_remove,
868};
869module_i2c_driver(stusb160x_driver);
870
871MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
872MODULE_DESCRIPTION("STMicroelectronics STUSB160x Type-C controller driver");
873MODULE_LICENSE("GPL v2");
874