Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0+
  2//
  3// mpq7920.c  - regulator driver for mps mpq7920
  4//
  5// Copyright 2019 Monolithic Power Systems, Inc
  6//
  7// Author: Saravanan Sekar <sravanhome@gmail.com>
  8
  9#include <linux/kernel.h>
 10#include <linux/module.h>
 11#include <linux/init.h>
 12#include <linux/err.h>
 13#include <linux/of.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>
 19#include "mpq7920.h"
 20
 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)
 25
 26#define MPQ7920BUCK(_name, _id, _ilim)					\
 27	[MPQ7920_BUCK ## _id] = {					\
 28		.id = MPQ7920_BUCK ## _id,				\
 29		.name = _name,						\
 30		.of_match = _name,					\
 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,			\
 52	}
 53
 54#define MPQ7920LDO(_name, _id, _ops, _ilim, _ilim_sz, _creg, _cmask)	\
 55	[MPQ7920_LDO ## _id] = {					\
 56		.id = MPQ7920_LDO ## _id,				\
 57		.name = _name,						\
 58		.of_match = _name,					\
 59		.regulators_node = "regulators",			\
 60		.ops = _ops,						\
 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,				\
 68		.csel_reg = _creg,					\
 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,			\
 78	}
 79
 80enum mpq7920_regulators {
 81	MPQ7920_BUCK1,
 82	MPQ7920_BUCK2,
 83	MPQ7920_BUCK3,
 84	MPQ7920_BUCK4,
 85	MPQ7920_LDO1, /* LDORTC */
 86	MPQ7920_LDO2,
 87	MPQ7920_LDO3,
 88	MPQ7920_LDO4,
 89	MPQ7920_LDO5,
 90	MPQ7920_MAX_REGULATORS,
 91};
 92
 93struct mpq7920_regulator_info {
 94	struct regmap *regmap;
 95	struct regulator_desc *rdesc;
 96};
 97
 98static const struct regmap_config mpq7920_regmap_config = {
 99	.reg_bits = 8,
100	.val_bits = 8,
101	.max_register = 0x25,
102};
103
104/* Current limits array (in uA)
105 * ILIM1 & ILIM3
106 */
107static const unsigned int mpq7920_I_limits1[] = {
108	4600000, 6600000, 7600000, 9300000
109};
110
111/* ILIM2 & ILIM4 */
112static const unsigned int mpq7920_I_limits2[] = {
113	2700000, 3900000, 5100000, 6100000
114};
115
116/* LDO4 & LDO5 */
117static const unsigned int mpq7920_I_limits3[] = {
118	300000, 700000
119};
120
121static int mpq7920_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay);
122static int mpq7920_parse_cb(struct device_node *np,
123				const struct regulator_desc *rdesc,
124				struct regulator_config *config);
125
126/* RTCLDO not controllable, always ON */
127static 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,
132};
133
134static 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,
143};
144
145static 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,
156};
157
158static 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,
169};
170
171static 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),
185};
186
187/*
188 * DVS ramp rate BUCK1 to BUCK4
189 * 00-01: Reserved
190 * 10: 8mV/us
191 * 11: 4mV/us
192 */
193static int mpq7920_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
194{
195	unsigned int ramp_val;
196
197	if (ramp_delay > 8000 || ramp_delay < 0)
198		return -EINVAL;
199
200	if (ramp_delay <= 4000)
201		ramp_val = 3;
202	else
203		ramp_val = 2;
204
205	return regmap_update_bits(rdev->regmap, MPQ7920_REG_CTL0,
206				  MPQ7920_MASK_DVS_SLEWRATE, ramp_val << 6);
207}
208
209static int mpq7920_parse_cb(struct device_node *np,
210				const struct regulator_desc *desc,
211				struct regulator_config *config)
212{
213	uint8_t val;
214	int ret;
215	struct mpq7920_regulator_info *info = config->driver_data;
216	struct regulator_desc *rdesc = &info->rdesc[desc->id];
217
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);
222	}
223
224	ret = of_property_read_u8(np, "mps,buck-phase-delay", &val);
225	if (!ret) {
226		regmap_update_bits(config->regmap,
227				MPQ7920_BUCK1_REG_C + (rdesc->id * 4),
228				MPQ7920_MASK_BUCK_PHASE_DEALY,
229				(val & 3) << 4);
230	}
231
232	ret = of_property_read_u8(np, "mps,buck-softstart", &val);
233	if (!ret)
234		rdesc->soft_start_val_on = (val & 3) << 2;
235
236	return 0;
237}
238
239static void mpq7920_parse_dt(struct device *dev,
240		 struct mpq7920_regulator_info *info)
241{
242	int ret;
243	struct device_node *np = dev->of_node;
244	uint8_t freq;
245
246	np = of_get_child_by_name(np, "regulators");
247	if (!np) {
248		dev_err(dev, "missing 'regulators' subnode in DT\n");
249		return;
250	}
251
252	ret = of_property_read_u8(np, "mps,switch-freq", &freq);
253	if (!ret) {
254		regmap_update_bits(info->regmap, MPQ7920_REG_CTL0,
255					MPQ7920_MASK_SWITCH_FREQ,
256					(freq & 3) << 4);
257	}
258
259	of_node_put(np);
260}
261
262static int mpq7920_i2c_probe(struct i2c_client *client)
263{
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;
269	int i;
270
271	info = devm_kzalloc(dev, sizeof(struct mpq7920_regulator_info),
272				GFP_KERNEL);
273	if (!info)
274		return -ENOMEM;
275
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);
281	}
282
283	i2c_set_clientdata(client, info);
284	info->regmap = regmap;
285	if (client->dev.of_node)
286		mpq7920_parse_dt(&client->dev, info);
287
288	config.dev = dev;
289	config.regmap = regmap;
290	config.driver_data = info;
291
292	for (i = 0; i < MPQ7920_MAX_REGULATORS; i++) {
293		rdev = devm_regulator_register(dev,
294					       &mpq7920_regulators_desc[i],
295					       &config);
296		if (IS_ERR(rdev)) {
297			dev_err(dev, "Failed to register regulator!\n");
298			return PTR_ERR(rdev);
299		}
300	}
301
302	return 0;
303}
304
305static const struct of_device_id mpq7920_of_match[] = {
306	{ .compatible = "mps,mpq7920"},
307	{},
308};
309MODULE_DEVICE_TABLE(of, mpq7920_of_match);
310
311static const struct i2c_device_id mpq7920_id[] = {
312	{ "mpq7920", },
313	{ },
314};
315MODULE_DEVICE_TABLE(i2c, mpq7920_id);
316
317static struct i2c_driver mpq7920_regulator_driver = {
318	.driver = {
319		.name = "mpq7920",
320		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
321		.of_match_table = mpq7920_of_match,
322	},
323	.probe = mpq7920_i2c_probe,
324	.id_table = mpq7920_id,
325};
326module_i2c_driver(mpq7920_regulator_driver);
327
328MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
329MODULE_DESCRIPTION("MPQ7920 PMIC regulator driver");
330MODULE_LICENSE("GPL");
v6.2
  1// SPDX-License-Identifier: GPL-2.0+
  2//
  3// mpq7920.c  - regulator driver for mps mpq7920
  4//
  5// Copyright 2019 Monolithic Power Systems, Inc
  6//
  7// Author: Saravanan Sekar <sravanhome@gmail.com>
  8
  9#include <linux/kernel.h>
 10#include <linux/module.h>
 11#include <linux/init.h>
 12#include <linux/err.h>
 13#include <linux/of.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>
 20#include "mpq7920.h"
 21
 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)
 26
 27#define MPQ7920BUCK(_name, _id, _ilim)					\
 28	[MPQ7920_BUCK ## _id] = {					\
 29		.id = MPQ7920_BUCK ## _id,				\
 30		.name = _name,						\
 31		.of_match = _name,					\
 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,			\
 53	}
 54
 55#define MPQ7920LDO(_name, _id, _ops, _ilim, _ilim_sz, _creg, _cmask)	\
 56	[MPQ7920_LDO ## _id] = {					\
 57		.id = MPQ7920_LDO ## _id,				\
 58		.name = _name,						\
 59		.of_match = _name,					\
 60		.regulators_node = "regulators",			\
 61		.ops = _ops,						\
 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,				\
 69		.csel_reg = _creg,					\
 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,			\
 79	}
 80
 81enum mpq7920_regulators {
 82	MPQ7920_BUCK1,
 83	MPQ7920_BUCK2,
 84	MPQ7920_BUCK3,
 85	MPQ7920_BUCK4,
 86	MPQ7920_LDO1, /* LDORTC */
 87	MPQ7920_LDO2,
 88	MPQ7920_LDO3,
 89	MPQ7920_LDO4,
 90	MPQ7920_LDO5,
 91	MPQ7920_MAX_REGULATORS,
 92};
 93
 94struct mpq7920_regulator_info {
 95	struct regmap *regmap;
 96	struct regulator_desc *rdesc;
 97};
 98
 99static const struct regmap_config mpq7920_regmap_config = {
100	.reg_bits = 8,
101	.val_bits = 8,
102	.max_register = 0x25,
103};
104
105/* Current limits array (in uA)
106 * ILIM1 & ILIM3
107 */
108static const unsigned int mpq7920_I_limits1[] = {
109	4600000, 6600000, 7600000, 9300000
110};
111
112/* ILIM2 & ILIM4 */
113static const unsigned int mpq7920_I_limits2[] = {
114	2700000, 3900000, 5100000, 6100000
115};
116
117/* LDO4 & LDO5 */
118static const unsigned int mpq7920_I_limits3[] = {
119	300000, 700000
120};
121
122static int mpq7920_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay);
123static int mpq7920_parse_cb(struct device_node *np,
124				const struct regulator_desc *rdesc,
125				struct regulator_config *config);
126
127/* RTCLDO not controllable, always ON */
128static 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,
133};
134
135static 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,
144};
145
146static 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,
157};
158
159static 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,
170};
171
172static 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),
186};
187
188/*
189 * DVS ramp rate BUCK1 to BUCK4
190 * 00-01: Reserved
191 * 10: 8mV/us
192 * 11: 4mV/us
193 */
194static int mpq7920_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
195{
196	unsigned int ramp_val;
197
198	if (ramp_delay > 8000 || ramp_delay < 0)
199		return -EINVAL;
200
201	if (ramp_delay <= 4000)
202		ramp_val = 3;
203	else
204		ramp_val = 2;
205
206	return regmap_update_bits(rdev->regmap, MPQ7920_REG_CTL0,
207				  MPQ7920_MASK_DVS_SLEWRATE, ramp_val << 6);
208}
209
210static int mpq7920_parse_cb(struct device_node *np,
211				const struct regulator_desc *desc,
212				struct regulator_config *config)
213{
214	uint8_t val;
215	int ret;
216	struct mpq7920_regulator_info *info = config->driver_data;
217	struct regulator_desc *rdesc = &info->rdesc[desc->id];
218
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);
223	}
224
225	ret = of_property_read_u8(np, "mps,buck-phase-delay", &val);
226	if (!ret) {
227		regmap_update_bits(config->regmap,
228				MPQ7920_BUCK1_REG_C + (rdesc->id * 4),
229				MPQ7920_MASK_BUCK_PHASE_DEALY,
230				(val & 3) << 4);
231	}
232
233	ret = of_property_read_u8(np, "mps,buck-softstart", &val);
234	if (!ret)
235		rdesc->soft_start_val_on = (val & 3) << 2;
236
237	return 0;
238}
239
240static void mpq7920_parse_dt(struct device *dev,
241		 struct mpq7920_regulator_info *info)
242{
243	int ret;
244	struct device_node *np = dev->of_node;
245	uint8_t freq;
246
247	np = of_get_child_by_name(np, "regulators");
248	if (!np) {
249		dev_err(dev, "missing 'regulators' subnode in DT\n");
250		return;
251	}
252
253	ret = of_property_read_u8(np, "mps,switch-freq", &freq);
254	if (!ret) {
255		regmap_update_bits(info->regmap, MPQ7920_REG_CTL0,
256					MPQ7920_MASK_SWITCH_FREQ,
257					(freq & 3) << 4);
258	}
259
260	of_node_put(np);
261}
262
263static int mpq7920_i2c_probe(struct i2c_client *client)
264{
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;
270	int i;
271
272	info = devm_kzalloc(dev, sizeof(struct mpq7920_regulator_info),
273				GFP_KERNEL);
274	if (!info)
275		return -ENOMEM;
276
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);
282	}
283
284	i2c_set_clientdata(client, info);
285	info->regmap = regmap;
286	if (client->dev.of_node)
287		mpq7920_parse_dt(&client->dev, info);
288
289	config.dev = dev;
290	config.regmap = regmap;
291	config.driver_data = info;
292
293	for (i = 0; i < MPQ7920_MAX_REGULATORS; i++) {
294		rdev = devm_regulator_register(dev,
295					       &mpq7920_regulators_desc[i],
296					       &config);
297		if (IS_ERR(rdev)) {
298			dev_err(dev, "Failed to register regulator!\n");
299			return PTR_ERR(rdev);
300		}
301	}
302
303	return 0;
304}
305
306static const struct of_device_id mpq7920_of_match[] = {
307	{ .compatible = "mps,mpq7920"},
308	{},
309};
310MODULE_DEVICE_TABLE(of, mpq7920_of_match);
311
312static const struct i2c_device_id mpq7920_id[] = {
313	{ "mpq7920", },
314	{ },
315};
316MODULE_DEVICE_TABLE(i2c, mpq7920_id);
317
318static struct i2c_driver mpq7920_regulator_driver = {
319	.driver = {
320		.name = "mpq7920",
321		.of_match_table = of_match_ptr(mpq7920_of_match),
 
322	},
323	.probe_new = mpq7920_i2c_probe,
324	.id_table = mpq7920_id,
325};
326module_i2c_driver(mpq7920_regulator_driver);
327
328MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
329MODULE_DESCRIPTION("MPQ7920 PMIC regulator driver");
330MODULE_LICENSE("GPL");