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/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> 18 19 enum max77826_registers { 20 MAX77826_REG_INT_SRC = 0x00, 21 MAX77826_REG_SYS_INT, 22 MAX77826_REG_INT1, 23 MAX77826_REG_INT2, 24 MAX77826_REG_BB_INT, 25 MAX77826_REG_INT_SRC_M, 26 MAX77826_REG_TOPSYS_INT_M, 27 MAX77826_REG_INT1_M, 28 MAX77826_REG_INT2_M, 29 MAX77826_REG_BB_INT_M, 30 MAX77826_REG_TOPSYS_STAT, 31 MAX77826_REG_STAT1, 32 MAX77826_REG_STAT2, 33 MAX77826_REG_BB_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, 56 /* 0x2F: Reserved */ 57 MAX77826_REG_BUCK_CFG = 0x30, 58 MAX77826_REG_BUCK_VOUT, 59 MAX77826_REG_BB_CFG, 60 MAX77826_REG_BB_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, 66 }; 67 68 enum max77826_regulators { 69 MAX77826_LDO1 = 0, 70 MAX77826_LDO2, 71 MAX77826_LDO3, 72 MAX77826_LDO4, 73 MAX77826_LDO5, 74 MAX77826_LDO6, 75 MAX77826_LDO7, 76 MAX77826_LDO8, 77 MAX77826_LDO9, 78 MAX77826_LDO10, 79 MAX77826_LDO11, 80 MAX77826_LDO12, 81 MAX77826_LDO13, 82 MAX77826_LDO14, 83 MAX77826_LDO15, 84 MAX77826_BUCK, 85 MAX77826_BUCKBOOST, 86 MAX77826_MAX_REGULATORS, 87 }; 88 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 93 94 /* values in mV */ 95 /* for LDO1-3 */ 96 #define MAX77826_NMOS_LDO_VOLT_MIN 600000 97 #define MAX77826_NMOS_LDO_VOLT_MAX 2187500 98 #define MAX77826_NMOS_LDO_VOLT_STEP 12500 99 100 /* for LDO4-15 */ 101 #define MAX77826_PMOS_LDO_VOLT_MIN 800000 102 #define MAX77826_PMOS_LDO_VOLT_MAX 3975000 103 #define MAX77826_PMOS_LDO_VOLT_STEP 25000 104 105 /* for BUCK */ 106 #define MAX77826_BUCK_VOLT_MIN 500000 107 #define MAX77826_BUCK_VOLT_MAX 1800000 108 #define MAX77826_BUCK_VOLT_STEP 6250 109 110 /* for BUCKBOOST */ 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) 118 119 #define MAX77826_LDO(_id, _type) \ 120 [MAX77826_LDO ## _id] = { \ 121 .id = MAX77826_LDO ## _id, \ 122 .name = "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, \ 134 } 135 136 #define MAX77826_BUCK(_idx, _id, _ops) \ 137 [MAX77826_ ## _id] = { \ 138 .id = MAX77826_ ## _id, \ 139 .name = #_id, \ 140 .of_match = of_match_ptr(#_id), \ 141 .regulators_node = "regulators", \ 142 .ops = &_ops, \ 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, \ 151 } 152 153 154 155 struct max77826_regulator_info { 156 struct regmap *regmap; 157 struct regulator_desc *rdesc; 158 }; 159 160 static const struct regmap_config max77826_regmap_config = { 161 .reg_bits = 8, 162 .val_bits = 8, 163 .max_register = MAX77826_REG_DEVICE_ID, 164 }; 165 166 static int max77826_set_voltage_time_sel(struct regulator_dev *, 167 unsigned int old_selector, 168 unsigned int new_selector); 169 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, 178 }; 179 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, 189 }; 190 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), 209 }; 210 211 static int max77826_set_voltage_time_sel(struct regulator_dev *rdev, 212 unsigned int old_selector, 213 unsigned int new_selector) 214 { 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); 219 } 220 221 return 0; 222 } 223 224 static int max77826_read_device_id(struct regmap *regmap, struct device *dev) 225 { 226 unsigned int device_id; 227 int res; 228 229 res = regmap_read(regmap, MAX77826_REG_DEVICE_ID, &device_id); 230 if (!res) 231 dev_dbg(dev, "DEVICE_ID: 0x%x\n", device_id); 232 233 return res; 234 } 235 236 static int max77826_i2c_probe(struct i2c_client *client) 237 { 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; 243 int i; 244 245 info = devm_kzalloc(dev, sizeof(struct max77826_regulator_info), 246 GFP_KERNEL); 247 if (!info) 248 return -ENOMEM; 249 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); 255 } 256 257 info->regmap = regmap; 258 i2c_set_clientdata(client, info); 259 260 config.dev = dev; 261 config.regmap = regmap; 262 config.driver_data = info; 263 264 for (i = 0; i < MAX77826_MAX_REGULATORS; i++) { 265 rdev = devm_regulator_register(dev, 266 &max77826_regulators_desc[i], 267 &config); 268 if (IS_ERR(rdev)) { 269 dev_err(dev, "Failed to register regulator!\n"); 270 return PTR_ERR(rdev); 271 } 272 } 273 274 return max77826_read_device_id(regmap, dev); 275 } 276 277 static const struct of_device_id __maybe_unused max77826_of_match[] = { 278 { .compatible = "maxim,max77826" }, 279 { /* sentinel */ } 280 }; 281 MODULE_DEVICE_TABLE(of, max77826_of_match); 282 283 static const struct i2c_device_id max77826_id[] = { 284 { "max77826-regulator" }, 285 { /* sentinel */ } 286 }; 287 MODULE_DEVICE_TABLE(i2c, max77826_id); 288 289 static struct i2c_driver max77826_regulator_driver = { 290 .driver = { 291 .name = "max77826", 292 .of_match_table = of_match_ptr(max77826_of_match), 293 }, 294 .probe_new = 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