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/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/of_regulator.h>
18 #include <linux/i2c.h>
19 #include <linux/regmap.h>
22 #define MPQ7920_BUCK_VOLT_RANGE \
23 ((MPQ7920_VOLT_MAX - MPQ7920_BUCK_VOLT_MIN)/MPQ7920_VOLT_STEP + 1)
24 #define MPQ7920_LDO_VOLT_RANGE \
25 ((MPQ7920_VOLT_MAX - MPQ7920_LDO_VOLT_MIN)/MPQ7920_VOLT_STEP + 1)
27 #define MPQ7920BUCK(_name, _id, _ilim) \
28 [MPQ7920_BUCK ## _id] = { \
29 .id = MPQ7920_BUCK ## _id, \
32 .regulators_node = "regulators", \
33 .of_parse_cb = mpq7920_parse_cb, \
34 .ops = &mpq7920_buck_ops, \
35 .min_uV = MPQ7920_BUCK_VOLT_MIN, \
36 .uV_step = MPQ7920_VOLT_STEP, \
37 .n_voltages = MPQ7920_BUCK_VOLT_RANGE, \
38 .curr_table = _ilim, \
39 .n_current_limits = ARRAY_SIZE(_ilim), \
40 .csel_reg = MPQ7920_BUCK ##_id## _REG_C, \
41 .csel_mask = MPQ7920_MASK_BUCK_ILIM, \
42 .enable_reg = MPQ7920_REG_REGULATOR_EN, \
43 .enable_mask = BIT(MPQ7920_REGULATOR_EN_OFFSET - \
44 MPQ7920_BUCK ## _id), \
45 .vsel_reg = MPQ7920_BUCK ##_id## _REG_A, \
46 .vsel_mask = MPQ7920_MASK_VREF, \
47 .active_discharge_on = MPQ7920_DISCHARGE_ON, \
48 .active_discharge_reg = MPQ7920_BUCK ##_id## _REG_B, \
49 .active_discharge_mask = MPQ7920_MASK_DISCHARGE, \
50 .soft_start_reg = MPQ7920_BUCK ##_id## _REG_C, \
51 .soft_start_mask = MPQ7920_MASK_SOFTSTART, \
52 .owner = THIS_MODULE, \
55 #define MPQ7920LDO(_name, _id, _ops, _ilim, _ilim_sz, _creg, _cmask) \
56 [MPQ7920_LDO ## _id] = { \
57 .id = MPQ7920_LDO ## _id, \
60 .regulators_node = "regulators", \
62 .min_uV = MPQ7920_LDO_VOLT_MIN, \
63 .uV_step = MPQ7920_VOLT_STEP, \
64 .n_voltages = MPQ7920_LDO_VOLT_RANGE, \
65 .vsel_reg = MPQ7920_LDO ##_id## _REG_A, \
66 .vsel_mask = MPQ7920_MASK_VREF, \
67 .curr_table = _ilim, \
68 .n_current_limits = _ilim_sz, \
70 .csel_mask = _cmask, \
71 .enable_reg = (_id == 1) ? 0 : MPQ7920_REG_REGULATOR_EN,\
72 .enable_mask = BIT(MPQ7920_REGULATOR_EN_OFFSET - \
73 MPQ7920_LDO ##_id + 1), \
74 .active_discharge_on = MPQ7920_DISCHARGE_ON, \
75 .active_discharge_mask = MPQ7920_MASK_DISCHARGE, \
76 .active_discharge_reg = MPQ7920_LDO ##_id## _REG_B, \
77 .type = REGULATOR_VOLTAGE, \
78 .owner = THIS_MODULE, \
81 enum mpq7920_regulators
{
86 MPQ7920_LDO1
, /* LDORTC */
91 MPQ7920_MAX_REGULATORS
,
94 struct mpq7920_regulator_info
{
95 struct regmap
*regmap
;
96 struct regulator_desc
*rdesc
;
99 static const struct regmap_config mpq7920_regmap_config
= {
102 .max_register
= 0x25,
105 /* Current limits array (in uA)
108 static const unsigned int mpq7920_I_limits1
[] = {
109 4600000, 6600000, 7600000, 9300000
113 static const unsigned int mpq7920_I_limits2
[] = {
114 2700000, 3900000, 5100000, 6100000
118 static const unsigned int mpq7920_I_limits3
[] = {
122 static int mpq7920_set_ramp_delay(struct regulator_dev
*rdev
, int ramp_delay
);
123 static int mpq7920_parse_cb(struct device_node
*np
,
124 const struct regulator_desc
*rdesc
,
125 struct regulator_config
*config
);
127 /* RTCLDO not controllable, always ON */
128 static const struct regulator_ops mpq7920_ldortc_ops
= {
129 .list_voltage
= regulator_list_voltage_linear
,
130 .map_voltage
= regulator_map_voltage_linear
,
131 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
132 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
135 static const struct regulator_ops mpq7920_ldo_wo_current_ops
= {
136 .enable
= regulator_enable_regmap
,
137 .disable
= regulator_disable_regmap
,
138 .is_enabled
= regulator_is_enabled_regmap
,
139 .list_voltage
= regulator_list_voltage_linear
,
140 .map_voltage
= regulator_map_voltage_linear
,
141 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
142 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
143 .set_active_discharge
= regulator_set_active_discharge_regmap
,
146 static const struct regulator_ops mpq7920_ldo_ops
= {
147 .enable
= regulator_enable_regmap
,
148 .disable
= regulator_disable_regmap
,
149 .is_enabled
= regulator_is_enabled_regmap
,
150 .list_voltage
= regulator_list_voltage_linear
,
151 .map_voltage
= regulator_map_voltage_linear
,
152 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
153 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
154 .set_active_discharge
= regulator_set_active_discharge_regmap
,
155 .get_current_limit
= regulator_get_current_limit_regmap
,
156 .set_current_limit
= regulator_set_current_limit_regmap
,
159 static const struct regulator_ops mpq7920_buck_ops
= {
160 .enable
= regulator_enable_regmap
,
161 .disable
= regulator_disable_regmap
,
162 .is_enabled
= regulator_is_enabled_regmap
,
163 .list_voltage
= regulator_list_voltage_linear
,
164 .map_voltage
= regulator_map_voltage_linear
,
165 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
166 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
167 .set_active_discharge
= regulator_set_active_discharge_regmap
,
168 .set_soft_start
= regulator_set_soft_start_regmap
,
169 .set_ramp_delay
= mpq7920_set_ramp_delay
,
172 static struct regulator_desc mpq7920_regulators_desc
[MPQ7920_MAX_REGULATORS
] = {
173 MPQ7920BUCK("buck1", 1, mpq7920_I_limits1
),
174 MPQ7920BUCK("buck2", 2, mpq7920_I_limits2
),
175 MPQ7920BUCK("buck3", 3, mpq7920_I_limits1
),
176 MPQ7920BUCK("buck4", 4, mpq7920_I_limits2
),
177 MPQ7920LDO("ldortc", 1, &mpq7920_ldortc_ops
, NULL
, 0, 0, 0),
178 MPQ7920LDO("ldo2", 2, &mpq7920_ldo_wo_current_ops
, NULL
, 0, 0, 0),
179 MPQ7920LDO("ldo3", 3, &mpq7920_ldo_wo_current_ops
, NULL
, 0, 0, 0),
180 MPQ7920LDO("ldo4", 4, &mpq7920_ldo_ops
, mpq7920_I_limits3
,
181 ARRAY_SIZE(mpq7920_I_limits3
), MPQ7920_LDO4_REG_B
,
182 MPQ7920_MASK_LDO_ILIM
),
183 MPQ7920LDO("ldo5", 5, &mpq7920_ldo_ops
, mpq7920_I_limits3
,
184 ARRAY_SIZE(mpq7920_I_limits3
), MPQ7920_LDO5_REG_B
,
185 MPQ7920_MASK_LDO_ILIM
),
189 * DVS ramp rate BUCK1 to BUCK4
194 static int mpq7920_set_ramp_delay(struct regulator_dev
*rdev
, int ramp_delay
)
196 unsigned int ramp_val
;
198 if (ramp_delay
> 8000 || ramp_delay
< 0)
201 if (ramp_delay
<= 4000)
206 return regmap_update_bits(rdev
->regmap
, MPQ7920_REG_CTL0
,
207 MPQ7920_MASK_DVS_SLEWRATE
, ramp_val
<< 6);
210 static int mpq7920_parse_cb(struct device_node
*np
,
211 const struct regulator_desc
*desc
,
212 struct regulator_config
*config
)
216 struct mpq7920_regulator_info
*info
= config
->driver_data
;
217 struct regulator_desc
*rdesc
= &info
->rdesc
[desc
->id
];
219 if (of_property_read_bool(np
, "mps,buck-ovp-disable")) {
220 regmap_update_bits(config
->regmap
,
221 MPQ7920_BUCK1_REG_B
+ (rdesc
->id
* 4),
222 MPQ7920_MASK_OVP
, MPQ7920_OVP_DISABLE
);
225 ret
= of_property_read_u8(np
, "mps,buck-phase-delay", &val
);
227 regmap_update_bits(config
->regmap
,
228 MPQ7920_BUCK1_REG_C
+ (rdesc
->id
* 4),
229 MPQ7920_MASK_BUCK_PHASE_DEALY
,
233 ret
= of_property_read_u8(np
, "mps,buck-softstart", &val
);
235 rdesc
->soft_start_val_on
= (val
& 3) << 2;
240 static void mpq7920_parse_dt(struct device
*dev
,
241 struct mpq7920_regulator_info
*info
)
244 struct device_node
*np
= dev
->of_node
;
247 np
= of_get_child_by_name(np
, "regulators");
249 dev_err(dev
, "missing 'regulators' subnode in DT\n");
253 ret
= of_property_read_u8(np
, "mps,switch-freq", &freq
);
255 regmap_update_bits(info
->regmap
, MPQ7920_REG_CTL0
,
256 MPQ7920_MASK_SWITCH_FREQ
,
263 static int mpq7920_i2c_probe(struct i2c_client
*client
)
265 struct device
*dev
= &client
->dev
;
266 struct mpq7920_regulator_info
*info
;
267 struct regulator_config config
= { NULL
, };
268 struct regulator_dev
*rdev
;
269 struct regmap
*regmap
;
272 info
= devm_kzalloc(dev
, sizeof(struct mpq7920_regulator_info
),
277 info
->rdesc
= mpq7920_regulators_desc
;
278 regmap
= devm_regmap_init_i2c(client
, &mpq7920_regmap_config
);
279 if (IS_ERR(regmap
)) {
280 dev_err(dev
, "Failed to allocate regmap!\n");
281 return PTR_ERR(regmap
);
284 i2c_set_clientdata(client
, info
);
285 info
->regmap
= regmap
;
286 if (client
->dev
.of_node
)
287 mpq7920_parse_dt(&client
->dev
, info
);
290 config
.regmap
= regmap
;
291 config
.driver_data
= info
;
293 for (i
= 0; i
< MPQ7920_MAX_REGULATORS
; i
++) {
294 rdev
= devm_regulator_register(dev
,
295 &mpq7920_regulators_desc
[i
],
298 dev_err(dev
, "Failed to register regulator!\n");
299 return PTR_ERR(rdev
);
306 static const struct of_device_id mpq7920_of_match
[] = {
307 { .compatible
= "mps,mpq7920"},
310 MODULE_DEVICE_TABLE(of
, mpq7920_of_match
);
312 static const struct i2c_device_id mpq7920_id
[] = {
316 MODULE_DEVICE_TABLE(i2c
, mpq7920_id
);
318 static struct i2c_driver mpq7920_regulator_driver
= {
321 .of_match_table
= of_match_ptr(mpq7920_of_match
),
323 .probe_new
= 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");