0001
0002
0003
0004
0005
0006 #include <linux/err.h>
0007 #include <linux/interrupt.h>
0008 #include <linux/kernel.h>
0009 #include <linux/mod_devicetable.h>
0010 #include <linux/module.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/regmap.h>
0013 #include <linux/regulator/consumer.h>
0014 #include <linux/slab.h>
0015 #include <linux/usb/role.h>
0016 #include <linux/usb/typec_mux.h>
0017
0018 #define TYPEC_MISC_STATUS 0xb
0019 #define CC_ATTACHED BIT(0)
0020 #define CC_ORIENTATION BIT(1)
0021 #define SNK_SRC_MODE BIT(6)
0022 #define TYPEC_MODE_CFG 0x44
0023 #define TYPEC_DISABLE_CMD BIT(0)
0024 #define EN_SNK_ONLY BIT(1)
0025 #define EN_SRC_ONLY BIT(2)
0026 #define TYPEC_VCONN_CONTROL 0x46
0027 #define VCONN_EN_SRC BIT(0)
0028 #define VCONN_EN_VAL BIT(1)
0029 #define TYPEC_EXIT_STATE_CFG 0x50
0030 #define SEL_SRC_UPPER_REF BIT(2)
0031 #define TYPEC_INTR_EN_CFG_1 0x5e
0032 #define TYPEC_INTR_EN_CFG_1_MASK GENMASK(7, 0)
0033
0034 struct qcom_pmic_typec {
0035 struct device *dev;
0036 struct regmap *regmap;
0037 u32 base;
0038
0039 struct typec_port *port;
0040 struct usb_role_switch *role_sw;
0041
0042 struct regulator *vbus_reg;
0043 bool vbus_enabled;
0044 };
0045
0046 static void qcom_pmic_typec_enable_vbus_regulator(struct qcom_pmic_typec
0047 *qcom_usb, bool enable)
0048 {
0049 int ret;
0050
0051 if (enable == qcom_usb->vbus_enabled)
0052 return;
0053
0054 if (enable) {
0055 ret = regulator_enable(qcom_usb->vbus_reg);
0056 if (ret)
0057 return;
0058 } else {
0059 ret = regulator_disable(qcom_usb->vbus_reg);
0060 if (ret)
0061 return;
0062 }
0063 qcom_usb->vbus_enabled = enable;
0064 }
0065
0066 static void qcom_pmic_typec_check_connection(struct qcom_pmic_typec *qcom_usb)
0067 {
0068 enum typec_orientation orientation;
0069 enum usb_role role;
0070 unsigned int stat;
0071 bool enable_vbus;
0072
0073 regmap_read(qcom_usb->regmap, qcom_usb->base + TYPEC_MISC_STATUS,
0074 &stat);
0075
0076 if (stat & CC_ATTACHED) {
0077 orientation = (stat & CC_ORIENTATION) ?
0078 TYPEC_ORIENTATION_REVERSE :
0079 TYPEC_ORIENTATION_NORMAL;
0080 typec_set_orientation(qcom_usb->port, orientation);
0081
0082 role = (stat & SNK_SRC_MODE) ? USB_ROLE_HOST : USB_ROLE_DEVICE;
0083 if (role == USB_ROLE_HOST)
0084 enable_vbus = true;
0085 else
0086 enable_vbus = false;
0087 } else {
0088 role = USB_ROLE_NONE;
0089 enable_vbus = false;
0090 }
0091
0092 qcom_pmic_typec_enable_vbus_regulator(qcom_usb, enable_vbus);
0093 usb_role_switch_set_role(qcom_usb->role_sw, role);
0094 }
0095
0096 static irqreturn_t qcom_pmic_typec_interrupt(int irq, void *_qcom_usb)
0097 {
0098 struct qcom_pmic_typec *qcom_usb = _qcom_usb;
0099
0100 qcom_pmic_typec_check_connection(qcom_usb);
0101 return IRQ_HANDLED;
0102 }
0103
0104 static void qcom_pmic_typec_typec_hw_init(struct qcom_pmic_typec *qcom_usb,
0105 enum typec_port_type type)
0106 {
0107 u8 mode = 0;
0108
0109 regmap_update_bits(qcom_usb->regmap,
0110 qcom_usb->base + TYPEC_INTR_EN_CFG_1,
0111 TYPEC_INTR_EN_CFG_1_MASK, 0);
0112
0113 if (type == TYPEC_PORT_SRC)
0114 mode = EN_SRC_ONLY;
0115 else if (type == TYPEC_PORT_SNK)
0116 mode = EN_SNK_ONLY;
0117
0118 regmap_update_bits(qcom_usb->regmap, qcom_usb->base + TYPEC_MODE_CFG,
0119 EN_SNK_ONLY | EN_SRC_ONLY, mode);
0120
0121 regmap_update_bits(qcom_usb->regmap,
0122 qcom_usb->base + TYPEC_VCONN_CONTROL,
0123 VCONN_EN_SRC | VCONN_EN_VAL, VCONN_EN_SRC);
0124 regmap_update_bits(qcom_usb->regmap,
0125 qcom_usb->base + TYPEC_EXIT_STATE_CFG,
0126 SEL_SRC_UPPER_REF, SEL_SRC_UPPER_REF);
0127 }
0128
0129 static int qcom_pmic_typec_probe(struct platform_device *pdev)
0130 {
0131 struct qcom_pmic_typec *qcom_usb;
0132 struct device *dev = &pdev->dev;
0133 struct fwnode_handle *fwnode;
0134 struct typec_capability cap;
0135 const char *buf;
0136 int ret, irq, role;
0137 u32 reg;
0138
0139 ret = device_property_read_u32(dev, "reg", ®);
0140 if (ret < 0) {
0141 dev_err(dev, "missing base address\n");
0142 return ret;
0143 }
0144
0145 qcom_usb = devm_kzalloc(dev, sizeof(*qcom_usb), GFP_KERNEL);
0146 if (!qcom_usb)
0147 return -ENOMEM;
0148
0149 qcom_usb->dev = dev;
0150 qcom_usb->base = reg;
0151
0152 qcom_usb->regmap = dev_get_regmap(dev->parent, NULL);
0153 if (!qcom_usb->regmap) {
0154 dev_err(dev, "Failed to get regmap\n");
0155 return -EINVAL;
0156 }
0157
0158 qcom_usb->vbus_reg = devm_regulator_get(qcom_usb->dev, "usb_vbus");
0159 if (IS_ERR(qcom_usb->vbus_reg))
0160 return PTR_ERR(qcom_usb->vbus_reg);
0161
0162 fwnode = device_get_named_child_node(dev, "connector");
0163 if (!fwnode)
0164 return -EINVAL;
0165
0166 ret = fwnode_property_read_string(fwnode, "power-role", &buf);
0167 if (!ret) {
0168 role = typec_find_port_power_role(buf);
0169 if (role < 0)
0170 role = TYPEC_PORT_SNK;
0171 } else {
0172 role = TYPEC_PORT_SNK;
0173 }
0174 cap.type = role;
0175
0176 ret = fwnode_property_read_string(fwnode, "data-role", &buf);
0177 if (!ret) {
0178 role = typec_find_port_data_role(buf);
0179 if (role < 0)
0180 role = TYPEC_PORT_UFP;
0181 } else {
0182 role = TYPEC_PORT_UFP;
0183 }
0184 cap.data = role;
0185
0186 cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
0187 cap.fwnode = fwnode;
0188 qcom_usb->port = typec_register_port(dev, &cap);
0189 if (IS_ERR(qcom_usb->port)) {
0190 ret = PTR_ERR(qcom_usb->port);
0191 dev_err(dev, "Failed to register type c port %d\n", ret);
0192 goto err_put_node;
0193 }
0194 fwnode_handle_put(fwnode);
0195
0196 qcom_usb->role_sw = fwnode_usb_role_switch_get(dev_fwnode(qcom_usb->dev));
0197 if (IS_ERR(qcom_usb->role_sw)) {
0198 if (PTR_ERR(qcom_usb->role_sw) != -EPROBE_DEFER)
0199 dev_err(dev, "failed to get role switch\n");
0200 ret = PTR_ERR(qcom_usb->role_sw);
0201 goto err_typec_port;
0202 }
0203
0204 irq = platform_get_irq(pdev, 0);
0205 if (irq < 0)
0206 goto err_usb_role_sw;
0207
0208 ret = devm_request_threaded_irq(qcom_usb->dev, irq, NULL,
0209 qcom_pmic_typec_interrupt, IRQF_ONESHOT,
0210 "qcom-pmic-typec", qcom_usb);
0211 if (ret) {
0212 dev_err(&pdev->dev, "Could not request IRQ\n");
0213 goto err_usb_role_sw;
0214 }
0215
0216 platform_set_drvdata(pdev, qcom_usb);
0217 qcom_pmic_typec_typec_hw_init(qcom_usb, cap.type);
0218 qcom_pmic_typec_check_connection(qcom_usb);
0219
0220 return 0;
0221
0222 err_usb_role_sw:
0223 usb_role_switch_put(qcom_usb->role_sw);
0224 err_typec_port:
0225 typec_unregister_port(qcom_usb->port);
0226 err_put_node:
0227 fwnode_handle_put(fwnode);
0228
0229 return ret;
0230 }
0231
0232 static int qcom_pmic_typec_remove(struct platform_device *pdev)
0233 {
0234 struct qcom_pmic_typec *qcom_usb = platform_get_drvdata(pdev);
0235
0236 usb_role_switch_set_role(qcom_usb->role_sw, USB_ROLE_NONE);
0237 qcom_pmic_typec_enable_vbus_regulator(qcom_usb, 0);
0238
0239 typec_unregister_port(qcom_usb->port);
0240 usb_role_switch_put(qcom_usb->role_sw);
0241
0242 return 0;
0243 }
0244
0245 static const struct of_device_id qcom_pmic_typec_table[] = {
0246 { .compatible = "qcom,pm8150b-usb-typec" },
0247 { }
0248 };
0249 MODULE_DEVICE_TABLE(of, qcom_pmic_typec_table);
0250
0251 static struct platform_driver qcom_pmic_typec = {
0252 .driver = {
0253 .name = "qcom,pmic-typec",
0254 .of_match_table = qcom_pmic_typec_table,
0255 },
0256 .probe = qcom_pmic_typec_probe,
0257 .remove = qcom_pmic_typec_remove,
0258 };
0259 module_platform_driver(qcom_pmic_typec);
0260
0261 MODULE_DESCRIPTION("QCOM PMIC USB type C driver");
0262 MODULE_LICENSE("GPL v2");