1 // SPDX-License-Identifier: GPL-2.0
3 // Device driver for regulators in Hisi IC
5 // Copyright (c) 2013 Linaro Ltd.
6 // Copyright (c) 2011 HiSilicon Ltd.
7 // Copyright (c) 2020-2021 Huawei Technologies Co., Ltd
9 // Guodong Xu <guodong.xu@linaro.org>
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>
19 struct hi6421_spmi_reg_priv {
20 /* Serialize regulator enable logic */
21 struct mutex enable_mutex;
24 struct hi6421_spmi_reg_info {
25 struct regulator_desc desc;
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
37 static const unsigned int ldo4_voltages[] = {
38 1725000, 1750000, 1775000, 1800000,
39 1825000, 1850000, 1875000, 1900000
42 static const unsigned int ldo9_voltages[] = {
43 1750000, 1800000, 1825000, 2800000,
44 2850000, 2950000, 3000000, 3300000
47 static const unsigned int ldo15_voltages[] = {
48 1800000, 1850000, 2400000, 2600000,
49 2700000, 2850000, 2950000, 3000000
52 static const unsigned int ldo17_voltages[] = {
53 2500000, 2600000, 2700000, 2800000,
54 3000000, 3100000, 3200000, 3300000
57 static const unsigned int ldo34_voltages[] = {
58 2600000, 2700000, 2800000, 2900000,
59 3000000, 3100000, 3200000, 3300000
63 * HI6421V600_LDO() - specify a LDO power line
64 * @_id: LDO id name string
65 * @vtable: voltage table
66 * @ereg: enable register
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
74 #define HI6421V600_LDO(_id, vtable, ereg, emask, vreg, \
75 odelay, etime, ecomask, ecoamp) \
76 [HI6421V600_##_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, \
90 .enable_mask = emask, \
91 .enable_time = etime, \
92 .ramp_delay = etime, \
93 .off_on_delay = odelay, \
95 .eco_mode_mask = ecomask, \
99 static int hi6421_spmi_regulator_enable(struct regulator_dev *rdev)
101 struct hi6421_spmi_reg_priv *priv;
104 priv = dev_get_drvdata(rdev->dev.parent);
105 /* cannot enable more than one regulator at one time */
106 mutex_lock(&priv->enable_mutex);
108 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
109 rdev->desc->enable_mask,
110 rdev->desc->enable_mask);
112 /* Avoid powering up multiple devices at the same time */
113 usleep_range(rdev->desc->off_on_delay, rdev->desc->off_on_delay + 60);
115 mutex_unlock(&priv->enable_mutex);
120 static unsigned int hi6421_spmi_regulator_get_mode(struct regulator_dev *rdev)
122 struct hi6421_spmi_reg_info *sreg = rdev_get_drvdata(rdev);
123 unsigned int reg_val;
125 regmap_read(rdev->regmap, rdev->desc->enable_reg, ®_val);
127 if (reg_val & sreg->eco_mode_mask)
128 return REGULATOR_MODE_IDLE;
130 return REGULATOR_MODE_NORMAL;
133 static int hi6421_spmi_regulator_set_mode(struct regulator_dev *rdev,
136 struct hi6421_spmi_reg_info *sreg = rdev_get_drvdata(rdev);
140 case REGULATOR_MODE_NORMAL:
143 case REGULATOR_MODE_IDLE:
144 if (!sreg->eco_mode_mask)
147 val = sreg->eco_mode_mask;
153 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
154 sreg->eco_mode_mask, val);
158 hi6421_spmi_regulator_get_optimum_mode(struct regulator_dev *rdev,
159 int input_uV, int output_uV,
162 struct hi6421_spmi_reg_info *sreg = rdev_get_drvdata(rdev);
164 if (!sreg->eco_uA || ((unsigned int)load_uA > sreg->eco_uA))
165 return REGULATOR_MODE_NORMAL;
167 return REGULATOR_MODE_IDLE;
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,
183 /* HI6421v600 regulators with known registers */
184 enum hi6421_spmi_regulator_id {
195 static struct hi6421_spmi_reg_info regulator_info[] = {
196 HI6421V600_LDO(LDO3, ldo3_voltages,
200 HI6421V600_LDO(LDO4, ldo4_voltages,
204 HI6421V600_LDO(LDO9, ldo9_voltages,
208 HI6421V600_LDO(LDO15, ldo15_voltages,
212 HI6421V600_LDO(LDO16, ldo15_voltages,
216 HI6421V600_LDO(LDO17, ldo17_voltages,
220 HI6421V600_LDO(LDO33, ldo17_voltages,
224 HI6421V600_LDO(LDO34, ldo34_voltages,
230 static int hi6421_spmi_regulator_probe(struct platform_device *pdev)
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;
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.
246 pmic = dev_get_drvdata(pmic_dev);
250 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
254 mutex_init(&priv->enable_mutex);
255 platform_set_drvdata(pdev, priv);
257 for (i = 0; i < ARRAY_SIZE(regulator_info); i++) {
258 info = ®ulator_info[i];
260 config.dev = pdev->dev.parent;
261 config.driver_data = info;
262 config.regmap = pmic->regmap;
264 rdev = devm_regulator_register(dev, &info->desc, &config);
266 dev_err(dev, "failed to register %s\n",
268 return PTR_ERR(rdev);
275 static const struct platform_device_id hi6421_spmi_regulator_table[] = {
276 { .name = "hi6421v600-regulator" },
279 MODULE_DEVICE_TABLE(platform, hi6421_spmi_regulator_table);
281 static struct platform_driver hi6421_spmi_regulator_driver = {
282 .id_table = hi6421_spmi_regulator_table,
284 .name = "hi6421v600-regulator",
286 .probe = hi6421_spmi_regulator_probe,
288 module_platform_driver(hi6421_spmi_regulator_driver);
290 MODULE_DESCRIPTION("Hi6421v600 SPMI regulator driver");
291 MODULE_LICENSE("GPL v2");