1 // SPDX-License-Identifier: GPL-2.0-only
3 // Qualcomm PMIC VBUS output regulator driver
5 // Copyright (c) 2020, The Linux Foundation. All rights reserved.
7 #include <linux/module.h>
9 #include <linux/kernel.h>
11 #include <linux/platform_device.h>
12 #include <linux/regulator/driver.h>
13 #include <linux/regulator/of_regulator.h>
14 #include <linux/regmap.h>
18 #define OTG_CURRENT_LIMIT_CFG 0x52
19 #define OTG_CURRENT_LIMIT_MASK GENMASK(2, 0)
21 #define OTG_EN_SRC_CFG BIT(1)
23 static const unsigned int curr_table
[] = {
24 500000, 1000000, 1500000, 2000000, 2500000, 3000000,
27 static const struct regulator_ops qcom_usb_vbus_reg_ops
= {
28 .enable
= regulator_enable_regmap
,
29 .disable
= regulator_disable_regmap
,
30 .is_enabled
= regulator_is_enabled_regmap
,
31 .get_current_limit
= regulator_get_current_limit_regmap
,
32 .set_current_limit
= regulator_set_current_limit_regmap
,
35 static struct regulator_desc qcom_usb_vbus_rdesc
= {
37 .ops
= &qcom_usb_vbus_reg_ops
,
39 .type
= REGULATOR_VOLTAGE
,
40 .curr_table
= curr_table
,
41 .n_current_limits
= ARRAY_SIZE(curr_table
),
44 static int qcom_usb_vbus_regulator_probe(struct platform_device
*pdev
)
46 struct device
*dev
= &pdev
->dev
;
47 struct regulator_dev
*rdev
;
48 struct regmap
*regmap
;
49 struct regulator_config config
= { };
50 struct regulator_init_data
*init_data
;
54 ret
= of_property_read_u32(dev
->of_node
, "reg", &base
);
56 dev_err(dev
, "no base address found\n");
60 regmap
= dev_get_regmap(dev
->parent
, NULL
);
62 dev_err(dev
, "Failed to get regmap\n");
66 init_data
= of_get_regulator_init_data(dev
, dev
->of_node
,
67 &qcom_usb_vbus_rdesc
);
71 qcom_usb_vbus_rdesc
.enable_reg
= base
+ CMD_OTG
;
72 qcom_usb_vbus_rdesc
.enable_mask
= OTG_EN
;
73 qcom_usb_vbus_rdesc
.csel_reg
= base
+ OTG_CURRENT_LIMIT_CFG
;
74 qcom_usb_vbus_rdesc
.csel_mask
= OTG_CURRENT_LIMIT_MASK
;
76 config
.init_data
= init_data
;
77 config
.of_node
= dev
->of_node
;
78 config
.regmap
= regmap
;
80 rdev
= devm_regulator_register(dev
, &qcom_usb_vbus_rdesc
, &config
);
83 dev_err(dev
, "not able to register vbus reg %d\n", ret
);
87 /* Disable HW logic for VBUS enable */
88 regmap_update_bits(regmap
, base
+ OTG_CFG
, OTG_EN_SRC_CFG
, 0);
93 static const struct of_device_id qcom_usb_vbus_regulator_match
[] = {
94 { .compatible
= "qcom,pm8150b-vbus-reg" },
97 MODULE_DEVICE_TABLE(of
, qcom_usb_vbus_regulator_match
);
99 static struct platform_driver qcom_usb_vbus_regulator_driver
= {
101 .name
= "qcom-usb-vbus-regulator",
102 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
103 .of_match_table
= qcom_usb_vbus_regulator_match
,
105 .probe
= qcom_usb_vbus_regulator_probe
,
107 module_platform_driver(qcom_usb_vbus_regulator_driver
);
109 MODULE_DESCRIPTION("Qualcomm USB vbus regulator driver");
110 MODULE_LICENSE("GPL v2");