1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // max77826-regulator.c - regulator driver for Maxim MAX77826
5 // Author: Iskren Chernev <iskren.chernev@gmail.com>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/err.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/of_regulator.h>
16 #include <linux/i2c.h>
17 #include <linux/regmap.h>
19 enum max77826_registers {
20 MAX77826_REG_INT_SRC = 0x00,
25 MAX77826_REG_INT_SRC_M,
26 MAX77826_REG_TOPSYS_INT_M,
29 MAX77826_REG_BB_INT_M,
30 MAX77826_REG_TOPSYS_STAT,
34 /* 0x0E - 0x0F: Reserved */
35 MAX77826_REG_LDO_OPMD1 = 0x10,
36 MAX77826_REG_LDO_OPMD2,
37 MAX77826_REG_LDO_OPMD3,
38 MAX77826_REG_LDO_OPMD4,
39 MAX77826_REG_B_BB_OPMD,
40 /* 0x15 - 0x1F: Reserved */
41 MAX77826_REG_LDO1_CFG = 0x20,
42 MAX77826_REG_LDO2_CFG,
43 MAX77826_REG_LDO3_CFG,
44 MAX77826_REG_LDO4_CFG,
45 MAX77826_REG_LDO5_CFG,
46 MAX77826_REG_LDO6_CFG,
47 MAX77826_REG_LDO7_CFG,
48 MAX77826_REG_LDO8_CFG,
49 MAX77826_REG_LDO9_CFG,
50 MAX77826_REG_LDO10_CFG,
51 MAX77826_REG_LDO11_CFG,
52 MAX77826_REG_LDO12_CFG,
53 MAX77826_REG_LDO13_CFG,
54 MAX77826_REG_LDO14_CFG,
55 MAX77826_REG_LDO15_CFG,
57 MAX77826_REG_BUCK_CFG = 0x30,
58 MAX77826_REG_BUCK_VOUT,
61 /* 0x34 - 0x3F: Reserved */
62 MAX77826_REG_BUCK_SS_FREQ = 0x40,
63 MAX77826_REG_UVLO_FALL,
64 /* 0x42 - 0xCE: Reserved */
65 MAX77826_REG_DEVICE_ID = 0xCF,
68 enum max77826_regulators {
86 MAX77826_MAX_REGULATORS,
89 #define MAX77826_MASK_LDO 0x7f
90 #define MAX77826_MASK_BUCK 0xff
91 #define MAX77826_MASK_BUCKBOOST 0x7f
92 #define MAX77826_BUCK_RAMP_DELAY 12500
96 #define MAX77826_NMOS_LDO_VOLT_MIN 600000
97 #define MAX77826_NMOS_LDO_VOLT_MAX 2187500
98 #define MAX77826_NMOS_LDO_VOLT_STEP 12500
101 #define MAX77826_PMOS_LDO_VOLT_MIN 800000
102 #define MAX77826_PMOS_LDO_VOLT_MAX 3975000
103 #define MAX77826_PMOS_LDO_VOLT_STEP 25000
106 #define MAX77826_BUCK_VOLT_MIN 500000
107 #define MAX77826_BUCK_VOLT_MAX 1800000
108 #define MAX77826_BUCK_VOLT_STEP 6250
111 #define MAX77826_BUCKBOOST_VOLT_MIN 2600000
112 #define MAX77826_BUCKBOOST_VOLT_MAX 4187500
113 #define MAX77826_BUCKBOOST_VOLT_STEP 12500
114 #define MAX77826_VOLT_RANGE(_type) \
115 ((MAX77826_ ## _type ## _VOLT_MAX - \
116 MAX77826_ ## _type ## _VOLT_MIN) / \
117 MAX77826_ ## _type ## _VOLT_STEP + 1)
119 #define MAX77826_LDO(_id, _type) \
120 [MAX77826_LDO ## _id] = { \
121 .id = MAX77826_LDO ## _id, \
123 .of_match = of_match_ptr("LDO"#_id), \
124 .regulators_node = "regulators", \
125 .ops = &max77826_most_ops, \
126 .min_uV = MAX77826_ ## _type ## _LDO_VOLT_MIN, \
127 .uV_step = MAX77826_ ## _type ## _LDO_VOLT_STEP, \
128 .n_voltages = MAX77826_VOLT_RANGE(_type ## _LDO), \
129 .enable_reg = MAX77826_REG_LDO_OPMD1 + (_id - 1) / 4, \
130 .enable_mask = BIT(((_id - 1) % 4) * 2 + 1), \
131 .vsel_reg = MAX77826_REG_LDO1_CFG + (_id - 1), \
132 .vsel_mask = MAX77826_MASK_LDO, \
133 .owner = THIS_MODULE, \
136 #define MAX77826_BUCK(_idx, _id, _ops) \
137 [MAX77826_ ## _id] = { \
138 .id = MAX77826_ ## _id, \
140 .of_match = of_match_ptr(#_id), \
141 .regulators_node = "regulators", \
143 .min_uV = MAX77826_ ## _id ## _VOLT_MIN, \
144 .uV_step = MAX77826_ ## _id ## _VOLT_STEP, \
145 .n_voltages = MAX77826_VOLT_RANGE(_id), \
146 .enable_reg = MAX77826_REG_B_BB_OPMD, \
147 .enable_mask = BIT(_idx * 2 + 1), \
148 .vsel_reg = MAX77826_REG_BUCK_VOUT + _idx * 2, \
149 .vsel_mask = MAX77826_MASK_ ## _id, \
150 .owner = THIS_MODULE, \
155 struct max77826_regulator_info {
156 struct regmap *regmap;
157 struct regulator_desc *rdesc;
160 static const struct regmap_config max77826_regmap_config = {
163 .max_register = MAX77826_REG_DEVICE_ID,
166 static int max77826_set_voltage_time_sel(struct regulator_dev *,
167 unsigned int old_selector,
168 unsigned int new_selector);
170 static const struct regulator_ops max77826_most_ops = {
171 .enable = regulator_enable_regmap,
172 .disable = regulator_disable_regmap,
173 .is_enabled = regulator_is_enabled_regmap,
174 .list_voltage = regulator_list_voltage_linear,
175 .map_voltage = regulator_map_voltage_linear,
176 .get_voltage_sel = regulator_get_voltage_sel_regmap,
177 .set_voltage_sel = regulator_set_voltage_sel_regmap,
180 static const struct regulator_ops max77826_buck_ops = {
181 .enable = regulator_enable_regmap,
182 .disable = regulator_disable_regmap,
183 .is_enabled = regulator_is_enabled_regmap,
184 .list_voltage = regulator_list_voltage_linear,
185 .map_voltage = regulator_map_voltage_linear,
186 .get_voltage_sel = regulator_get_voltage_sel_regmap,
187 .set_voltage_sel = regulator_set_voltage_sel_regmap,
188 .set_voltage_time_sel = max77826_set_voltage_time_sel,
191 static struct regulator_desc max77826_regulators_desc[] = {
192 MAX77826_LDO(1, NMOS),
193 MAX77826_LDO(2, NMOS),
194 MAX77826_LDO(3, NMOS),
195 MAX77826_LDO(4, PMOS),
196 MAX77826_LDO(5, PMOS),
197 MAX77826_LDO(6, PMOS),
198 MAX77826_LDO(7, PMOS),
199 MAX77826_LDO(8, PMOS),
200 MAX77826_LDO(9, PMOS),
201 MAX77826_LDO(10, PMOS),
202 MAX77826_LDO(11, PMOS),
203 MAX77826_LDO(12, PMOS),
204 MAX77826_LDO(13, PMOS),
205 MAX77826_LDO(14, PMOS),
206 MAX77826_LDO(15, PMOS),
207 MAX77826_BUCK(0, BUCK, max77826_buck_ops),
208 MAX77826_BUCK(1, BUCKBOOST, max77826_most_ops),
211 static int max77826_set_voltage_time_sel(struct regulator_dev *rdev,
212 unsigned int old_selector,
213 unsigned int new_selector)
215 if (new_selector > old_selector) {
216 return DIV_ROUND_UP(MAX77826_BUCK_VOLT_STEP *
217 (new_selector - old_selector),
218 MAX77826_BUCK_RAMP_DELAY);
224 static int max77826_read_device_id(struct regmap *regmap, struct device *dev)
226 unsigned int device_id;
229 res = regmap_read(regmap, MAX77826_REG_DEVICE_ID, &device_id);
231 dev_dbg(dev, "DEVICE_ID: 0x%x\n", device_id);
236 static int max77826_i2c_probe(struct i2c_client *client)
238 struct device *dev = &client->dev;
239 struct max77826_regulator_info *info;
240 struct regulator_config config = {};
241 struct regulator_dev *rdev;
242 struct regmap *regmap;
245 info = devm_kzalloc(dev, sizeof(struct max77826_regulator_info),
250 info->rdesc = max77826_regulators_desc;
251 regmap = devm_regmap_init_i2c(client, &max77826_regmap_config);
252 if (IS_ERR(regmap)) {
253 dev_err(dev, "Failed to allocate regmap!\n");
254 return PTR_ERR(regmap);
257 info->regmap = regmap;
258 i2c_set_clientdata(client, info);
261 config.regmap = regmap;
262 config.driver_data = info;
264 for (i = 0; i < MAX77826_MAX_REGULATORS; i++) {
265 rdev = devm_regulator_register(dev,
266 &max77826_regulators_desc[i],
269 dev_err(dev, "Failed to register regulator!\n");
270 return PTR_ERR(rdev);
274 return max77826_read_device_id(regmap, dev);
277 static const struct of_device_id __maybe_unused max77826_of_match[] = {
278 { .compatible = "maxim,max77826" },
281 MODULE_DEVICE_TABLE(of, max77826_of_match);
283 static const struct i2c_device_id max77826_id[] = {
284 { "max77826-regulator" },
287 MODULE_DEVICE_TABLE(i2c, max77826_id);
289 static struct i2c_driver max77826_regulator_driver = {
292 .of_match_table = of_match_ptr(max77826_of_match),
294 .probe_new = max77826_i2c_probe,
295 .id_table = max77826_id,
297 module_i2c_driver(max77826_regulator_driver);
299 MODULE_AUTHOR("Iskren Chernev <iskren.chernev@gmail.com>");
300 MODULE_DESCRIPTION("MAX77826 PMIC regulator driver");
301 MODULE_LICENSE("GPL");