1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (C) 2020 ROHM Semiconductors 3 // ROHM BD9576MUF/BD9573MUF regulator driver 4 5 #include <linux/delay.h> 6 #include <linux/err.h> 7 #include <linux/gpio/consumer.h> 8 #include <linux/interrupt.h> 9 #include <linux/kernel.h> 10 #include <linux/mfd/rohm-bd957x.h> 11 #include <linux/mfd/rohm-generic.h> 12 #include <linux/module.h> 13 #include <linux/of.h> 14 #include <linux/platform_device.h> 15 #include <linux/regulator/driver.h> 16 #include <linux/regulator/machine.h> 17 #include <linux/regulator/of_regulator.h> 18 #include <linux/slab.h> 19 20 #define BD957X_VOUTS1_VOLT 3300000 21 #define BD957X_VOUTS4_BASE_VOLT 1030000 22 #define BD957X_VOUTS34_NUM_VOLT 32 23 24 static const unsigned int vout1_volt_table[] = { 25 5000000, 4900000, 4800000, 4700000, 4600000, 26 4500000, 4500000, 4500000, 5000000, 5100000, 27 5200000, 5300000, 5400000, 5500000, 5500000, 28 5500000 29 }; 30 31 static const unsigned int vout2_volt_table[] = { 32 1800000, 1780000, 1760000, 1740000, 1720000, 33 1700000, 1680000, 1660000, 1800000, 1820000, 34 1840000, 1860000, 1880000, 1900000, 1920000, 35 1940000 36 }; 37 38 static const unsigned int voutl1_volt_table[] = { 39 2500000, 2540000, 2580000, 2620000, 2660000, 40 2700000, 2740000, 2780000, 2500000, 2460000, 41 2420000, 2380000, 2340000, 2300000, 2260000, 42 2220000 43 }; 44 45 struct bd957x_regulator_data { 46 struct regulator_desc desc; 47 int base_voltage; 48 }; 49 50 static int bd957x_vout34_list_voltage(struct regulator_dev *rdev, 51 unsigned int selector) 52 { 53 const struct regulator_desc *desc = rdev->desc; 54 int multiplier = selector & desc->vsel_mask & 0x7f; 55 int tune; 56 57 /* VOUT3 and 4 has 10mV step */ 58 tune = multiplier * 10000; 59 60 if (!(selector & 0x80)) 61 return desc->fixed_uV - tune; 62 63 return desc->fixed_uV + tune; 64 } 65 66 static int bd957x_list_voltage(struct regulator_dev *rdev, 67 unsigned int selector) 68 { 69 const struct regulator_desc *desc = rdev->desc; 70 int index = selector & desc->vsel_mask & 0x7f; 71 72 if (!(selector & 0x80)) 73 index += desc->n_voltages/2; 74 75 if (index >= desc->n_voltages) 76 return -EINVAL; 77 78 return desc->volt_table[index]; 79 } 80 81 static const struct regulator_ops bd957x_vout34_ops = { 82 .is_enabled = regulator_is_enabled_regmap, 83 .list_voltage = bd957x_vout34_list_voltage, 84 .get_voltage_sel = regulator_get_voltage_sel_regmap, 85 }; 86 87 static const struct regulator_ops bd957X_vouts1_regulator_ops = { 88 .is_enabled = regulator_is_enabled_regmap, 89 }; 90 91 static const struct regulator_ops bd957x_ops = { 92 .is_enabled = regulator_is_enabled_regmap, 93 .list_voltage = bd957x_list_voltage, 94 .get_voltage_sel = regulator_get_voltage_sel_regmap, 95 }; 96 97 static struct bd957x_regulator_data bd9576_regulators[] = { 98 { 99 .desc = { 100 .name = "VD50", 101 .of_match = of_match_ptr("regulator-vd50"), 102 .regulators_node = of_match_ptr("regulators"), 103 .id = BD957X_VD50, 104 .type = REGULATOR_VOLTAGE, 105 .ops = &bd957x_ops, 106 .volt_table = &vout1_volt_table[0], 107 .n_voltages = ARRAY_SIZE(vout1_volt_table), 108 .vsel_reg = BD957X_REG_VOUT1_TUNE, 109 .vsel_mask = BD957X_MASK_VOUT1_TUNE, 110 .enable_reg = BD957X_REG_POW_TRIGGER1, 111 .enable_mask = BD957X_REGULATOR_EN_MASK, 112 .enable_val = BD957X_REGULATOR_DIS_VAL, 113 .enable_is_inverted = true, 114 .owner = THIS_MODULE, 115 }, 116 }, 117 { 118 .desc = { 119 .name = "VD18", 120 .of_match = of_match_ptr("regulator-vd18"), 121 .regulators_node = of_match_ptr("regulators"), 122 .id = BD957X_VD18, 123 .type = REGULATOR_VOLTAGE, 124 .ops = &bd957x_ops, 125 .volt_table = &vout2_volt_table[0], 126 .n_voltages = ARRAY_SIZE(vout2_volt_table), 127 .vsel_reg = BD957X_REG_VOUT2_TUNE, 128 .vsel_mask = BD957X_MASK_VOUT2_TUNE, 129 .enable_reg = BD957X_REG_POW_TRIGGER2, 130 .enable_mask = BD957X_REGULATOR_EN_MASK, 131 .enable_val = BD957X_REGULATOR_DIS_VAL, 132 .enable_is_inverted = true, 133 .owner = THIS_MODULE, 134 }, 135 }, 136 { 137 .desc = { 138 .name = "VDDDR", 139 .of_match = of_match_ptr("regulator-vdddr"), 140 .regulators_node = of_match_ptr("regulators"), 141 .id = BD957X_VDDDR, 142 .ops = &bd957x_vout34_ops, 143 .type = REGULATOR_VOLTAGE, 144 .n_voltages = BD957X_VOUTS34_NUM_VOLT, 145 .vsel_reg = BD957X_REG_VOUT3_TUNE, 146 .vsel_mask = BD957X_MASK_VOUT3_TUNE, 147 .enable_reg = BD957X_REG_POW_TRIGGER3, 148 .enable_mask = BD957X_REGULATOR_EN_MASK, 149 .enable_val = BD957X_REGULATOR_DIS_VAL, 150 .enable_is_inverted = true, 151 .owner = THIS_MODULE, 152 }, 153 }, 154 { 155 .desc = { 156 .name = "VD10", 157 .of_match = of_match_ptr("regulator-vd10"), 158 .regulators_node = of_match_ptr("regulators"), 159 .id = BD957X_VD10, 160 .ops = &bd957x_vout34_ops, 161 .type = REGULATOR_VOLTAGE, 162 .fixed_uV = BD957X_VOUTS4_BASE_VOLT, 163 .n_voltages = BD957X_VOUTS34_NUM_VOLT, 164 .vsel_reg = BD957X_REG_VOUT4_TUNE, 165 .vsel_mask = BD957X_MASK_VOUT4_TUNE, 166 .enable_reg = BD957X_REG_POW_TRIGGER4, 167 .enable_mask = BD957X_REGULATOR_EN_MASK, 168 .enable_val = BD957X_REGULATOR_DIS_VAL, 169 .enable_is_inverted = true, 170 .owner = THIS_MODULE, 171 }, 172 }, 173 { 174 .desc = { 175 .name = "VOUTL1", 176 .of_match = of_match_ptr("regulator-voutl1"), 177 .regulators_node = of_match_ptr("regulators"), 178 .id = BD957X_VOUTL1, 179 .ops = &bd957x_ops, 180 .type = REGULATOR_VOLTAGE, 181 .volt_table = &voutl1_volt_table[0], 182 .n_voltages = ARRAY_SIZE(voutl1_volt_table), 183 .vsel_reg = BD957X_REG_VOUTL1_TUNE, 184 .vsel_mask = BD957X_MASK_VOUTL1_TUNE, 185 .enable_reg = BD957X_REG_POW_TRIGGERL1, 186 .enable_mask = BD957X_REGULATOR_EN_MASK, 187 .enable_val = BD957X_REGULATOR_DIS_VAL, 188 .enable_is_inverted = true, 189 .owner = THIS_MODULE, 190 }, 191 }, 192 { 193 .desc = { 194 .name = "VOUTS1", 195 .of_match = of_match_ptr("regulator-vouts1"), 196 .regulators_node = of_match_ptr("regulators"), 197 .id = BD957X_VOUTS1, 198 .ops = &bd957X_vouts1_regulator_ops, 199 .type = REGULATOR_VOLTAGE, 200 .n_voltages = 1, 201 .fixed_uV = BD957X_VOUTS1_VOLT, 202 .enable_reg = BD957X_REG_POW_TRIGGERS1, 203 .enable_mask = BD957X_REGULATOR_EN_MASK, 204 .enable_val = BD957X_REGULATOR_DIS_VAL, 205 .enable_is_inverted = true, 206 .owner = THIS_MODULE, 207 }, 208 }, 209 }; 210 211 static int bd957x_probe(struct platform_device *pdev) 212 { 213 struct regmap *regmap; 214 struct regulator_config config = { 0 }; 215 int i; 216 bool vout_mode, ddr_sel; 217 const struct bd957x_regulator_data *reg_data = &bd9576_regulators[0]; 218 unsigned int num_reg_data = ARRAY_SIZE(bd9576_regulators); 219 enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data; 220 221 regmap = dev_get_regmap(pdev->dev.parent, NULL); 222 if (!regmap) { 223 dev_err(&pdev->dev, "No regmap\n"); 224 return -EINVAL; 225 } 226 vout_mode = of_property_read_bool(pdev->dev.parent->of_node, 227 "rohm,vout1-en-low"); 228 if (vout_mode) { 229 struct gpio_desc *en; 230 231 dev_dbg(&pdev->dev, "GPIO controlled mode\n"); 232 233 /* VOUT1 enable state judged by VOUT1_EN pin */ 234 /* See if we have GPIO defined */ 235 en = devm_gpiod_get_from_of_node(&pdev->dev, 236 pdev->dev.parent->of_node, 237 "rohm,vout1-en-gpios", 0, 238 GPIOD_OUT_LOW, "vout1-en"); 239 if (!IS_ERR(en)) { 240 /* VOUT1_OPS gpio ctrl */ 241 /* 242 * Regulator core prioritizes the ena_gpio over 243 * enable/disable/is_enabled callbacks so no need to 244 * clear them. We can still use same ops 245 */ 246 config.ena_gpiod = en; 247 } else { 248 /* 249 * In theory it is possible someone wants to set 250 * vout1-en LOW during OTP loading and set VOUT1 to be 251 * controlled by GPIO - but control the GPIO from some 252 * where else than this driver. For that to work we 253 * should unset the is_enabled callback here. 254 * 255 * I believe such case where rohm,vout1-en-low is set 256 * and vout1-en-gpios is not is likely to be a 257 * misconfiguration. So let's just err out for now. 258 */ 259 dev_err(&pdev->dev, 260 "Failed to get VOUT1 control GPIO\n"); 261 return PTR_ERR(en); 262 } 263 } 264 265 /* 266 * If more than one PMIC needs to be controlled by same processor then 267 * allocate the regulator data array here and use bd9576_regulators as 268 * template. At the moment I see no such use-case so I spare some 269 * bytes and use bd9576_regulators directly for non-constant configs 270 * like DDR voltage selection. 271 */ 272 ddr_sel = of_property_read_bool(pdev->dev.parent->of_node, 273 "rohm,ddr-sel-low"); 274 if (ddr_sel) 275 bd9576_regulators[2].desc.fixed_uV = 1350000; 276 else 277 bd9576_regulators[2].desc.fixed_uV = 1500000; 278 279 switch (chip) { 280 case ROHM_CHIP_TYPE_BD9576: 281 dev_dbg(&pdev->dev, "Found BD9576MUF\n"); 282 break; 283 case ROHM_CHIP_TYPE_BD9573: 284 dev_dbg(&pdev->dev, "Found BD9573MUF\n"); 285 break; 286 default: 287 dev_err(&pdev->dev, "Unsupported chip type\n"); 288 return -EINVAL; 289 } 290 291 config.dev = pdev->dev.parent; 292 config.regmap = regmap; 293 294 for (i = 0; i < num_reg_data; i++) { 295 296 const struct regulator_desc *desc; 297 struct regulator_dev *rdev; 298 const struct bd957x_regulator_data *r; 299 300 r = ®_data[i]; 301 desc = &r->desc; 302 303 rdev = devm_regulator_register(&pdev->dev, desc, &config); 304 if (IS_ERR(rdev)) { 305 dev_err(&pdev->dev, 306 "failed to register %s regulator\n", 307 desc->name); 308 return PTR_ERR(rdev); 309 } 310 /* 311 * Clear the VOUT1 GPIO setting - rest of the regulators do not 312 * support GPIO control 313 */ 314 config.ena_gpiod = NULL; 315 } 316 317 return 0; 318 } 319 320 static const struct platform_device_id bd957x_pmic_id[] = { 321 { "bd9573-pmic", ROHM_CHIP_TYPE_BD9573 }, 322 { "bd9576-pmic", ROHM_CHIP_TYPE_BD9576 }, 323 { }, 324 }; 325 MODULE_DEVICE_TABLE(platform, bd957x_pmic_id); 326 327 static struct platform_driver bd957x_regulator = { 328 .driver = { 329 .name = "bd957x-pmic", 330 }, 331 .probe = bd957x_probe, 332 .id_table = bd957x_pmic_id, 333 }; 334 335 module_platform_driver(bd957x_regulator); 336 337 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>"); 338 MODULE_DESCRIPTION("ROHM BD9576/BD9573 voltage regulator driver"); 339 MODULE_LICENSE("GPL"); 340 MODULE_ALIAS("platform:bd957x-pmic"); 341