1 // SPDX-License-Identifier: GPL-2.0
3 * STMicroelectronics STUSB160x Type-C controller family driver
5 * Copyright (C) 2020, STMicroelectronics
6 * Author(s): Amelie Delaunay <amelie.delaunay@st.com>
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>
19 #define STUSB160X_ALERT_STATUS 0x0B /* RC */
20 #define STUSB160X_ALERT_STATUS_MASK_CTRL 0x0C /* RW */
21 #define STUSB160X_CC_CONNECTION_STATUS_TRANS 0x0D /* RC */
22 #define STUSB160X_CC_CONNECTION_STATUS 0x0E /* RO */
23 #define STUSB160X_MONITORING_STATUS_TRANS 0x0F /* RC */
24 #define STUSB160X_MONITORING_STATUS 0x10 /* RO */
25 #define STUSB160X_CC_OPERATION_STATUS 0x11 /* RO */
26 #define STUSB160X_HW_FAULT_STATUS_TRANS 0x12 /* RC */
27 #define STUSB160X_HW_FAULT_STATUS 0x13 /* RO */
28 #define STUSB160X_CC_CAPABILITY_CTRL 0x18 /* RW */
29 #define STUSB160X_CC_VCONN_SWITCH_CTRL 0x1E /* RW */
30 #define STUSB160X_VCONN_MONITORING_CTRL 0x20 /* RW */
31 #define STUSB160X_VBUS_MONITORING_RANGE_CTRL 0x22 /* RW */
32 #define STUSB160X_RESET_CTRL 0x23 /* RW */
33 #define STUSB160X_VBUS_DISCHARGE_TIME_CTRL 0x25 /* RW */
34 #define STUSB160X_VBUS_DISCHARGE_STATUS 0x26 /* RO */
35 #define STUSB160X_VBUS_ENABLE_STATUS 0x27 /* RO */
36 #define STUSB160X_CC_POWER_MODE_CTRL 0x28 /* RW */
37 #define STUSB160X_VBUS_MONITORING_CTRL 0x2E /* RW */
38 #define STUSB1600_REG_MAX 0x2F /* RO - Reserved */
40 /* STUSB160X_ALERT_STATUS/STUSB160X_ALERT_STATUS_MASK_CTRL bitfields */
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)
46 /* STUSB160X_CC_CONNECTION_STATUS_TRANS bitfields */
47 #define STUSB160X_CC_ATTACH_TRANS BIT(0)
49 /* STUSB160X_CC_CONNECTION_STATUS bitfields */
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)
56 /* STUSB160X_MONITORING_STATUS_TRANS bitfields */
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)
62 /* STUSB160X_MONITORING_STATUS bitfields */
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)
68 /* STUSB160X_CC_OPERATION_STATUS bitfields */
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)
73 /* STUSB160X_HW_FAULT_STATUS_TRANS bitfields */
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)
81 /* STUSB160X_HW_FAULT_STATUS bitfields */
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)
91 /* STUSB160X_CC_CAPABILITY_CTRL bitfields */
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)
96 /* STUSB160X_VCONN_SWITCH_CTRL bitfields */
97 #define STUSB160X_CC_VCONN_SWITCH_ILIM GENMASK(3, 0)
99 /* STUSB160X_VCONN_MONITORING_CTRL bitfields */
100 #define STUSB160X_VCONN_UVLO_THRESHOLD BIT(6)
101 #define STUSB160X_VCONN_MONITORING_EN BIT(7)
103 /* STUSB160X_VBUS_MONITORING_RANGE_CTRL bitfields */
104 #define STUSB160X_SHIFT_LOW_VBUS_LIMIT GENMASK(3, 0)
105 #define STUSB160X_SHIFT_HIGH_VBUS_LIMIT GENMASK(7, 4)
107 /* STUSB160X_RESET_CTRL bitfields */
108 #define STUSB160X_SW_RESET_EN BIT(0)
110 /* STUSB160X_VBUS_DISCHARGE_TIME_CTRL bitfields */
111 #define STUSBXX02_VBUS_DISCHARGE_TIME_TO_PDO GENMASK(3, 0)
112 #define STUSB160X_VBUS_DISCHARGE_TIME_TO_0V GENMASK(7, 4)
114 /* STUSB160X_VBUS_DISCHARGE_STATUS bitfields */
115 #define STUSB160X_VBUS_DISCHARGE_EN BIT(7)
117 /* STUSB160X_VBUS_ENABLE_STATUS bitfields */
118 #define STUSB160X_VBUS_SOURCE_EN BIT(0)
119 #define STUSB160X_VBUS_SINK_EN BIT(1)
121 /* STUSB160X_CC_POWER_MODE_CTRL bitfields */
122 #define STUSB160X_CC_POWER_MODE GENMASK(2, 0)
124 /* STUSB160X_VBUS_MONITORING_CTRL bitfields */
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)
130 enum stusb160x_pwr_mode {
131 SOURCE_WITH_ACCESSORY,
133 SINK_WITHOUT_ACCESSORY,
135 DUAL_WITH_ACCESSORY_AND_TRY_SRC,
136 DUAL_WITH_ACCESSORY_AND_TRY_SNK,
139 enum stusb160x_attached_mode {
143 DEBUG_ACCESSORY_ATTACHED,
144 AUDIO_ACCESSORY_ATTACHED,
149 struct regmap *regmap;
150 struct regulator *vdd_supply;
151 struct regulator *vsys_supply;
152 struct regulator *vconn_supply;
153 struct regulator *main_supply;
155 struct typec_port *port;
156 struct typec_capability capability;
157 struct typec_partner *partner;
159 enum typec_port_type port_type;
160 enum typec_pwr_opmode pwr_opmode;
163 struct usb_role_switch *role_sw;
166 static bool stusb160x_reg_writeable(struct device *dev, unsigned int 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:
184 static bool stusb160x_reg_readable(struct device *dev, unsigned int reg)
187 (reg >= 0x14 && reg <= 0x17) ||
188 (reg >= 0x19 && reg <= 0x1D) ||
189 (reg >= 0x29 && reg <= 0x2D) ||
190 (reg == 0x1F || reg == 0x21 || reg == 0x24 || reg == 0x2F))
196 static bool stusb160x_reg_volatile(struct device *dev, unsigned int 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:
215 static bool stusb160x_reg_precious(struct device *dev, unsigned int 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:
228 static const struct regmap_config stusb1600_regmap_config = {
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,
240 static bool stusb160x_get_vconn(struct stusb160x *chip)
245 ret = regmap_read(chip->regmap, STUSB160X_CC_CAPABILITY_CTRL, &val);
247 dev_err(chip->dev, "Unable to get Vconn status: %d\n", ret);
251 return !!FIELD_GET(STUSB160X_CC_VCONN_SUPPLY_EN, val);
254 static int stusb160x_set_vconn(struct stusb160x *chip, bool on)
258 /* Manage VCONN input supply */
259 if (chip->vconn_supply) {
261 ret = regulator_enable(chip->vconn_supply);
264 "failed to enable vconn supply: %d\n",
269 regulator_disable(chip->vconn_supply);
273 /* Manage VCONN monitoring and power path */
274 ret = regmap_update_bits(chip->regmap, STUSB160X_VCONN_MONITORING_CTRL,
275 STUSB160X_VCONN_MONITORING_EN,
276 on ? STUSB160X_VCONN_MONITORING_EN : 0);
278 goto vconn_reg_disable;
283 if (chip->vconn_supply && on)
284 regulator_disable(chip->vconn_supply);
289 static enum typec_pwr_opmode stusb160x_get_pwr_opmode(struct stusb160x *chip)
294 ret = regmap_read(chip->regmap, STUSB160X_CC_CAPABILITY_CTRL, &val);
296 dev_err(chip->dev, "Unable to get pwr opmode: %d\n", ret);
297 return TYPEC_PWR_MODE_USB;
300 return FIELD_GET(STUSB160X_CC_CURRENT_ADVERTISED, val);
303 static enum typec_accessory stusb160x_get_accessory(u32 status)
305 enum stusb160x_attached_mode mode;
307 mode = FIELD_GET(STUSB160X_CC_ATTACHED_MODE, status);
310 case DEBUG_ACCESSORY_ATTACHED:
311 return TYPEC_ACCESSORY_DEBUG;
312 case AUDIO_ACCESSORY_ATTACHED:
313 return TYPEC_ACCESSORY_AUDIO;
315 return TYPEC_ACCESSORY_NONE;
319 static enum typec_role stusb160x_get_vconn_role(u32 status)
321 if (FIELD_GET(STUSB160X_CC_VCONN_SUPPLY, status))
327 static void stusb160x_set_data_role(struct stusb160x *chip,
328 enum typec_data_role data_role,
331 enum usb_role usb_role = USB_ROLE_NONE;
334 if (data_role == TYPEC_HOST)
335 usb_role = USB_ROLE_HOST;
337 usb_role = USB_ROLE_DEVICE;
340 usb_role_switch_set_role(chip->role_sw, usb_role);
341 typec_set_data_role(chip->port, data_role);
344 static int stusb160x_attach(struct stusb160x *chip, u32 status)
346 struct typec_partner_desc desc;
349 if ((STUSB160X_CC_POWER_ROLE(status) == TYPEC_SOURCE) &&
351 ret = regulator_enable(chip->vdd_supply);
354 "Failed to enable Vbus supply: %d\n", ret);
357 chip->vbus_on = true;
361 desc.accessory = stusb160x_get_accessory(status);
362 desc.identity = NULL;
364 chip->partner = typec_register_partner(chip->port, &desc);
365 if (IS_ERR(chip->partner)) {
366 ret = PTR_ERR(chip->partner);
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);
379 regulator_disable(chip->vdd_supply);
380 chip->vbus_on = false;
386 static void stusb160x_detach(struct stusb160x *chip, u32 status)
388 typec_unregister_partner(chip->partner);
389 chip->partner = NULL;
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);
397 regulator_disable(chip->vdd_supply);
398 chip->vbus_on = false;
402 static irqreturn_t stusb160x_irq_handler(int irq, void *data)
404 struct stusb160x *chip = data;
405 u32 pending, trans, status;
408 ret = regmap_read(chip->regmap, STUSB160X_ALERT_STATUS, &pending);
412 if (pending & STUSB160X_CC_CONNECTION) {
413 ret = regmap_read(chip->regmap,
414 STUSB160X_CC_CONNECTION_STATUS_TRANS, &trans);
417 ret = regmap_read(chip->regmap,
418 STUSB160X_CC_CONNECTION_STATUS, &status);
422 if (trans & STUSB160X_CC_ATTACH_TRANS) {
423 if (status & STUSB160X_CC_ATTACH) {
424 ret = stusb160x_attach(chip, status);
428 stusb160x_detach(chip, status);
436 static int stusb160x_irq_init(struct stusb160x *chip, int irq)
441 ret = regmap_read(chip->regmap,
442 STUSB160X_CC_CONNECTION_STATUS, &status);
446 if (status & STUSB160X_CC_ATTACH) {
447 ret = stusb160x_attach(chip, status);
449 dev_err(chip->dev, "attach failed: %d\n", ret);
452 ret = devm_request_threaded_irq(chip->dev, irq, NULL,
453 stusb160x_irq_handler, IRQF_ONESHOT,
454 dev_name(chip->dev), chip);
456 goto partner_unregister;
458 /* Unmask CC_CONNECTION events */
459 ret = regmap_write_bits(chip->regmap, STUSB160X_ALERT_STATUS_MASK_CTRL,
460 STUSB160X_CC_CONNECTION, 0);
462 goto partner_unregister;
468 typec_unregister_partner(chip->partner);
469 chip->partner = NULL;
475 static int stusb160x_chip_init(struct stusb160x *chip)
480 /* Change the default Type-C power mode */
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 /* (chip->port_type == TYPEC_PORT_DRP) */
492 ret = regmap_update_bits(chip->regmap,
493 STUSB160X_CC_POWER_MODE_CTRL,
494 STUSB160X_CC_POWER_MODE,
495 DUAL_WITH_ACCESSORY);
499 if (chip->port_type == TYPEC_PORT_SNK)
502 /* Change the default Type-C Source power operation mode capability */
503 ret = regmap_update_bits(chip->regmap, STUSB160X_CC_CAPABILITY_CTRL,
504 STUSB160X_CC_CURRENT_ADVERTISED,
505 FIELD_PREP(STUSB160X_CC_CURRENT_ADVERTISED,
510 /* Manage Type-C Source Vconn supply */
511 if (stusb160x_get_vconn(chip)) {
512 ret = stusb160x_set_vconn(chip, true);
518 /* Mask all events interrupts - to be unmasked with interrupt support */
519 ret = regmap_update_bits(chip->regmap, STUSB160X_ALERT_STATUS_MASK_CTRL,
520 STUSB160X_ALL_ALERTS, STUSB160X_ALL_ALERTS);
524 /* Read status at least once to clear any stale interrupts */
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);
533 static int stusb160x_get_fw_caps(struct stusb160x *chip,
534 struct fwnode_handle *fwnode)
539 chip->capability.fwnode = fwnode;
542 * Supported port type can be configured through device tree
543 * else it is read from chip registers in stusb160x_get_caps.
545 ret = fwnode_property_read_string(fwnode, "power-role", &cap_str);
547 ret = typec_find_port_power_role(cap_str);
550 chip->port_type = ret;
552 chip->capability.type = chip->port_type;
554 /* Skip DRP/Source capabilities in case of Sink only */
555 if (chip->port_type == TYPEC_PORT_SNK)
558 if (chip->port_type == TYPEC_PORT_DRP)
559 chip->capability.prefer_role = TYPEC_SINK;
562 * Supported power operation mode can be configured through device tree
563 * else it is read from chip registers in stusb160x_get_caps.
565 ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &cap_str);
567 ret = typec_find_pwr_opmode(cap_str);
568 /* Power delivery not yet supported */
569 if (ret < 0 || ret == TYPEC_PWR_MODE_PD) {
570 dev_err(chip->dev, "bad power operation mode: %d\n", ret);
573 chip->pwr_opmode = ret;
579 static int stusb160x_get_caps(struct stusb160x *chip)
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;
587 chip->capability.revision = USB_TYPEC_REV_1_2;
589 ret = regmap_read(chip->regmap, STUSB160X_CC_POWER_MODE_CTRL, &val);
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;
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;
606 case SINK_WITHOUT_ACCESSORY:
607 *type = TYPEC_PORT_SNK;
608 *data = TYPEC_PORT_UFP;
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;
622 chip->port_type = *type;
623 chip->pwr_opmode = stusb160x_get_pwr_opmode(chip);
628 static const struct of_device_id stusb160x_of_match[] = {
629 { .compatible = "st,stusb1600", .data = &stusb1600_regmap_config},
632 MODULE_DEVICE_TABLE(of, stusb160x_of_match);
634 static int stusb160x_probe(struct i2c_client *client)
636 struct stusb160x *chip;
637 const struct of_device_id *match;
638 struct regmap_config *regmap_config;
639 struct fwnode_handle *fwnode;
642 chip = devm_kzalloc(&client->dev, sizeof(struct stusb160x), GFP_KERNEL);
646 i2c_set_clientdata(client, chip);
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);
658 chip->dev = &client->dev;
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);
665 chip->vsys_supply = NULL;
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);
673 chip->vdd_supply = NULL;
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);
681 chip->vconn_supply = NULL;
684 fwnode = device_get_named_child_node(chip->dev, "connector");
689 * This fwnode has a "compatible" property, but is never populated as a
690 * struct device. Instead we simply parse it to read the properties.
691 * This it breaks fw_devlink=on. To maintain backward compatibility
692 * with existing DT files, we work around this by deleting any
693 * fwnode_links to/from this fwnode.
695 fw_devlink_purge_absent_suppliers(fwnode);
698 * When both VDD and VSYS power supplies are present, the low power
699 * supply VSYS is selected when VSYS voltage is above 3.1 V.
700 * Otherwise VDD is selected.
702 if (chip->vdd_supply &&
703 (!chip->vsys_supply ||
704 (regulator_get_voltage(chip->vsys_supply) <= 3100000)))
705 chip->main_supply = chip->vdd_supply;
707 chip->main_supply = chip->vsys_supply;
709 if (chip->main_supply) {
710 ret = regulator_enable(chip->main_supply);
713 "Failed to enable main supply: %d\n", ret);
718 /* Get configuration from chip */
719 ret = stusb160x_get_caps(chip);
721 dev_err(chip->dev, "Failed to get port caps: %d\n", ret);
722 goto main_reg_disable;
725 /* Get optional re-configuration from device tree */
726 ret = stusb160x_get_fw_caps(chip, fwnode);
728 dev_err(chip->dev, "Failed to get connector caps: %d\n", ret);
729 goto main_reg_disable;
732 ret = stusb160x_chip_init(chip);
734 dev_err(chip->dev, "Failed to init port: %d\n", ret);
735 goto main_reg_disable;
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;
745 * Default power operation mode initialization: will be updated upon
746 * attach/detach interrupt
748 typec_set_pwr_opmode(chip->port, chip->pwr_opmode);
751 chip->role_sw = fwnode_usb_role_switch_get(fwnode);
752 if (IS_ERR(chip->role_sw)) {
753 ret = dev_err_probe(chip->dev, PTR_ERR(chip->role_sw),
754 "Failed to get usb role switch\n");
755 goto port_unregister;
758 ret = stusb160x_irq_init(chip, client->irq);
763 * If Source or Dual power role, need to enable VDD supply
764 * providing Vbus if present. In case of interrupt support,
765 * VDD supply will be dynamically managed upon attach/detach
768 if (chip->port_type != TYPEC_PORT_SNK && chip->vdd_supply) {
769 ret = regulator_enable(chip->vdd_supply);
772 "Failed to enable VDD supply: %d\n",
774 goto port_unregister;
776 chip->vbus_on = true;
780 fwnode_handle_put(fwnode);
786 usb_role_switch_put(chip->role_sw);
788 typec_unregister_port(chip->port);
790 if (stusb160x_get_vconn(chip))
791 stusb160x_set_vconn(chip, false);
793 if (chip->main_supply)
794 regulator_disable(chip->main_supply);
796 fwnode_handle_put(fwnode);
801 static void stusb160x_remove(struct i2c_client *client)
803 struct stusb160x *chip = i2c_get_clientdata(client);
806 typec_unregister_partner(chip->partner);
807 chip->partner = NULL;
811 regulator_disable(chip->vdd_supply);
814 usb_role_switch_put(chip->role_sw);
816 typec_unregister_port(chip->port);
818 if (stusb160x_get_vconn(chip))
819 stusb160x_set_vconn(chip, false);
821 if (chip->main_supply)
822 regulator_disable(chip->main_supply);
825 static int __maybe_unused stusb160x_suspend(struct device *dev)
827 struct stusb160x *chip = dev_get_drvdata(dev);
829 /* Mask interrupts */
830 return regmap_update_bits(chip->regmap,
831 STUSB160X_ALERT_STATUS_MASK_CTRL,
832 STUSB160X_ALL_ALERTS, STUSB160X_ALL_ALERTS);
835 static int __maybe_unused stusb160x_resume(struct device *dev)
837 struct stusb160x *chip = dev_get_drvdata(dev);
841 ret = regcache_sync(chip->regmap);
845 /* Check if attach/detach occurred during low power */
846 ret = regmap_read(chip->regmap,
847 STUSB160X_CC_CONNECTION_STATUS, &status);
851 if (chip->partner && !(status & STUSB160X_CC_ATTACH))
852 stusb160x_detach(chip, status);
854 if (!chip->partner && (status & STUSB160X_CC_ATTACH)) {
855 ret = stusb160x_attach(chip, status);
857 dev_err(chip->dev, "attach failed: %d\n", ret);
860 /* Unmask interrupts */
861 return regmap_write_bits(chip->regmap, STUSB160X_ALERT_STATUS_MASK_CTRL,
862 STUSB160X_CC_CONNECTION, 0);
865 static SIMPLE_DEV_PM_OPS(stusb160x_pm_ops, stusb160x_suspend, stusb160x_resume);
867 static struct i2c_driver stusb160x_driver = {
870 .pm = &stusb160x_pm_ops,
871 .of_match_table = stusb160x_of_match,
873 .probe_new = stusb160x_probe,
874 .remove = stusb160x_remove,
876 module_i2c_driver(stusb160x_driver);
878 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
879 MODULE_DESCRIPTION("STMicroelectronics STUSB160x Type-C controller driver");
880 MODULE_LICENSE("GPL v2");