Merge tag 'thermal-v5.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/therma...
[linux-2.6-microblaze.git] / drivers / regulator / hi6421v600-regulator.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Device driver for regulators in Hisi IC
4 //
5 // Copyright (c) 2013 Linaro Ltd.
6 // Copyright (c) 2011 HiSilicon Ltd.
7 // Copyright (c) 2020-2021 Huawei Technologies Co., Ltd
8 //
9 // Guodong Xu <guodong.xu@linaro.org>
10
11 #include <linux/delay.h>
12 #include <linux/mfd/hi6421-spmi-pmic.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/spmi.h>
18
19 struct hi6421_spmi_reg_priv {
20         /* Serialize regulator enable logic */
21         struct mutex enable_mutex;
22 };
23
24 struct hi6421_spmi_reg_info {
25         struct regulator_desc   desc;
26         u8                      eco_mode_mask;
27         u32                     eco_uA;
28 };
29
30 static const unsigned int ldo3_voltages[] = {
31         1500000, 1550000, 1600000, 1650000,
32         1700000, 1725000, 1750000, 1775000,
33         1800000, 1825000, 1850000, 1875000,
34         1900000, 1925000, 1950000, 2000000
35 };
36
37 static const unsigned int ldo4_voltages[] = {
38         1725000, 1750000, 1775000, 1800000,
39         1825000, 1850000, 1875000, 1900000
40 };
41
42 static const unsigned int ldo9_voltages[] = {
43         1750000, 1800000, 1825000, 2800000,
44         2850000, 2950000, 3000000, 3300000
45 };
46
47 static const unsigned int ldo15_voltages[] = {
48         1800000, 1850000, 2400000, 2600000,
49         2700000, 2850000, 2950000, 3000000
50 };
51
52 static const unsigned int ldo17_voltages[] = {
53         2500000, 2600000, 2700000, 2800000,
54         3000000, 3100000, 3200000, 3300000
55 };
56
57 static const unsigned int ldo34_voltages[] = {
58         2600000, 2700000, 2800000, 2900000,
59         3000000, 3100000, 3200000, 3300000
60 };
61
62 /**
63  * HI6421V600_LDO() - specify a LDO power line
64  * @_id: LDO id name string
65  * @vtable: voltage table
66  * @ereg: enable register
67  * @emask: enable mask
68  * @vreg: voltage select register
69  * @odelay: off/on delay time in uS
70  * @etime: enable time in uS
71  * @ecomask: eco mode mask
72  * @ecoamp: eco mode load uppler limit in uA
73  */
74 #define HI6421V600_LDO(_id, vtable, ereg, emask, vreg,                         \
75                        odelay, etime, ecomask, ecoamp)                         \
76         [HI6421V600_##_id] = {                                                 \
77                 .desc = {                                                      \
78                         .name           = #_id,                                \
79                         .of_match        = of_match_ptr(#_id),                 \
80                         .regulators_node = of_match_ptr("regulators"),         \
81                         .ops            = &hi6421_spmi_ldo_rops,               \
82                         .type           = REGULATOR_VOLTAGE,                   \
83                         .id             = HI6421V600_##_id,                    \
84                         .owner          = THIS_MODULE,                         \
85                         .volt_table     = vtable,                              \
86                         .n_voltages     = ARRAY_SIZE(vtable),                  \
87                         .vsel_mask      = ARRAY_SIZE(vtable) - 1,              \
88                         .vsel_reg       = vreg,                                \
89                         .enable_reg     = ereg,                                \
90                         .enable_mask    = emask,                               \
91                         .enable_time    = etime,                               \
92                         .ramp_delay     = etime,                               \
93                         .off_on_delay   = odelay,                              \
94                 },                                                             \
95                 .eco_mode_mask          = ecomask,                             \
96                 .eco_uA                 = ecoamp,                              \
97         }
98
99 static int hi6421_spmi_regulator_enable(struct regulator_dev *rdev)
100 {
101         struct hi6421_spmi_reg_priv *priv;
102         int ret;
103
104         priv = dev_get_drvdata(rdev->dev.parent);
105         /* cannot enable more than one regulator at one time */
106         mutex_lock(&priv->enable_mutex);
107
108         ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
109                                  rdev->desc->enable_mask,
110                                  rdev->desc->enable_mask);
111
112         /* Avoid powering up multiple devices at the same time */
113         usleep_range(rdev->desc->off_on_delay, rdev->desc->off_on_delay + 60);
114
115         mutex_unlock(&priv->enable_mutex);
116
117         return ret;
118 }
119
120 static unsigned int hi6421_spmi_regulator_get_mode(struct regulator_dev *rdev)
121 {
122         struct hi6421_spmi_reg_info *sreg = rdev_get_drvdata(rdev);
123         unsigned int reg_val;
124
125         regmap_read(rdev->regmap, rdev->desc->enable_reg, &reg_val);
126
127         if (reg_val & sreg->eco_mode_mask)
128                 return REGULATOR_MODE_IDLE;
129
130         return REGULATOR_MODE_NORMAL;
131 }
132
133 static int hi6421_spmi_regulator_set_mode(struct regulator_dev *rdev,
134                                           unsigned int mode)
135 {
136         struct hi6421_spmi_reg_info *sreg = rdev_get_drvdata(rdev);
137         unsigned int val;
138
139         switch (mode) {
140         case REGULATOR_MODE_NORMAL:
141                 val = 0;
142                 break;
143         case REGULATOR_MODE_IDLE:
144                 if (!sreg->eco_mode_mask)
145                         return -EINVAL;
146
147                 val = sreg->eco_mode_mask;
148                 break;
149         default:
150                 return -EINVAL;
151         }
152
153         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
154                                   sreg->eco_mode_mask, val);
155 }
156
157 static unsigned int
158 hi6421_spmi_regulator_get_optimum_mode(struct regulator_dev *rdev,
159                                        int input_uV, int output_uV,
160                                        int load_uA)
161 {
162         struct hi6421_spmi_reg_info *sreg = rdev_get_drvdata(rdev);
163
164         if (!sreg->eco_uA || ((unsigned int)load_uA > sreg->eco_uA))
165                 return REGULATOR_MODE_NORMAL;
166
167         return REGULATOR_MODE_IDLE;
168 }
169
170 static const struct regulator_ops hi6421_spmi_ldo_rops = {
171         .is_enabled = regulator_is_enabled_regmap,
172         .enable = hi6421_spmi_regulator_enable,
173         .disable = regulator_disable_regmap,
174         .list_voltage = regulator_list_voltage_table,
175         .map_voltage = regulator_map_voltage_ascend,
176         .get_voltage_sel = regulator_get_voltage_sel_regmap,
177         .set_voltage_sel = regulator_set_voltage_sel_regmap,
178         .get_mode = hi6421_spmi_regulator_get_mode,
179         .set_mode = hi6421_spmi_regulator_set_mode,
180         .get_optimum_mode = hi6421_spmi_regulator_get_optimum_mode,
181 };
182
183 /* HI6421v600 regulators with known registers */
184 enum hi6421_spmi_regulator_id {
185         HI6421V600_LDO3,
186         HI6421V600_LDO4,
187         HI6421V600_LDO9,
188         HI6421V600_LDO15,
189         HI6421V600_LDO16,
190         HI6421V600_LDO17,
191         HI6421V600_LDO33,
192         HI6421V600_LDO34,
193 };
194
195 static struct hi6421_spmi_reg_info regulator_info[] = {
196         HI6421V600_LDO(LDO3, ldo3_voltages,
197                        0x16, 0x01, 0x51,
198                        20000, 120,
199                        0, 0),
200         HI6421V600_LDO(LDO4, ldo4_voltages,
201                        0x17, 0x01, 0x52,
202                        20000, 120,
203                        0x10, 10000),
204         HI6421V600_LDO(LDO9, ldo9_voltages,
205                        0x1c, 0x01, 0x57,
206                        20000, 360,
207                        0x10, 10000),
208         HI6421V600_LDO(LDO15, ldo15_voltages,
209                        0x21, 0x01, 0x5c,
210                        20000, 360,
211                        0x10, 10000),
212         HI6421V600_LDO(LDO16, ldo15_voltages,
213                        0x22, 0x01, 0x5d,
214                        20000, 360,
215                        0x10, 10000),
216         HI6421V600_LDO(LDO17, ldo17_voltages,
217                        0x23, 0x01, 0x5e,
218                        20000, 120,
219                        0x10, 10000),
220         HI6421V600_LDO(LDO33, ldo17_voltages,
221                        0x32, 0x01, 0x6d,
222                        20000, 120,
223                        0, 0),
224         HI6421V600_LDO(LDO34, ldo34_voltages,
225                        0x33, 0x01, 0x6e,
226                        20000, 120,
227                        0, 0),
228 };
229
230 static int hi6421_spmi_regulator_probe(struct platform_device *pdev)
231 {
232         struct device *pmic_dev = pdev->dev.parent;
233         struct regulator_config config = { };
234         struct hi6421_spmi_reg_priv *priv;
235         struct hi6421_spmi_reg_info *info;
236         struct device *dev = &pdev->dev;
237         struct hi6421_spmi_pmic *pmic;
238         struct regulator_dev *rdev;
239         int i;
240
241         /*
242          * This driver is meant to be called by hi6421-spmi-core,
243          * which should first set drvdata. If this doesn't happen, hit
244          * a warn on and return.
245          */
246         pmic = dev_get_drvdata(pmic_dev);
247         if (WARN_ON(!pmic))
248                 return -ENODEV;
249
250         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
251         if (!priv)
252                 return -ENOMEM;
253
254         mutex_init(&priv->enable_mutex);
255         platform_set_drvdata(pdev, priv);
256
257         for (i = 0; i < ARRAY_SIZE(regulator_info); i++) {
258                 info = &regulator_info[i];
259
260                 config.dev = pdev->dev.parent;
261                 config.driver_data = info;
262                 config.regmap = pmic->regmap;
263
264                 rdev = devm_regulator_register(dev, &info->desc, &config);
265                 if (IS_ERR(rdev)) {
266                         dev_err(dev, "failed to register %s\n",
267                                 info->desc.name);
268                         return PTR_ERR(rdev);
269                 }
270         }
271
272         return 0;
273 }
274
275 static const struct platform_device_id hi6421_spmi_regulator_table[] = {
276         { .name = "hi6421v600-regulator" },
277         {},
278 };
279 MODULE_DEVICE_TABLE(platform, hi6421_spmi_regulator_table);
280
281 static struct platform_driver hi6421_spmi_regulator_driver = {
282         .id_table = hi6421_spmi_regulator_table,
283         .driver = {
284                 .name = "hi6421v600-regulator",
285         },
286         .probe  = hi6421_spmi_regulator_probe,
287 };
288 module_platform_driver(hi6421_spmi_regulator_driver);
289
290 MODULE_DESCRIPTION("Hi6421v600 SPMI regulator driver");
291 MODULE_LICENSE("GPL v2");
292