1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Qualcomm PMIC VBUS output regulator driver
4 //
5 // Copyright (c) 2020, The Linux Foundation. All rights reserved.
6 
7 #include <linux/module.h>
8 #include <linux/err.h>
9 #include <linux/kernel.h>
10 #include <linux/of.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/regulator/of_regulator.h>
15 #include <linux/regmap.h>
16 
17 #define CMD_OTG				0x40
18 #define OTG_EN				BIT(0)
19 #define OTG_CFG				0x53
20 #define OTG_EN_SRC_CFG			BIT(1)
21 
22 static const struct regulator_ops qcom_usb_vbus_reg_ops = {
23 	.enable = regulator_enable_regmap,
24 	.disable = regulator_disable_regmap,
25 	.is_enabled = regulator_is_enabled_regmap,
26 };
27 
28 static struct regulator_desc qcom_usb_vbus_rdesc = {
29 	.name = "usb_vbus",
30 	.ops = &qcom_usb_vbus_reg_ops,
31 	.owner = THIS_MODULE,
32 	.type = REGULATOR_VOLTAGE,
33 };
34 
35 static int qcom_usb_vbus_regulator_probe(struct platform_device *pdev)
36 {
37 	struct device *dev = &pdev->dev;
38 	struct regulator_dev *rdev;
39 	struct regmap *regmap;
40 	struct regulator_config config = { };
41 	struct regulator_init_data *init_data;
42 	int ret;
43 	u32 base;
44 
45 	ret = of_property_read_u32(dev->of_node, "reg", &base);
46 	if (ret < 0) {
47 		dev_err(dev, "no base address found\n");
48 		return ret;
49 	}
50 
51 	regmap = dev_get_regmap(dev->parent, NULL);
52 	if (!regmap) {
53 		dev_err(dev, "Failed to get regmap\n");
54 		return -ENOENT;
55 	}
56 
57 	init_data = of_get_regulator_init_data(dev, dev->of_node,
58 					       &qcom_usb_vbus_rdesc);
59 	if (!init_data)
60 		return -ENOMEM;
61 
62 	qcom_usb_vbus_rdesc.enable_reg = base + CMD_OTG;
63 	qcom_usb_vbus_rdesc.enable_mask = OTG_EN;
64 	config.dev = dev;
65 	config.init_data = init_data;
66 	config.regmap = regmap;
67 
68 	rdev = devm_regulator_register(dev, &qcom_usb_vbus_rdesc, &config);
69 	if (IS_ERR(rdev)) {
70 		ret = PTR_ERR(rdev);
71 		dev_err(dev, "not able to register vbus reg %d\n", ret);
72 		return ret;
73 	}
74 
75 	/* Disable HW logic for VBUS enable */
76 	regmap_update_bits(regmap, base + OTG_CFG, OTG_EN_SRC_CFG, 0);
77 
78 	return 0;
79 }
80 
81 static const struct of_device_id qcom_usb_vbus_regulator_match[] = {
82 	{ .compatible = "qcom,pm8150b-vbus-reg" },
83 	{ }
84 };
85 MODULE_DEVICE_TABLE(of, qcom_usb_vbus_regulator_match);
86 
87 static struct platform_driver qcom_usb_vbus_regulator_driver = {
88 	.driver		= {
89 		.name	= "qcom-usb-vbus-regulator",
90 		.of_match_table = qcom_usb_vbus_regulator_match,
91 	},
92 	.probe		= qcom_usb_vbus_regulator_probe,
93 };
94 module_platform_driver(qcom_usb_vbus_regulator_driver);
95 
96 MODULE_DESCRIPTION("Qualcomm USB vbus regulator driver");
97 MODULE_LICENSE("GPL v2");
98