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/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>
18 enum max77826_registers
{
19 MAX77826_REG_INT_SRC
= 0x00,
24 MAX77826_REG_INT_SRC_M
,
25 MAX77826_REG_TOPSYS_INT_M
,
28 MAX77826_REG_BB_INT_M
,
29 MAX77826_REG_TOPSYS_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
,
56 MAX77826_REG_BUCK_CFG
= 0x30,
57 MAX77826_REG_BUCK_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,
67 enum max77826_regulators
{
85 MAX77826_MAX_REGULATORS
,
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
95 #define MAX77826_NMOS_LDO_VOLT_MIN 600000
96 #define MAX77826_NMOS_LDO_VOLT_MAX 2187500
97 #define MAX77826_NMOS_LDO_VOLT_STEP 12500
100 #define MAX77826_PMOS_LDO_VOLT_MIN 800000
101 #define MAX77826_PMOS_LDO_VOLT_MAX 3975000
102 #define MAX77826_PMOS_LDO_VOLT_STEP 25000
105 #define MAX77826_BUCK_VOLT_MIN 500000
106 #define MAX77826_BUCK_VOLT_MAX 1800000
107 #define MAX77826_BUCK_VOLT_STEP 6250
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)
118 #define MAX77826_LDO(_id, _type) \
119 [MAX77826_LDO ## _id] = { \
120 .id = MAX77826_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, \
135 #define MAX77826_BUCK(_idx, _id, _ops) \
136 [MAX77826_ ## _id] = { \
137 .id = MAX77826_ ## _id, \
139 .of_match = of_match_ptr(#_id), \
140 .regulators_node = "regulators", \
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, \
154 struct max77826_regulator_info
{
155 struct regmap
*regmap
;
158 static const struct regmap_config max77826_regmap_config
= {
161 .max_register
= MAX77826_REG_DEVICE_ID
,
164 static int max77826_set_voltage_time_sel(struct regulator_dev
*,
165 unsigned int old_selector
,
166 unsigned int new_selector
);
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
,
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
,
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
),
209 static int max77826_set_voltage_time_sel(struct regulator_dev
*rdev
,
210 unsigned int old_selector
,
211 unsigned int new_selector
)
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
);
222 static int max77826_read_device_id(struct regmap
*regmap
, struct device
*dev
)
224 unsigned int device_id
;
227 res
= regmap_read(regmap
, MAX77826_REG_DEVICE_ID
, &device_id
);
229 dev_dbg(dev
, "DEVICE_ID: 0x%x\n", device_id
);
234 static int max77826_i2c_probe(struct i2c_client
*client
)
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
;
243 info
= devm_kzalloc(dev
, sizeof(struct max77826_regulator_info
),
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
);
254 info
->regmap
= regmap
;
255 i2c_set_clientdata(client
, info
);
258 config
.regmap
= regmap
;
259 config
.driver_data
= info
;
261 for (i
= 0; i
< MAX77826_MAX_REGULATORS
; i
++) {
262 rdev
= devm_regulator_register(dev
,
263 &max77826_regulators_desc
[i
],
266 dev_err(dev
, "Failed to register regulator!\n");
267 return PTR_ERR(rdev
);
271 return max77826_read_device_id(regmap
, dev
);
274 static const struct of_device_id __maybe_unused max77826_of_match
[] = {
275 { .compatible
= "maxim,max77826" },
278 MODULE_DEVICE_TABLE(of
, max77826_of_match
);
280 static const struct i2c_device_id max77826_id
[] = {
281 { "max77826-regulator" },
284 MODULE_DEVICE_TABLE(i2c
, max77826_id
);
286 static struct i2c_driver max77826_regulator_driver
= {
289 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
290 .of_match_table
= of_match_ptr(max77826_of_match
),
292 .probe
= max77826_i2c_probe
,
293 .id_table
= max77826_id
,
295 module_i2c_driver(max77826_regulator_driver
);
297 MODULE_AUTHOR("Iskren Chernev <iskren.chernev@gmail.com>");
298 MODULE_DESCRIPTION("MAX77826 PMIC regulator driver");
299 MODULE_LICENSE("GPL");