1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020, The Linux Foundation. All rights reserved.
4 #include <linux/module.h>
5 #include <linux/of_irq.h>
7 #include <linux/of_device.h>
8 #include <linux/platform_device.h>
9 #include <linux/regmap.h>
10 #include <linux/regulator/driver.h>
11 #include <linux/regulator/of_regulator.h>
13 #define REG_PERPH_TYPE 0x04
15 #define QCOM_LAB_TYPE 0x24
16 #define QCOM_IBB_TYPE 0x20
18 #define PMI8998_LAB_REG_BASE 0xde00
19 #define PMI8998_IBB_REG_BASE 0xdc00
21 #define REG_LABIBB_STATUS1 0x08
23 #define REG_LABIBB_VOLTAGE 0x41
24 #define LABIBB_VOLTAGE_OVERRIDE_EN BIT(7)
25 #define LAB_VOLTAGE_SET_MASK GENMASK(3, 0)
26 #define IBB_VOLTAGE_SET_MASK GENMASK(5, 0)
28 #define REG_LABIBB_ENABLE_CTL 0x46
29 #define LABIBB_STATUS1_VREG_OK_BIT BIT(7)
30 #define LABIBB_CONTROL_ENABLE BIT(7)
32 #define LAB_ENABLE_CTL_MASK BIT(7)
33 #define IBB_ENABLE_CTL_MASK (BIT(7) | BIT(6))
35 #define LABIBB_OFF_ON_DELAY 1000
36 #define LAB_ENABLE_TIME (LABIBB_OFF_ON_DELAY * 2)
37 #define IBB_ENABLE_TIME (LABIBB_OFF_ON_DELAY * 10)
38 #define LABIBB_POLL_ENABLED_TIME 1000
40 struct labibb_regulator {
41 struct regulator_desc desc;
43 struct regmap *regmap;
44 struct regulator_dev *rdev;
49 struct labibb_regulator_data {
53 const struct regulator_desc *desc;
56 static const struct regulator_ops qcom_labibb_ops = {
57 .enable = regulator_enable_regmap,
58 .disable = regulator_disable_regmap,
59 .is_enabled = regulator_is_enabled_regmap,
60 .set_voltage_sel = regulator_set_voltage_sel_regmap,
61 .get_voltage_sel = regulator_get_voltage_sel_regmap,
62 .list_voltage = regulator_list_voltage_linear_range,
63 .map_voltage = regulator_map_voltage_linear_range,
66 static const struct regulator_desc pmi8998_lab_desc = {
67 .enable_mask = LAB_ENABLE_CTL_MASK,
68 .enable_reg = (PMI8998_LAB_REG_BASE + REG_LABIBB_ENABLE_CTL),
69 .enable_val = LABIBB_CONTROL_ENABLE,
70 .enable_time = LAB_ENABLE_TIME,
71 .poll_enabled_time = LABIBB_POLL_ENABLED_TIME,
72 .vsel_reg = (PMI8998_LAB_REG_BASE + REG_LABIBB_VOLTAGE),
73 .vsel_mask = LAB_VOLTAGE_SET_MASK,
74 .apply_reg = (PMI8998_LAB_REG_BASE + REG_LABIBB_VOLTAGE),
75 .apply_bit = LABIBB_VOLTAGE_OVERRIDE_EN,
76 .off_on_delay = LABIBB_OFF_ON_DELAY,
78 .type = REGULATOR_VOLTAGE,
79 .linear_ranges = (struct linear_range[]) {
80 REGULATOR_LINEAR_RANGE(4600000, 0, 15, 100000),
84 .ops = &qcom_labibb_ops,
87 static const struct regulator_desc pmi8998_ibb_desc = {
88 .enable_mask = IBB_ENABLE_CTL_MASK,
89 .enable_reg = (PMI8998_IBB_REG_BASE + REG_LABIBB_ENABLE_CTL),
90 .enable_val = LABIBB_CONTROL_ENABLE,
91 .enable_time = IBB_ENABLE_TIME,
92 .poll_enabled_time = LABIBB_POLL_ENABLED_TIME,
93 .vsel_reg = (PMI8998_IBB_REG_BASE + REG_LABIBB_VOLTAGE),
94 .vsel_mask = IBB_VOLTAGE_SET_MASK,
95 .apply_reg = (PMI8998_IBB_REG_BASE + REG_LABIBB_VOLTAGE),
96 .apply_bit = LABIBB_VOLTAGE_OVERRIDE_EN,
97 .off_on_delay = LABIBB_OFF_ON_DELAY,
99 .type = REGULATOR_VOLTAGE,
100 .linear_ranges = (struct linear_range[]) {
101 REGULATOR_LINEAR_RANGE(1400000, 0, 63, 100000),
103 .n_linear_ranges = 1,
105 .ops = &qcom_labibb_ops,
108 static const struct labibb_regulator_data pmi8998_labibb_data[] = {
109 {"lab", QCOM_LAB_TYPE, PMI8998_LAB_REG_BASE, &pmi8998_lab_desc},
110 {"ibb", QCOM_IBB_TYPE, PMI8998_IBB_REG_BASE, &pmi8998_ibb_desc},
114 static const struct of_device_id qcom_labibb_match[] = {
115 { .compatible = "qcom,pmi8998-lab-ibb", .data = &pmi8998_labibb_data},
118 MODULE_DEVICE_TABLE(of, qcom_labibb_match);
120 static int qcom_labibb_regulator_probe(struct platform_device *pdev)
122 struct labibb_regulator *vreg;
123 struct device *dev = &pdev->dev;
124 struct regulator_config cfg = {};
126 const struct of_device_id *match;
127 const struct labibb_regulator_data *reg_data;
128 struct regmap *reg_regmap;
132 reg_regmap = dev_get_regmap(pdev->dev.parent, NULL);
134 dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
138 match = of_match_device(qcom_labibb_match, &pdev->dev);
142 for (reg_data = match->data; reg_data->name; reg_data++) {
144 /* Validate if the type of regulator is indeed
145 * what's mentioned in DT.
147 ret = regmap_read(reg_regmap, reg_data->base + REG_PERPH_TYPE,
151 "Peripheral type read failed ret=%d\n",
156 if (WARN_ON((type != QCOM_LAB_TYPE) && (type != QCOM_IBB_TYPE)) ||
157 WARN_ON(type != reg_data->type))
160 vreg = devm_kzalloc(&pdev->dev, sizeof(*vreg),
165 vreg->regmap = reg_regmap;
167 vreg->base = reg_data->base;
168 vreg->type = reg_data->type;
170 memcpy(&vreg->desc, reg_data->desc, sizeof(vreg->desc));
171 vreg->desc.of_match = reg_data->name;
172 vreg->desc.name = reg_data->name;
175 cfg.driver_data = vreg;
176 cfg.regmap = vreg->regmap;
178 vreg->rdev = devm_regulator_register(vreg->dev, &vreg->desc,
181 if (IS_ERR(vreg->rdev)) {
182 dev_err(dev, "qcom_labibb: error registering %s : %d\n",
183 reg_data->name, ret);
184 return PTR_ERR(vreg->rdev);
191 static struct platform_driver qcom_labibb_regulator_driver = {
193 .name = "qcom-lab-ibb-regulator",
194 .of_match_table = qcom_labibb_match,
196 .probe = qcom_labibb_regulator_probe,
198 module_platform_driver(qcom_labibb_regulator_driver);
200 MODULE_DESCRIPTION("Qualcomm labibb driver");
201 MODULE_AUTHOR("Nisha Kumari <nishakumari@codeaurora.org>");
202 MODULE_AUTHOR("Sumit Semwal <sumit.semwal@linaro.org>");
203 MODULE_LICENSE("GPL v2");