xref: /linux/drivers/regulator/max77826-regulator.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 //
3 // max77826-regulator.c  - regulator driver for Maxim MAX77826
4 //
5 // Author: Iskren Chernev <iskren.chernev@gmail.com>
6 
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/err.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/regulator/of_regulator.h>
15 #include <linux/i2c.h>
16 #include <linux/regmap.h>
17 
18 enum max77826_registers {
19 	MAX77826_REG_INT_SRC = 0x00,
20 	MAX77826_REG_SYS_INT,
21 	MAX77826_REG_INT1,
22 	MAX77826_REG_INT2,
23 	MAX77826_REG_BB_INT,
24 	MAX77826_REG_INT_SRC_M,
25 	MAX77826_REG_TOPSYS_INT_M,
26 	MAX77826_REG_INT1_M,
27 	MAX77826_REG_INT2_M,
28 	MAX77826_REG_BB_INT_M,
29 	MAX77826_REG_TOPSYS_STAT,
30 	MAX77826_REG_STAT1,
31 	MAX77826_REG_STAT2,
32 	MAX77826_REG_BB_STAT,
33 	/* 0x0E - 0x0F: Reserved */
34 	MAX77826_REG_LDO_OPMD1 = 0x10,
35 	MAX77826_REG_LDO_OPMD2,
36 	MAX77826_REG_LDO_OPMD3,
37 	MAX77826_REG_LDO_OPMD4,
38 	MAX77826_REG_B_BB_OPMD,
39 	/* 0x15 - 0x1F: Reserved */
40 	MAX77826_REG_LDO1_CFG = 0x20,
41 	MAX77826_REG_LDO2_CFG,
42 	MAX77826_REG_LDO3_CFG,
43 	MAX77826_REG_LDO4_CFG,
44 	MAX77826_REG_LDO5_CFG,
45 	MAX77826_REG_LDO6_CFG,
46 	MAX77826_REG_LDO7_CFG,
47 	MAX77826_REG_LDO8_CFG,
48 	MAX77826_REG_LDO9_CFG,
49 	MAX77826_REG_LDO10_CFG,
50 	MAX77826_REG_LDO11_CFG,
51 	MAX77826_REG_LDO12_CFG,
52 	MAX77826_REG_LDO13_CFG,
53 	MAX77826_REG_LDO14_CFG,
54 	MAX77826_REG_LDO15_CFG,
55 	/* 0x2F: Reserved */
56 	MAX77826_REG_BUCK_CFG = 0x30,
57 	MAX77826_REG_BUCK_VOUT,
58 	MAX77826_REG_BB_CFG,
59 	MAX77826_REG_BB_VOUT,
60 	/* 0x34 - 0x3F: Reserved */
61 	MAX77826_REG_BUCK_SS_FREQ = 0x40,
62 	MAX77826_REG_UVLO_FALL,
63 	/* 0x42 - 0xCE: Reserved */
64 	MAX77826_REG_DEVICE_ID = 0xCF,
65 };
66 
67 enum max77826_regulators {
68 	MAX77826_LDO1 = 0,
69 	MAX77826_LDO2,
70 	MAX77826_LDO3,
71 	MAX77826_LDO4,
72 	MAX77826_LDO5,
73 	MAX77826_LDO6,
74 	MAX77826_LDO7,
75 	MAX77826_LDO8,
76 	MAX77826_LDO9,
77 	MAX77826_LDO10,
78 	MAX77826_LDO11,
79 	MAX77826_LDO12,
80 	MAX77826_LDO13,
81 	MAX77826_LDO14,
82 	MAX77826_LDO15,
83 	MAX77826_BUCK,
84 	MAX77826_BUCKBOOST,
85 	MAX77826_MAX_REGULATORS,
86 };
87 
88 #define MAX77826_MASK_LDO		0x7f
89 #define MAX77826_MASK_BUCK		0xff
90 #define MAX77826_MASK_BUCKBOOST		0x7f
91 #define MAX77826_BUCK_RAMP_DELAY	12500
92 
93 /* values in mV */
94 /* for LDO1-3 */
95 #define MAX77826_NMOS_LDO_VOLT_MIN	600000
96 #define MAX77826_NMOS_LDO_VOLT_MAX	2187500
97 #define MAX77826_NMOS_LDO_VOLT_STEP	12500
98 
99 /* for LDO4-15 */
100 #define MAX77826_PMOS_LDO_VOLT_MIN	800000
101 #define MAX77826_PMOS_LDO_VOLT_MAX	3975000
102 #define MAX77826_PMOS_LDO_VOLT_STEP	25000
103 
104 /* for BUCK */
105 #define MAX77826_BUCK_VOLT_MIN		500000
106 #define MAX77826_BUCK_VOLT_MAX		1800000
107 #define MAX77826_BUCK_VOLT_STEP		6250
108 
109 /* for BUCKBOOST */
110 #define MAX77826_BUCKBOOST_VOLT_MIN	2600000
111 #define MAX77826_BUCKBOOST_VOLT_MAX	4187500
112 #define MAX77826_BUCKBOOST_VOLT_STEP	12500
113 #define MAX77826_VOLT_RANGE(_type)					\
114 	((MAX77826_ ## _type ## _VOLT_MAX -				\
115 	  MAX77826_ ## _type ## _VOLT_MIN) /				\
116 	 MAX77826_ ## _type ## _VOLT_STEP + 1)
117 
118 #define MAX77826_LDO(_id, _type)					\
119 	[MAX77826_LDO ## _id] = {					\
120 		.id = MAX77826_LDO ## _id,				\
121 		.name = "LDO"#_id,					\
122 		.of_match = of_match_ptr("LDO"#_id),			\
123 		.regulators_node = "regulators",			\
124 		.ops = &max77826_most_ops,				\
125 		.min_uV = MAX77826_ ## _type ## _LDO_VOLT_MIN,		\
126 		.uV_step = MAX77826_ ## _type ## _LDO_VOLT_STEP,	\
127 		.n_voltages = MAX77826_VOLT_RANGE(_type ## _LDO),	\
128 		.enable_reg = MAX77826_REG_LDO_OPMD1 + (_id - 1) / 4,	\
129 		.enable_mask = BIT(((_id - 1) % 4) * 2 + 1),		\
130 		.vsel_reg = MAX77826_REG_LDO1_CFG + (_id - 1),		\
131 		.vsel_mask = MAX77826_MASK_LDO,				\
132 		.owner = THIS_MODULE,					\
133 	}
134 
135 #define MAX77826_BUCK(_idx, _id, _ops)					\
136 	[MAX77826_ ## _id] = {						\
137 		.id = MAX77826_ ## _id,					\
138 		.name = #_id,						\
139 		.of_match = of_match_ptr(#_id),				\
140 		.regulators_node = "regulators",			\
141 		.ops = &_ops,						\
142 		.min_uV =  MAX77826_ ## _id ## _VOLT_MIN,		\
143 		.uV_step = MAX77826_ ## _id ## _VOLT_STEP,		\
144 		.n_voltages = MAX77826_VOLT_RANGE(_id),			\
145 		.enable_reg = MAX77826_REG_B_BB_OPMD,			\
146 		.enable_mask = BIT(_idx * 2 + 1),			\
147 		.vsel_reg = MAX77826_REG_BUCK_VOUT + _idx * 2,		\
148 		.vsel_mask = MAX77826_MASK_ ## _id,			\
149 		.owner = THIS_MODULE,					\
150 	}
151 
152 
153 
154 struct max77826_regulator_info {
155 	struct regmap *regmap;
156 };
157 
158 static const struct regmap_config max77826_regmap_config = {
159 	.reg_bits = 8,
160 	.val_bits = 8,
161 	.max_register = MAX77826_REG_DEVICE_ID,
162 };
163 
164 static int max77826_set_voltage_time_sel(struct regulator_dev *,
165 				unsigned int old_selector,
166 				unsigned int new_selector);
167 
168 static const struct regulator_ops max77826_most_ops = {
169 	.enable			= regulator_enable_regmap,
170 	.disable		= regulator_disable_regmap,
171 	.is_enabled		= regulator_is_enabled_regmap,
172 	.list_voltage		= regulator_list_voltage_linear,
173 	.map_voltage		= regulator_map_voltage_linear,
174 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
175 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
176 };
177 
178 static const struct regulator_ops max77826_buck_ops = {
179 	.enable			= regulator_enable_regmap,
180 	.disable		= regulator_disable_regmap,
181 	.is_enabled		= regulator_is_enabled_regmap,
182 	.list_voltage		= regulator_list_voltage_linear,
183 	.map_voltage		= regulator_map_voltage_linear,
184 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
185 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
186 	.set_voltage_time_sel	= max77826_set_voltage_time_sel,
187 };
188 
189 static const struct regulator_desc max77826_regulators_desc[] = {
190 	MAX77826_LDO(1, NMOS),
191 	MAX77826_LDO(2, NMOS),
192 	MAX77826_LDO(3, NMOS),
193 	MAX77826_LDO(4, PMOS),
194 	MAX77826_LDO(5, PMOS),
195 	MAX77826_LDO(6, PMOS),
196 	MAX77826_LDO(7, PMOS),
197 	MAX77826_LDO(8, PMOS),
198 	MAX77826_LDO(9, PMOS),
199 	MAX77826_LDO(10, PMOS),
200 	MAX77826_LDO(11, PMOS),
201 	MAX77826_LDO(12, PMOS),
202 	MAX77826_LDO(13, PMOS),
203 	MAX77826_LDO(14, PMOS),
204 	MAX77826_LDO(15, PMOS),
205 	MAX77826_BUCK(0, BUCK, max77826_buck_ops),
206 	MAX77826_BUCK(1, BUCKBOOST, max77826_most_ops),
207 };
208 
209 static int max77826_set_voltage_time_sel(struct regulator_dev *rdev,
210 				unsigned int old_selector,
211 				unsigned int new_selector)
212 {
213 	if (new_selector > old_selector) {
214 		return DIV_ROUND_UP(MAX77826_BUCK_VOLT_STEP *
215 				(new_selector - old_selector),
216 				MAX77826_BUCK_RAMP_DELAY);
217 	}
218 
219 	return 0;
220 }
221 
222 static int max77826_read_device_id(struct regmap *regmap, struct device *dev)
223 {
224 	unsigned int device_id;
225 	int res;
226 
227 	res = regmap_read(regmap, MAX77826_REG_DEVICE_ID, &device_id);
228 	if (!res)
229 		dev_dbg(dev, "DEVICE_ID: 0x%x\n", device_id);
230 
231 	return res;
232 }
233 
234 static int max77826_i2c_probe(struct i2c_client *client)
235 {
236 	struct device *dev = &client->dev;
237 	struct max77826_regulator_info *info;
238 	struct regulator_config config = {};
239 	struct regulator_dev *rdev;
240 	struct regmap *regmap;
241 	int i;
242 
243 	info = devm_kzalloc(dev, sizeof(struct max77826_regulator_info),
244 				GFP_KERNEL);
245 	if (!info)
246 		return -ENOMEM;
247 
248 	regmap = devm_regmap_init_i2c(client, &max77826_regmap_config);
249 	if (IS_ERR(regmap)) {
250 		dev_err(dev, "Failed to allocate regmap!\n");
251 		return PTR_ERR(regmap);
252 	}
253 
254 	info->regmap = regmap;
255 	i2c_set_clientdata(client, info);
256 
257 	config.dev = dev;
258 	config.regmap = regmap;
259 	config.driver_data = info;
260 
261 	for (i = 0; i < MAX77826_MAX_REGULATORS; i++) {
262 		rdev = devm_regulator_register(dev,
263 					       &max77826_regulators_desc[i],
264 					       &config);
265 		if (IS_ERR(rdev)) {
266 			dev_err(dev, "Failed to register regulator!\n");
267 			return PTR_ERR(rdev);
268 		}
269 	}
270 
271 	return max77826_read_device_id(regmap, dev);
272 }
273 
274 static const struct of_device_id __maybe_unused max77826_of_match[] = {
275 	{ .compatible = "maxim,max77826" },
276 	{ /* sentinel */ }
277 };
278 MODULE_DEVICE_TABLE(of, max77826_of_match);
279 
280 static const struct i2c_device_id max77826_id[] = {
281 	{ "max77826-regulator" },
282 	{ /* sentinel */ }
283 };
284 MODULE_DEVICE_TABLE(i2c, max77826_id);
285 
286 static struct i2c_driver max77826_regulator_driver = {
287 	.driver = {
288 		.name = "max77826",
289 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
290 		.of_match_table = of_match_ptr(max77826_of_match),
291 	},
292 	.probe = max77826_i2c_probe,
293 	.id_table = max77826_id,
294 };
295 module_i2c_driver(max77826_regulator_driver);
296 
297 MODULE_AUTHOR("Iskren Chernev <iskren.chernev@gmail.com>");
298 MODULE_DESCRIPTION("MAX77826 PMIC regulator driver");
299 MODULE_LICENSE("GPL");
300