Merge series "Really implement Qualcomm LAB/IBB regulators" from AngeloGioacchino...
[linux-2.6-microblaze.git] / drivers / regulator / qcom-labibb-regulator.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020, The Linux Foundation. All rights reserved.
3
4 #include <linux/module.h>
5 #include <linux/of_irq.h>
6 #include <linux/of.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>
12
13 #define REG_PERPH_TYPE                  0x04
14
15 #define QCOM_LAB_TYPE                   0x24
16 #define QCOM_IBB_TYPE                   0x20
17
18 #define PMI8998_LAB_REG_BASE            0xde00
19 #define PMI8998_IBB_REG_BASE            0xdc00
20
21 #define REG_LABIBB_STATUS1              0x08
22
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)
27
28 #define REG_LABIBB_ENABLE_CTL           0x46
29 #define LABIBB_STATUS1_VREG_OK_BIT      BIT(7)
30 #define LABIBB_CONTROL_ENABLE           BIT(7)
31
32 #define LAB_ENABLE_CTL_MASK             BIT(7)
33 #define IBB_ENABLE_CTL_MASK             (BIT(7) | BIT(6))
34
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
39
40 struct labibb_regulator {
41         struct regulator_desc           desc;
42         struct device                   *dev;
43         struct regmap                   *regmap;
44         struct regulator_dev            *rdev;
45         u16                             base;
46         u8                              type;
47 };
48
49 struct labibb_regulator_data {
50         const char                      *name;
51         u8                              type;
52         u16                             base;
53         const struct regulator_desc     *desc;
54 };
55
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,
64 };
65
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,
77         .owner                  = THIS_MODULE,
78         .type                   = REGULATOR_VOLTAGE,
79         .linear_ranges          = (struct linear_range[]) {
80                 REGULATOR_LINEAR_RANGE(4600000, 0, 15, 100000),
81         },
82         .n_linear_ranges        = 1,
83         .n_voltages             = 16,
84         .ops                    = &qcom_labibb_ops,
85 };
86
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,
98         .owner                  = THIS_MODULE,
99         .type                   = REGULATOR_VOLTAGE,
100         .linear_ranges          = (struct linear_range[]) {
101                 REGULATOR_LINEAR_RANGE(1400000, 0, 63, 100000),
102         },
103         .n_linear_ranges        = 1,
104         .n_voltages             = 64,
105         .ops                    = &qcom_labibb_ops,
106 };
107
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},
111         { },
112 };
113
114 static const struct of_device_id qcom_labibb_match[] = {
115         { .compatible = "qcom,pmi8998-lab-ibb", .data = &pmi8998_labibb_data},
116         { },
117 };
118 MODULE_DEVICE_TABLE(of, qcom_labibb_match);
119
120 static int qcom_labibb_regulator_probe(struct platform_device *pdev)
121 {
122         struct labibb_regulator *vreg;
123         struct device *dev = &pdev->dev;
124         struct regulator_config cfg = {};
125
126         const struct of_device_id *match;
127         const struct labibb_regulator_data *reg_data;
128         struct regmap *reg_regmap;
129         unsigned int type;
130         int ret;
131
132         reg_regmap = dev_get_regmap(pdev->dev.parent, NULL);
133         if (!reg_regmap) {
134                 dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
135                 return -ENODEV;
136         }
137
138         match = of_match_device(qcom_labibb_match, &pdev->dev);
139         if (!match)
140                 return -ENODEV;
141
142         for (reg_data = match->data; reg_data->name; reg_data++) {
143
144                 /* Validate if the type of regulator is indeed
145                  * what's mentioned in DT.
146                  */
147                 ret = regmap_read(reg_regmap, reg_data->base + REG_PERPH_TYPE,
148                                   &type);
149                 if (ret < 0) {
150                         dev_err(dev,
151                                 "Peripheral type read failed ret=%d\n",
152                                 ret);
153                         return -EINVAL;
154                 }
155
156                 if (WARN_ON((type != QCOM_LAB_TYPE) && (type != QCOM_IBB_TYPE)) ||
157                     WARN_ON(type != reg_data->type))
158                         return -EINVAL;
159
160                 vreg  = devm_kzalloc(&pdev->dev, sizeof(*vreg),
161                                            GFP_KERNEL);
162                 if (!vreg)
163                         return -ENOMEM;
164
165                 vreg->regmap = reg_regmap;
166                 vreg->dev = dev;
167                 vreg->base = reg_data->base;
168                 vreg->type = reg_data->type;
169
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;
173
174                 cfg.dev = vreg->dev;
175                 cfg.driver_data = vreg;
176                 cfg.regmap = vreg->regmap;
177
178                 vreg->rdev = devm_regulator_register(vreg->dev, &vreg->desc,
179                                                         &cfg);
180
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);
185                 }
186         }
187
188         return 0;
189 }
190
191 static struct platform_driver qcom_labibb_regulator_driver = {
192         .driver = {
193                 .name = "qcom-lab-ibb-regulator",
194                 .of_match_table = qcom_labibb_match,
195         },
196         .probe = qcom_labibb_regulator_probe,
197 };
198 module_platform_driver(qcom_labibb_regulator_driver);
199
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");