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 = rdev_get_drvdata(rdev);
104 /* cannot enable more than one regulator at one time */
105 mutex_lock(&priv->enable_mutex);
107 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
108 rdev->desc->enable_mask,
109 rdev->desc->enable_mask);
111 /* Avoid powering up multiple devices at the same time */
112 usleep_range(rdev->desc->off_on_delay, rdev->desc->off_on_delay + 60);
114 mutex_unlock(&priv->enable_mutex);
119 static unsigned int hi6421_spmi_regulator_get_mode(struct regulator_dev *rdev)
121 struct hi6421_spmi_reg_info *sreg;
122 unsigned int reg_val;
124 sreg = container_of(rdev->desc, struct hi6421_spmi_reg_info, desc);
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;
139 sreg = container_of(rdev->desc, struct hi6421_spmi_reg_info, desc);
141 case REGULATOR_MODE_NORMAL:
144 case REGULATOR_MODE_IDLE:
145 if (!sreg->eco_mode_mask)
148 val = sreg->eco_mode_mask;
154 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
155 sreg->eco_mode_mask, val);
159 hi6421_spmi_regulator_get_optimum_mode(struct regulator_dev *rdev,
160 int input_uV, int output_uV,
163 struct hi6421_spmi_reg_info *sreg;
165 sreg = container_of(rdev->desc, struct hi6421_spmi_reg_info, desc);
167 if (!sreg->eco_uA || ((unsigned int)load_uA > sreg->eco_uA))
168 return REGULATOR_MODE_NORMAL;
170 return REGULATOR_MODE_IDLE;
173 static const struct regulator_ops hi6421_spmi_ldo_rops = {
174 .is_enabled = regulator_is_enabled_regmap,
175 .enable = hi6421_spmi_regulator_enable,
176 .disable = regulator_disable_regmap,
177 .list_voltage = regulator_list_voltage_table,
178 .map_voltage = regulator_map_voltage_ascend,
179 .get_voltage_sel = regulator_get_voltage_sel_regmap,
180 .set_voltage_sel = regulator_set_voltage_sel_regmap,
181 .get_mode = hi6421_spmi_regulator_get_mode,
182 .set_mode = hi6421_spmi_regulator_set_mode,
183 .get_optimum_mode = hi6421_spmi_regulator_get_optimum_mode,
186 /* HI6421v600 regulators with known registers */
187 enum hi6421_spmi_regulator_id {
198 static struct hi6421_spmi_reg_info regulator_info[] = {
199 HI6421V600_LDO(LDO3, ldo3_voltages,
203 HI6421V600_LDO(LDO4, ldo4_voltages,
207 HI6421V600_LDO(LDO9, ldo9_voltages,
211 HI6421V600_LDO(LDO15, ldo15_voltages,
215 HI6421V600_LDO(LDO16, ldo15_voltages,
219 HI6421V600_LDO(LDO17, ldo17_voltages,
223 HI6421V600_LDO(LDO33, ldo17_voltages,
227 HI6421V600_LDO(LDO34, ldo34_voltages,
233 static int hi6421_spmi_regulator_probe(struct platform_device *pdev)
235 struct device *pmic_dev = pdev->dev.parent;
236 struct regulator_config config = { };
237 struct hi6421_spmi_reg_priv *priv;
238 struct hi6421_spmi_reg_info *info;
239 struct device *dev = &pdev->dev;
240 struct hi6421_spmi_pmic *pmic;
241 struct regulator_dev *rdev;
245 * This driver is meant to be called by hi6421-spmi-core,
246 * which should first set drvdata. If this doesn't happen, hit
247 * a warn on and return.
249 pmic = dev_get_drvdata(pmic_dev);
253 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
257 mutex_init(&priv->enable_mutex);
259 for (i = 0; i < ARRAY_SIZE(regulator_info); i++) {
260 info = ®ulator_info[i];
262 config.dev = pdev->dev.parent;
263 config.driver_data = priv;
264 config.regmap = pmic->regmap;
266 rdev = devm_regulator_register(dev, &info->desc, &config);
268 dev_err(dev, "failed to register %s\n",
270 return PTR_ERR(rdev);
277 static const struct platform_device_id hi6421_spmi_regulator_table[] = {
278 { .name = "hi6421v600-regulator" },
281 MODULE_DEVICE_TABLE(platform, hi6421_spmi_regulator_table);
283 static struct platform_driver hi6421_spmi_regulator_driver = {
284 .id_table = hi6421_spmi_regulator_table,
286 .name = "hi6421v600-regulator",
288 .probe = hi6421_spmi_regulator_probe,
290 module_platform_driver(hi6421_spmi_regulator_driver);
292 MODULE_DESCRIPTION("Hi6421v600 SPMI regulator driver");
293 MODULE_LICENSE("GPL v2");