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");
331