1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // max14577.c - Regulator driver for the Maxim 14577/77836 4 // 5 // Copyright (C) 2013,2014 Samsung Electronics 6 // Krzysztof Kozlowski <krzk@kernel.org> 7 8 #include <linux/module.h> 9 #include <linux/platform_device.h> 10 #include <linux/regulator/driver.h> 11 #include <linux/mfd/max14577.h> 12 #include <linux/mfd/max14577-private.h> 13 #include <linux/regulator/of_regulator.h> 14 15 static int max14577_reg_is_enabled(struct regulator_dev *rdev) 16 { 17 int rid = rdev_get_id(rdev); 18 struct regmap *rmap = rdev->regmap; 19 u8 reg_data; 20 21 switch (rid) { 22 case MAX14577_CHARGER: 23 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, ®_data); 24 if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0) 25 return 0; 26 max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, ®_data); 27 if ((reg_data & STATUS3_CGMBC_MASK) == 0) 28 return 0; 29 /* MBCHOSTEN and CGMBC are on */ 30 return 1; 31 default: 32 return -EINVAL; 33 } 34 } 35 36 static int max14577_reg_get_current_limit(struct regulator_dev *rdev) 37 { 38 u8 reg_data; 39 struct regmap *rmap = rdev->regmap; 40 struct max14577 *max14577 = rdev_get_drvdata(rdev); 41 const struct maxim_charger_current *limits = 42 &maxim_charger_currents[max14577->dev_type]; 43 44 if (rdev_get_id(rdev) != MAX14577_CHARGER) 45 return -EINVAL; 46 47 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, ®_data); 48 49 if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0) 50 return limits->min; 51 52 reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >> 53 CHGCTRL4_MBCICHWRCH_SHIFT); 54 return limits->high_start + reg_data * limits->high_step; 55 } 56 57 static int max14577_reg_set_current_limit(struct regulator_dev *rdev, 58 int min_uA, int max_uA) 59 { 60 u8 reg_data; 61 int ret; 62 struct max14577 *max14577 = rdev_get_drvdata(rdev); 63 const struct maxim_charger_current *limits = 64 &maxim_charger_currents[max14577->dev_type]; 65 66 if (rdev_get_id(rdev) != MAX14577_CHARGER) 67 return -EINVAL; 68 69 ret = maxim_charger_calc_reg_current(limits, min_uA, max_uA, ®_data); 70 if (ret) 71 return ret; 72 73 return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4, 74 CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK, 75 reg_data); 76 } 77 78 static const struct regulator_ops max14577_safeout_ops = { 79 .is_enabled = regulator_is_enabled_regmap, 80 .enable = regulator_enable_regmap, 81 .disable = regulator_disable_regmap, 82 .list_voltage = regulator_list_voltage_linear, 83 }; 84 85 static const struct regulator_ops max14577_charger_ops = { 86 .is_enabled = max14577_reg_is_enabled, 87 .enable = regulator_enable_regmap, 88 .disable = regulator_disable_regmap, 89 .get_current_limit = max14577_reg_get_current_limit, 90 .set_current_limit = max14577_reg_set_current_limit, 91 }; 92 93 #define MAX14577_SAFEOUT_REG { \ 94 .name = "SAFEOUT", \ 95 .of_match = of_match_ptr("SAFEOUT"), \ 96 .regulators_node = of_match_ptr("regulators"), \ 97 .id = MAX14577_SAFEOUT, \ 98 .ops = &max14577_safeout_ops, \ 99 .type = REGULATOR_VOLTAGE, \ 100 .owner = THIS_MODULE, \ 101 .n_voltages = 1, \ 102 .min_uV = MAX14577_REGULATOR_SAFEOUT_VOLTAGE, \ 103 .enable_reg = MAX14577_REG_CONTROL2, \ 104 .enable_mask = CTRL2_SFOUTORD_MASK, \ 105 } 106 #define MAX14577_CHARGER_REG { \ 107 .name = "CHARGER", \ 108 .of_match = of_match_ptr("CHARGER"), \ 109 .regulators_node = of_match_ptr("regulators"), \ 110 .id = MAX14577_CHARGER, \ 111 .ops = &max14577_charger_ops, \ 112 .type = REGULATOR_CURRENT, \ 113 .owner = THIS_MODULE, \ 114 .enable_reg = MAX14577_CHG_REG_CHG_CTRL2, \ 115 .enable_mask = CHGCTRL2_MBCHOSTEN_MASK, \ 116 } 117 118 static const struct regulator_desc max14577_supported_regulators[] = { 119 [MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG, 120 [MAX14577_CHARGER] = MAX14577_CHARGER_REG, 121 }; 122 123 static const struct regulator_ops max77836_ldo_ops = { 124 .is_enabled = regulator_is_enabled_regmap, 125 .enable = regulator_enable_regmap, 126 .disable = regulator_disable_regmap, 127 .list_voltage = regulator_list_voltage_linear, 128 .map_voltage = regulator_map_voltage_linear, 129 .get_voltage_sel = regulator_get_voltage_sel_regmap, 130 .set_voltage_sel = regulator_set_voltage_sel_regmap, 131 /* TODO: add .set_suspend_mode */ 132 }; 133 134 #define MAX77836_LDO_REG(num) { \ 135 .name = "LDO" # num, \ 136 .of_match = of_match_ptr("LDO" # num), \ 137 .regulators_node = of_match_ptr("regulators"), \ 138 .id = MAX77836_LDO ## num, \ 139 .ops = &max77836_ldo_ops, \ 140 .type = REGULATOR_VOLTAGE, \ 141 .owner = THIS_MODULE, \ 142 .n_voltages = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM, \ 143 .min_uV = MAX77836_REGULATOR_LDO_VOLTAGE_MIN, \ 144 .uV_step = MAX77836_REGULATOR_LDO_VOLTAGE_STEP, \ 145 .enable_reg = MAX77836_LDO_REG_CNFG1_LDO ## num, \ 146 .enable_mask = MAX77836_CNFG1_LDO_PWRMD_MASK, \ 147 .vsel_reg = MAX77836_LDO_REG_CNFG1_LDO ## num, \ 148 .vsel_mask = MAX77836_CNFG1_LDO_TV_MASK, \ 149 } 150 151 static const struct regulator_desc max77836_supported_regulators[] = { 152 [MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG, 153 [MAX14577_CHARGER] = MAX14577_CHARGER_REG, 154 [MAX77836_LDO1] = MAX77836_LDO_REG(1), 155 [MAX77836_LDO2] = MAX77836_LDO_REG(2), 156 }; 157 158 #ifdef CONFIG_OF 159 static struct of_regulator_match max14577_regulator_matches[] = { 160 { .name = "SAFEOUT", }, 161 { .name = "CHARGER", }, 162 }; 163 164 static struct of_regulator_match max77836_regulator_matches[] = { 165 { .name = "SAFEOUT", }, 166 { .name = "CHARGER", }, 167 { .name = "LDO1", }, 168 { .name = "LDO2", }, 169 }; 170 171 static inline struct regulator_init_data *match_init_data(int index, 172 enum maxim_device_type dev_type) 173 { 174 switch (dev_type) { 175 case MAXIM_DEVICE_TYPE_MAX77836: 176 return max77836_regulator_matches[index].init_data; 177 178 case MAXIM_DEVICE_TYPE_MAX14577: 179 default: 180 return max14577_regulator_matches[index].init_data; 181 } 182 } 183 184 static inline struct device_node *match_of_node(int index, 185 enum maxim_device_type dev_type) 186 { 187 switch (dev_type) { 188 case MAXIM_DEVICE_TYPE_MAX77836: 189 return max77836_regulator_matches[index].of_node; 190 191 case MAXIM_DEVICE_TYPE_MAX14577: 192 default: 193 return max14577_regulator_matches[index].of_node; 194 } 195 } 196 #else /* CONFIG_OF */ 197 static inline struct regulator_init_data *match_init_data(int index, 198 enum maxim_device_type dev_type) 199 { 200 return NULL; 201 } 202 203 static inline struct device_node *match_of_node(int index, 204 enum maxim_device_type dev_type) 205 { 206 return NULL; 207 } 208 #endif /* CONFIG_OF */ 209 210 /** 211 * Registers for regulators of max77836 use different I2C slave addresses so 212 * different regmaps must be used for them. 213 * 214 * Returns proper regmap for accessing regulator passed by id. 215 */ 216 static struct regmap *max14577_get_regmap(struct max14577 *max14577, 217 int reg_id) 218 { 219 switch (max14577->dev_type) { 220 case MAXIM_DEVICE_TYPE_MAX77836: 221 switch (reg_id) { 222 case MAX77836_SAFEOUT ... MAX77836_CHARGER: 223 return max14577->regmap; 224 default: 225 /* MAX77836_LDO1 ... MAX77836_LDO2 */ 226 return max14577->regmap_pmic; 227 } 228 229 case MAXIM_DEVICE_TYPE_MAX14577: 230 default: 231 return max14577->regmap; 232 } 233 } 234 235 static int max14577_regulator_probe(struct platform_device *pdev) 236 { 237 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent); 238 struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev); 239 int i, ret = 0; 240 struct regulator_config config = {}; 241 const struct regulator_desc *supported_regulators; 242 unsigned int supported_regulators_size; 243 enum maxim_device_type dev_type = max14577->dev_type; 244 245 switch (dev_type) { 246 case MAXIM_DEVICE_TYPE_MAX77836: 247 supported_regulators = max77836_supported_regulators; 248 supported_regulators_size = ARRAY_SIZE(max77836_supported_regulators); 249 break; 250 case MAXIM_DEVICE_TYPE_MAX14577: 251 default: 252 supported_regulators = max14577_supported_regulators; 253 supported_regulators_size = ARRAY_SIZE(max14577_supported_regulators); 254 } 255 256 config.dev = max14577->dev; 257 config.driver_data = max14577; 258 259 for (i = 0; i < supported_regulators_size; i++) { 260 struct regulator_dev *regulator; 261 /* 262 * Index of supported_regulators[] is also the id and must 263 * match index of pdata->regulators[]. 264 */ 265 if (pdata && pdata->regulators) { 266 config.init_data = pdata->regulators[i].initdata; 267 config.of_node = pdata->regulators[i].of_node; 268 } else { 269 config.init_data = match_init_data(i, dev_type); 270 config.of_node = match_of_node(i, dev_type); 271 } 272 config.regmap = max14577_get_regmap(max14577, 273 supported_regulators[i].id); 274 275 regulator = devm_regulator_register(&pdev->dev, 276 &supported_regulators[i], &config); 277 if (IS_ERR(regulator)) { 278 ret = PTR_ERR(regulator); 279 dev_err(&pdev->dev, 280 "Regulator init failed for %d/%s with error: %d\n", 281 i, supported_regulators[i].name, ret); 282 return ret; 283 } 284 } 285 286 return ret; 287 } 288 289 static const struct platform_device_id max14577_regulator_id[] = { 290 { "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577, }, 291 { "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836, }, 292 { } 293 }; 294 MODULE_DEVICE_TABLE(platform, max14577_regulator_id); 295 296 static struct platform_driver max14577_regulator_driver = { 297 .driver = { 298 .name = "max14577-regulator", 299 }, 300 .probe = max14577_regulator_probe, 301 .id_table = max14577_regulator_id, 302 }; 303 304 static int __init max14577_regulator_init(void) 305 { 306 BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators) != MAX14577_REGULATOR_NUM); 307 BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators) != MAX77836_REGULATOR_NUM); 308 309 BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN + 310 (MAX77836_REGULATOR_LDO_VOLTAGE_STEP * 311 (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM - 1)) != 312 MAX77836_REGULATOR_LDO_VOLTAGE_MAX); 313 314 return platform_driver_register(&max14577_regulator_driver); 315 } 316 subsys_initcall(max14577_regulator_init); 317 318 static void __exit max14577_regulator_exit(void) 319 { 320 platform_driver_unregister(&max14577_regulator_driver); 321 } 322 module_exit(max14577_regulator_exit); 323 324 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>"); 325 MODULE_DESCRIPTION("Maxim 14577/77836 regulator driver"); 326 MODULE_LICENSE("GPL"); 327 MODULE_ALIAS("platform:max14577-regulator"); 328