1 // SPDX-License-Identifier: GPL-2.0+
3 // mpq7920.c - regulator driver for mps mpq7920
5 // Copyright 2019 Monolithic Power Systems, Inc
7 // Author: Saravanan Sekar <sravanhome@gmail.com>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/err.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/of_regulator.h>
17 #include <linux/i2c.h>
18 #include <linux/regmap.h>
21 #define MPQ7920_BUCK_VOLT_RANGE \
22 ((MPQ7920_VOLT_MAX - MPQ7920_BUCK_VOLT_MIN)/MPQ7920_VOLT_STEP + 1)
23 #define MPQ7920_LDO_VOLT_RANGE \
24 ((MPQ7920_VOLT_MAX - MPQ7920_LDO_VOLT_MIN)/MPQ7920_VOLT_STEP + 1)
26 #define MPQ7920BUCK(_name, _id, _ilim) \
27 [MPQ7920_BUCK ## _id] = { \
28 .id = MPQ7920_BUCK ## _id, \
31 .regulators_node = "regulators", \
32 .of_parse_cb = mpq7920_parse_cb, \
33 .ops = &mpq7920_buck_ops, \
34 .min_uV = MPQ7920_BUCK_VOLT_MIN, \
35 .uV_step = MPQ7920_VOLT_STEP, \
36 .n_voltages = MPQ7920_BUCK_VOLT_RANGE, \
37 .curr_table = _ilim, \
38 .n_current_limits = ARRAY_SIZE(_ilim), \
39 .csel_reg = MPQ7920_BUCK ##_id## _REG_C, \
40 .csel_mask = MPQ7920_MASK_BUCK_ILIM, \
41 .enable_reg = MPQ7920_REG_REGULATOR_EN, \
42 .enable_mask = BIT(MPQ7920_REGULATOR_EN_OFFSET - \
43 MPQ7920_BUCK ## _id), \
44 .vsel_reg = MPQ7920_BUCK ##_id## _REG_A, \
45 .vsel_mask = MPQ7920_MASK_VREF, \
46 .active_discharge_on = MPQ7920_DISCHARGE_ON, \
47 .active_discharge_reg = MPQ7920_BUCK ##_id## _REG_B, \
48 .active_discharge_mask = MPQ7920_MASK_DISCHARGE, \
49 .soft_start_reg = MPQ7920_BUCK ##_id## _REG_C, \
50 .soft_start_mask = MPQ7920_MASK_SOFTSTART, \
51 .owner = THIS_MODULE, \
54 #define MPQ7920LDO(_name, _id, _ops, _ilim, _ilim_sz, _creg, _cmask) \
55 [MPQ7920_LDO ## _id] = { \
56 .id = MPQ7920_LDO ## _id, \
59 .regulators_node = "regulators", \
61 .min_uV = MPQ7920_LDO_VOLT_MIN, \
62 .uV_step = MPQ7920_VOLT_STEP, \
63 .n_voltages = MPQ7920_LDO_VOLT_RANGE, \
64 .vsel_reg = MPQ7920_LDO ##_id## _REG_A, \
65 .vsel_mask = MPQ7920_MASK_VREF, \
66 .curr_table = _ilim, \
67 .n_current_limits = _ilim_sz, \
69 .csel_mask = _cmask, \
70 .enable_reg = (_id == 1) ? 0 : MPQ7920_REG_REGULATOR_EN,\
71 .enable_mask = BIT(MPQ7920_REGULATOR_EN_OFFSET - \
72 MPQ7920_LDO ##_id + 1), \
73 .active_discharge_on = MPQ7920_DISCHARGE_ON, \
74 .active_discharge_mask = MPQ7920_MASK_DISCHARGE, \
75 .active_discharge_reg = MPQ7920_LDO ##_id## _REG_B, \
76 .type = REGULATOR_VOLTAGE, \
77 .owner = THIS_MODULE, \
80 enum mpq7920_regulators {
85 MPQ7920_LDO1, /* LDORTC */
90 MPQ7920_MAX_REGULATORS,
93 struct mpq7920_regulator_info {
94 struct regmap *regmap;
95 struct regulator_desc *rdesc;
98 static const struct regmap_config mpq7920_regmap_config = {
101 .max_register = 0x25,
104 /* Current limits array (in uA)
107 static const unsigned int mpq7920_I_limits1[] = {
108 4600000, 6600000, 7600000, 9300000
112 static const unsigned int mpq7920_I_limits2[] = {
113 2700000, 3900000, 5100000, 6100000
117 static const unsigned int mpq7920_I_limits3[] = {
121 static int mpq7920_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay);
122 static int mpq7920_parse_cb(struct device_node *np,
123 const struct regulator_desc *rdesc,
124 struct regulator_config *config);
126 /* RTCLDO not controllable, always ON */
127 static const struct regulator_ops mpq7920_ldortc_ops = {
128 .list_voltage = regulator_list_voltage_linear,
129 .map_voltage = regulator_map_voltage_linear,
130 .get_voltage_sel = regulator_get_voltage_sel_regmap,
131 .set_voltage_sel = regulator_set_voltage_sel_regmap,
134 static const struct regulator_ops mpq7920_ldo_wo_current_ops = {
135 .enable = regulator_enable_regmap,
136 .disable = regulator_disable_regmap,
137 .is_enabled = regulator_is_enabled_regmap,
138 .list_voltage = regulator_list_voltage_linear,
139 .map_voltage = regulator_map_voltage_linear,
140 .get_voltage_sel = regulator_get_voltage_sel_regmap,
141 .set_voltage_sel = regulator_set_voltage_sel_regmap,
142 .set_active_discharge = regulator_set_active_discharge_regmap,
145 static const struct regulator_ops mpq7920_ldo_ops = {
146 .enable = regulator_enable_regmap,
147 .disable = regulator_disable_regmap,
148 .is_enabled = regulator_is_enabled_regmap,
149 .list_voltage = regulator_list_voltage_linear,
150 .map_voltage = regulator_map_voltage_linear,
151 .get_voltage_sel = regulator_get_voltage_sel_regmap,
152 .set_voltage_sel = regulator_set_voltage_sel_regmap,
153 .set_active_discharge = regulator_set_active_discharge_regmap,
154 .get_current_limit = regulator_get_current_limit_regmap,
155 .set_current_limit = regulator_set_current_limit_regmap,
158 static const struct regulator_ops mpq7920_buck_ops = {
159 .enable = regulator_enable_regmap,
160 .disable = regulator_disable_regmap,
161 .is_enabled = regulator_is_enabled_regmap,
162 .list_voltage = regulator_list_voltage_linear,
163 .map_voltage = regulator_map_voltage_linear,
164 .get_voltage_sel = regulator_get_voltage_sel_regmap,
165 .set_voltage_sel = regulator_set_voltage_sel_regmap,
166 .set_active_discharge = regulator_set_active_discharge_regmap,
167 .set_soft_start = regulator_set_soft_start_regmap,
168 .set_ramp_delay = mpq7920_set_ramp_delay,
171 static struct regulator_desc mpq7920_regulators_desc[MPQ7920_MAX_REGULATORS] = {
172 MPQ7920BUCK("buck1", 1, mpq7920_I_limits1),
173 MPQ7920BUCK("buck2", 2, mpq7920_I_limits2),
174 MPQ7920BUCK("buck3", 3, mpq7920_I_limits1),
175 MPQ7920BUCK("buck4", 4, mpq7920_I_limits2),
176 MPQ7920LDO("ldortc", 1, &mpq7920_ldortc_ops, NULL, 0, 0, 0),
177 MPQ7920LDO("ldo2", 2, &mpq7920_ldo_wo_current_ops, NULL, 0, 0, 0),
178 MPQ7920LDO("ldo3", 3, &mpq7920_ldo_wo_current_ops, NULL, 0, 0, 0),
179 MPQ7920LDO("ldo4", 4, &mpq7920_ldo_ops, mpq7920_I_limits3,
180 ARRAY_SIZE(mpq7920_I_limits3), MPQ7920_LDO4_REG_B,
181 MPQ7920_MASK_LDO_ILIM),
182 MPQ7920LDO("ldo5", 5, &mpq7920_ldo_ops, mpq7920_I_limits3,
183 ARRAY_SIZE(mpq7920_I_limits3), MPQ7920_LDO5_REG_B,
184 MPQ7920_MASK_LDO_ILIM),
188 * DVS ramp rate BUCK1 to BUCK4
193 static int mpq7920_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
195 unsigned int ramp_val;
197 if (ramp_delay > 8000 || ramp_delay < 0)
200 if (ramp_delay <= 4000)
205 return regmap_update_bits(rdev->regmap, MPQ7920_REG_CTL0,
206 MPQ7920_MASK_DVS_SLEWRATE, ramp_val << 6);
209 static int mpq7920_parse_cb(struct device_node *np,
210 const struct regulator_desc *desc,
211 struct regulator_config *config)
215 struct mpq7920_regulator_info *info = config->driver_data;
216 struct regulator_desc *rdesc = &info->rdesc[desc->id];
218 if (of_property_read_bool(np, "mps,buck-ovp-disable")) {
219 regmap_update_bits(config->regmap,
220 MPQ7920_BUCK1_REG_B + (rdesc->id * 4),
221 MPQ7920_MASK_OVP, MPQ7920_OVP_DISABLE);
224 ret = of_property_read_u8(np, "mps,buck-phase-delay", &val);
226 regmap_update_bits(config->regmap,
227 MPQ7920_BUCK1_REG_C + (rdesc->id * 4),
228 MPQ7920_MASK_BUCK_PHASE_DEALY,
232 ret = of_property_read_u8(np, "mps,buck-softstart", &val);
234 rdesc->soft_start_val_on = (val & 3) << 2;
239 static void mpq7920_parse_dt(struct device *dev,
240 struct mpq7920_regulator_info *info)
243 struct device_node *np = dev->of_node;
246 np = of_get_child_by_name(np, "regulators");
248 dev_err(dev, "missing 'regulators' subnode in DT\n");
252 ret = of_property_read_u8(np, "mps,switch-freq", &freq);
254 regmap_update_bits(info->regmap, MPQ7920_REG_CTL0,
255 MPQ7920_MASK_SWITCH_FREQ,
262 static int mpq7920_i2c_probe(struct i2c_client *client)
264 struct device *dev = &client->dev;
265 struct mpq7920_regulator_info *info;
266 struct regulator_config config = { NULL, };
267 struct regulator_dev *rdev;
268 struct regmap *regmap;
271 info = devm_kzalloc(dev, sizeof(struct mpq7920_regulator_info),
276 info->rdesc = mpq7920_regulators_desc;
277 regmap = devm_regmap_init_i2c(client, &mpq7920_regmap_config);
278 if (IS_ERR(regmap)) {
279 dev_err(dev, "Failed to allocate regmap!\n");
280 return PTR_ERR(regmap);
283 i2c_set_clientdata(client, info);
284 info->regmap = regmap;
285 if (client->dev.of_node)
286 mpq7920_parse_dt(&client->dev, info);
289 config.regmap = regmap;
290 config.driver_data = info;
292 for (i = 0; i < MPQ7920_MAX_REGULATORS; i++) {
293 rdev = devm_regulator_register(dev,
294 &mpq7920_regulators_desc[i],
297 dev_err(dev, "Failed to register regulator!\n");
298 return PTR_ERR(rdev);
305 static const struct of_device_id mpq7920_of_match[] = {
306 { .compatible = "mps,mpq7920"},
309 MODULE_DEVICE_TABLE(of, mpq7920_of_match);
311 static const struct i2c_device_id mpq7920_id[] = {
315 MODULE_DEVICE_TABLE(i2c, mpq7920_id);
317 static struct i2c_driver mpq7920_regulator_driver = {
320 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
321 .of_match_table = mpq7920_of_match,
323 .probe = mpq7920_i2c_probe,
324 .id_table = mpq7920_id,
326 module_i2c_driver(mpq7920_regulator_driver);
328 MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
329 MODULE_DESCRIPTION("MPQ7920 PMIC regulator driver");
330 MODULE_LICENSE("GPL");