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 	struct regulator_desc *rdesc;
157 };
158 
159 static const struct regmap_config max77826_regmap_config = {
160 	.reg_bits = 8,
161 	.val_bits = 8,
162 	.max_register = MAX77826_REG_DEVICE_ID,
163 };
164 
165 static int max77826_set_voltage_time_sel(struct regulator_dev *,
166 				unsigned int old_selector,
167 				unsigned int new_selector);
168 
169 static const struct regulator_ops max77826_most_ops = {
170 	.enable			= regulator_enable_regmap,
171 	.disable		= regulator_disable_regmap,
172 	.is_enabled		= regulator_is_enabled_regmap,
173 	.list_voltage		= regulator_list_voltage_linear,
174 	.map_voltage		= regulator_map_voltage_linear,
175 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
176 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
177 };
178 
179 static const struct regulator_ops max77826_buck_ops = {
180 	.enable			= regulator_enable_regmap,
181 	.disable		= regulator_disable_regmap,
182 	.is_enabled		= regulator_is_enabled_regmap,
183 	.list_voltage		= regulator_list_voltage_linear,
184 	.map_voltage		= regulator_map_voltage_linear,
185 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
186 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
187 	.set_voltage_time_sel	= max77826_set_voltage_time_sel,
188 };
189 
190 static struct regulator_desc max77826_regulators_desc[] = {
191 	MAX77826_LDO(1, NMOS),
192 	MAX77826_LDO(2, NMOS),
193 	MAX77826_LDO(3, NMOS),
194 	MAX77826_LDO(4, PMOS),
195 	MAX77826_LDO(5, PMOS),
196 	MAX77826_LDO(6, PMOS),
197 	MAX77826_LDO(7, PMOS),
198 	MAX77826_LDO(8, PMOS),
199 	MAX77826_LDO(9, PMOS),
200 	MAX77826_LDO(10, PMOS),
201 	MAX77826_LDO(11, PMOS),
202 	MAX77826_LDO(12, PMOS),
203 	MAX77826_LDO(13, PMOS),
204 	MAX77826_LDO(14, PMOS),
205 	MAX77826_LDO(15, PMOS),
206 	MAX77826_BUCK(0, BUCK, max77826_buck_ops),
207 	MAX77826_BUCK(1, BUCKBOOST, max77826_most_ops),
208 };
209 
max77826_set_voltage_time_sel(struct regulator_dev * rdev,unsigned int old_selector,unsigned int new_selector)210 static int max77826_set_voltage_time_sel(struct regulator_dev *rdev,
211 				unsigned int old_selector,
212 				unsigned int new_selector)
213 {
214 	if (new_selector > old_selector) {
215 		return DIV_ROUND_UP(MAX77826_BUCK_VOLT_STEP *
216 				(new_selector - old_selector),
217 				MAX77826_BUCK_RAMP_DELAY);
218 	}
219 
220 	return 0;
221 }
222 
max77826_read_device_id(struct regmap * regmap,struct device * dev)223 static int max77826_read_device_id(struct regmap *regmap, struct device *dev)
224 {
225 	unsigned int device_id;
226 	int res;
227 
228 	res = regmap_read(regmap, MAX77826_REG_DEVICE_ID, &device_id);
229 	if (!res)
230 		dev_dbg(dev, "DEVICE_ID: 0x%x\n", device_id);
231 
232 	return res;
233 }
234 
max77826_i2c_probe(struct i2c_client * client)235 static int max77826_i2c_probe(struct i2c_client *client)
236 {
237 	struct device *dev = &client->dev;
238 	struct max77826_regulator_info *info;
239 	struct regulator_config config = {};
240 	struct regulator_dev *rdev;
241 	struct regmap *regmap;
242 	int i;
243 
244 	info = devm_kzalloc(dev, sizeof(struct max77826_regulator_info),
245 				GFP_KERNEL);
246 	if (!info)
247 		return -ENOMEM;
248 
249 	info->rdesc = max77826_regulators_desc;
250 	regmap = devm_regmap_init_i2c(client, &max77826_regmap_config);
251 	if (IS_ERR(regmap)) {
252 		dev_err(dev, "Failed to allocate regmap!\n");
253 		return PTR_ERR(regmap);
254 	}
255 
256 	info->regmap = regmap;
257 	i2c_set_clientdata(client, info);
258 
259 	config.dev = dev;
260 	config.regmap = regmap;
261 	config.driver_data = info;
262 
263 	for (i = 0; i < MAX77826_MAX_REGULATORS; i++) {
264 		rdev = devm_regulator_register(dev,
265 					       &max77826_regulators_desc[i],
266 					       &config);
267 		if (IS_ERR(rdev)) {
268 			dev_err(dev, "Failed to register regulator!\n");
269 			return PTR_ERR(rdev);
270 		}
271 	}
272 
273 	return max77826_read_device_id(regmap, dev);
274 }
275 
276 static const struct of_device_id __maybe_unused max77826_of_match[] = {
277 	{ .compatible = "maxim,max77826" },
278 	{ /* sentinel */ }
279 };
280 MODULE_DEVICE_TABLE(of, max77826_of_match);
281 
282 static const struct i2c_device_id max77826_id[] = {
283 	{ "max77826-regulator" },
284 	{ /* sentinel */ }
285 };
286 MODULE_DEVICE_TABLE(i2c, max77826_id);
287 
288 static struct i2c_driver max77826_regulator_driver = {
289 	.driver = {
290 		.name = "max77826",
291 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
292 		.of_match_table = of_match_ptr(max77826_of_match),
293 	},
294 	.probe = max77826_i2c_probe,
295 	.id_table = max77826_id,
296 };
297 module_i2c_driver(max77826_regulator_driver);
298 
299 MODULE_AUTHOR("Iskren Chernev <iskren.chernev@gmail.com>");
300 MODULE_DESCRIPTION("MAX77826 PMIC regulator driver");
301 MODULE_LICENSE("GPL");
302