1 /* 2 * s2mps11.c 3 * 4 * Copyright (c) 2012-2014 Samsung Electronics Co., Ltd 5 * http://www.samsung.com 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the 9 * Free Software Foundation; either version 2 of the License, or (at your 10 * option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 */ 18 19 #include <linux/bug.h> 20 #include <linux/err.h> 21 #include <linux/gpio/consumer.h> 22 #include <linux/slab.h> 23 #include <linux/module.h> 24 #include <linux/of.h> 25 #include <linux/regmap.h> 26 #include <linux/platform_device.h> 27 #include <linux/regulator/driver.h> 28 #include <linux/regulator/machine.h> 29 #include <linux/regulator/of_regulator.h> 30 #include <linux/mfd/samsung/core.h> 31 #include <linux/mfd/samsung/s2mps11.h> 32 #include <linux/mfd/samsung/s2mps13.h> 33 #include <linux/mfd/samsung/s2mps14.h> 34 #include <linux/mfd/samsung/s2mps15.h> 35 #include <linux/mfd/samsung/s2mpu02.h> 36 37 /* The highest number of possible regulators for supported devices. */ 38 #define S2MPS_REGULATOR_MAX S2MPS13_REGULATOR_MAX 39 struct s2mps11_info { 40 int ramp_delay2; 41 int ramp_delay34; 42 int ramp_delay5; 43 int ramp_delay16; 44 int ramp_delay7810; 45 int ramp_delay9; 46 47 enum sec_device_type dev_type; 48 49 /* 50 * One bit for each S2MPS13/S2MPS14/S2MPU02 regulator whether 51 * the suspend mode was enabled. 52 */ 53 DECLARE_BITMAP(suspend_state, S2MPS_REGULATOR_MAX); 54 55 /* 56 * Array (size: number of regulators) with GPIO-s for external 57 * sleep control. 58 */ 59 struct gpio_desc **ext_control_gpiod; 60 }; 61 62 static int get_ramp_delay(int ramp_delay) 63 { 64 unsigned char cnt = 0; 65 66 ramp_delay /= 6250; 67 68 while (true) { 69 ramp_delay = ramp_delay >> 1; 70 if (ramp_delay == 0) 71 break; 72 cnt++; 73 } 74 75 if (cnt > 3) 76 cnt = 3; 77 78 return cnt; 79 } 80 81 static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev, 82 unsigned int old_selector, 83 unsigned int new_selector) 84 { 85 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev); 86 unsigned int ramp_delay = 0; 87 int old_volt, new_volt; 88 89 switch (rdev_get_id(rdev)) { 90 case S2MPS11_BUCK2: 91 ramp_delay = s2mps11->ramp_delay2; 92 break; 93 case S2MPS11_BUCK3: 94 case S2MPS11_BUCK4: 95 ramp_delay = s2mps11->ramp_delay34; 96 break; 97 case S2MPS11_BUCK5: 98 ramp_delay = s2mps11->ramp_delay5; 99 break; 100 case S2MPS11_BUCK6: 101 case S2MPS11_BUCK1: 102 ramp_delay = s2mps11->ramp_delay16; 103 break; 104 case S2MPS11_BUCK7: 105 case S2MPS11_BUCK8: 106 case S2MPS11_BUCK10: 107 ramp_delay = s2mps11->ramp_delay7810; 108 break; 109 case S2MPS11_BUCK9: 110 ramp_delay = s2mps11->ramp_delay9; 111 } 112 113 if (ramp_delay == 0) 114 ramp_delay = rdev->desc->ramp_delay; 115 116 old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector); 117 new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector); 118 119 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay); 120 } 121 122 static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay) 123 { 124 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev); 125 unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK; 126 unsigned int ramp_enable = 1, enable_shift = 0; 127 int ret; 128 129 switch (rdev_get_id(rdev)) { 130 case S2MPS11_BUCK1: 131 if (ramp_delay > s2mps11->ramp_delay16) 132 s2mps11->ramp_delay16 = ramp_delay; 133 else 134 ramp_delay = s2mps11->ramp_delay16; 135 136 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT; 137 break; 138 case S2MPS11_BUCK2: 139 enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT; 140 if (!ramp_delay) { 141 ramp_enable = 0; 142 break; 143 } 144 145 s2mps11->ramp_delay2 = ramp_delay; 146 ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT; 147 ramp_reg = S2MPS11_REG_RAMP; 148 break; 149 case S2MPS11_BUCK3: 150 enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT; 151 if (!ramp_delay) { 152 ramp_enable = 0; 153 break; 154 } 155 156 if (ramp_delay > s2mps11->ramp_delay34) 157 s2mps11->ramp_delay34 = ramp_delay; 158 else 159 ramp_delay = s2mps11->ramp_delay34; 160 161 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT; 162 ramp_reg = S2MPS11_REG_RAMP; 163 break; 164 case S2MPS11_BUCK4: 165 enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT; 166 if (!ramp_delay) { 167 ramp_enable = 0; 168 break; 169 } 170 171 if (ramp_delay > s2mps11->ramp_delay34) 172 s2mps11->ramp_delay34 = ramp_delay; 173 else 174 ramp_delay = s2mps11->ramp_delay34; 175 176 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT; 177 ramp_reg = S2MPS11_REG_RAMP; 178 break; 179 case S2MPS11_BUCK5: 180 s2mps11->ramp_delay5 = ramp_delay; 181 ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT; 182 break; 183 case S2MPS11_BUCK6: 184 enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT; 185 if (!ramp_delay) { 186 ramp_enable = 0; 187 break; 188 } 189 190 if (ramp_delay > s2mps11->ramp_delay16) 191 s2mps11->ramp_delay16 = ramp_delay; 192 else 193 ramp_delay = s2mps11->ramp_delay16; 194 195 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT; 196 break; 197 case S2MPS11_BUCK7: 198 case S2MPS11_BUCK8: 199 case S2MPS11_BUCK10: 200 if (ramp_delay > s2mps11->ramp_delay7810) 201 s2mps11->ramp_delay7810 = ramp_delay; 202 else 203 ramp_delay = s2mps11->ramp_delay7810; 204 205 ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT; 206 break; 207 case S2MPS11_BUCK9: 208 s2mps11->ramp_delay9 = ramp_delay; 209 ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT; 210 break; 211 default: 212 return 0; 213 } 214 215 if (!ramp_enable) 216 goto ramp_disable; 217 218 /* Ramp delay can be enabled/disabled only for buck[2346] */ 219 if ((rdev_get_id(rdev) >= S2MPS11_BUCK2 && 220 rdev_get_id(rdev) <= S2MPS11_BUCK4) || 221 rdev_get_id(rdev) == S2MPS11_BUCK6) { 222 ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP, 223 1 << enable_shift, 1 << enable_shift); 224 if (ret) { 225 dev_err(&rdev->dev, "failed to enable ramp rate\n"); 226 return ret; 227 } 228 } 229 230 ramp_val = get_ramp_delay(ramp_delay); 231 232 return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift, 233 ramp_val << ramp_shift); 234 235 ramp_disable: 236 return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP, 237 1 << enable_shift, 0); 238 } 239 240 static const struct regulator_ops s2mps11_ldo_ops = { 241 .list_voltage = regulator_list_voltage_linear, 242 .map_voltage = regulator_map_voltage_linear, 243 .is_enabled = regulator_is_enabled_regmap, 244 .enable = regulator_enable_regmap, 245 .disable = regulator_disable_regmap, 246 .get_voltage_sel = regulator_get_voltage_sel_regmap, 247 .set_voltage_sel = regulator_set_voltage_sel_regmap, 248 .set_voltage_time_sel = regulator_set_voltage_time_sel, 249 }; 250 251 static const struct regulator_ops s2mps11_buck_ops = { 252 .list_voltage = regulator_list_voltage_linear, 253 .map_voltage = regulator_map_voltage_linear, 254 .is_enabled = regulator_is_enabled_regmap, 255 .enable = regulator_enable_regmap, 256 .disable = regulator_disable_regmap, 257 .get_voltage_sel = regulator_get_voltage_sel_regmap, 258 .set_voltage_sel = regulator_set_voltage_sel_regmap, 259 .set_voltage_time_sel = s2mps11_regulator_set_voltage_time_sel, 260 .set_ramp_delay = s2mps11_set_ramp_delay, 261 }; 262 263 #define regulator_desc_s2mps11_ldo(num, step) { \ 264 .name = "LDO"#num, \ 265 .id = S2MPS11_LDO##num, \ 266 .ops = &s2mps11_ldo_ops, \ 267 .type = REGULATOR_VOLTAGE, \ 268 .owner = THIS_MODULE, \ 269 .ramp_delay = RAMP_DELAY_12_MVUS, \ 270 .min_uV = MIN_800_MV, \ 271 .uV_step = step, \ 272 .n_voltages = S2MPS11_LDO_N_VOLTAGES, \ 273 .vsel_reg = S2MPS11_REG_L1CTRL + num - 1, \ 274 .vsel_mask = S2MPS11_LDO_VSEL_MASK, \ 275 .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \ 276 .enable_mask = S2MPS11_ENABLE_MASK \ 277 } 278 279 #define regulator_desc_s2mps11_buck1_4(num) { \ 280 .name = "BUCK"#num, \ 281 .id = S2MPS11_BUCK##num, \ 282 .ops = &s2mps11_buck_ops, \ 283 .type = REGULATOR_VOLTAGE, \ 284 .owner = THIS_MODULE, \ 285 .min_uV = MIN_600_MV, \ 286 .uV_step = STEP_6_25_MV, \ 287 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \ 288 .ramp_delay = S2MPS11_RAMP_DELAY, \ 289 .vsel_reg = S2MPS11_REG_B1CTRL2 + (num - 1) * 2, \ 290 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \ 291 .enable_reg = S2MPS11_REG_B1CTRL1 + (num - 1) * 2, \ 292 .enable_mask = S2MPS11_ENABLE_MASK \ 293 } 294 295 #define regulator_desc_s2mps11_buck5 { \ 296 .name = "BUCK5", \ 297 .id = S2MPS11_BUCK5, \ 298 .ops = &s2mps11_buck_ops, \ 299 .type = REGULATOR_VOLTAGE, \ 300 .owner = THIS_MODULE, \ 301 .min_uV = MIN_600_MV, \ 302 .uV_step = STEP_6_25_MV, \ 303 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \ 304 .ramp_delay = S2MPS11_RAMP_DELAY, \ 305 .vsel_reg = S2MPS11_REG_B5CTRL2, \ 306 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \ 307 .enable_reg = S2MPS11_REG_B5CTRL1, \ 308 .enable_mask = S2MPS11_ENABLE_MASK \ 309 } 310 311 #define regulator_desc_s2mps11_buck67810(num, min, step) { \ 312 .name = "BUCK"#num, \ 313 .id = S2MPS11_BUCK##num, \ 314 .ops = &s2mps11_buck_ops, \ 315 .type = REGULATOR_VOLTAGE, \ 316 .owner = THIS_MODULE, \ 317 .min_uV = min, \ 318 .uV_step = step, \ 319 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \ 320 .ramp_delay = S2MPS11_RAMP_DELAY, \ 321 .vsel_reg = S2MPS11_REG_B6CTRL2 + (num - 6) * 2, \ 322 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \ 323 .enable_reg = S2MPS11_REG_B6CTRL1 + (num - 6) * 2, \ 324 .enable_mask = S2MPS11_ENABLE_MASK \ 325 } 326 327 #define regulator_desc_s2mps11_buck9 { \ 328 .name = "BUCK9", \ 329 .id = S2MPS11_BUCK9, \ 330 .ops = &s2mps11_buck_ops, \ 331 .type = REGULATOR_VOLTAGE, \ 332 .owner = THIS_MODULE, \ 333 .min_uV = MIN_3000_MV, \ 334 .uV_step = STEP_25_MV, \ 335 .n_voltages = S2MPS11_BUCK9_N_VOLTAGES, \ 336 .ramp_delay = S2MPS11_RAMP_DELAY, \ 337 .vsel_reg = S2MPS11_REG_B9CTRL2, \ 338 .vsel_mask = S2MPS11_BUCK9_VSEL_MASK, \ 339 .enable_reg = S2MPS11_REG_B9CTRL1, \ 340 .enable_mask = S2MPS11_ENABLE_MASK \ 341 } 342 343 static const struct regulator_desc s2mps11_regulators[] = { 344 regulator_desc_s2mps11_ldo(1, STEP_25_MV), 345 regulator_desc_s2mps11_ldo(2, STEP_50_MV), 346 regulator_desc_s2mps11_ldo(3, STEP_50_MV), 347 regulator_desc_s2mps11_ldo(4, STEP_50_MV), 348 regulator_desc_s2mps11_ldo(5, STEP_50_MV), 349 regulator_desc_s2mps11_ldo(6, STEP_25_MV), 350 regulator_desc_s2mps11_ldo(7, STEP_50_MV), 351 regulator_desc_s2mps11_ldo(8, STEP_50_MV), 352 regulator_desc_s2mps11_ldo(9, STEP_50_MV), 353 regulator_desc_s2mps11_ldo(10, STEP_50_MV), 354 regulator_desc_s2mps11_ldo(11, STEP_25_MV), 355 regulator_desc_s2mps11_ldo(12, STEP_50_MV), 356 regulator_desc_s2mps11_ldo(13, STEP_50_MV), 357 regulator_desc_s2mps11_ldo(14, STEP_50_MV), 358 regulator_desc_s2mps11_ldo(15, STEP_50_MV), 359 regulator_desc_s2mps11_ldo(16, STEP_50_MV), 360 regulator_desc_s2mps11_ldo(17, STEP_50_MV), 361 regulator_desc_s2mps11_ldo(18, STEP_50_MV), 362 regulator_desc_s2mps11_ldo(19, STEP_50_MV), 363 regulator_desc_s2mps11_ldo(20, STEP_50_MV), 364 regulator_desc_s2mps11_ldo(21, STEP_50_MV), 365 regulator_desc_s2mps11_ldo(22, STEP_25_MV), 366 regulator_desc_s2mps11_ldo(23, STEP_25_MV), 367 regulator_desc_s2mps11_ldo(24, STEP_50_MV), 368 regulator_desc_s2mps11_ldo(25, STEP_50_MV), 369 regulator_desc_s2mps11_ldo(26, STEP_50_MV), 370 regulator_desc_s2mps11_ldo(27, STEP_25_MV), 371 regulator_desc_s2mps11_ldo(28, STEP_50_MV), 372 regulator_desc_s2mps11_ldo(29, STEP_50_MV), 373 regulator_desc_s2mps11_ldo(30, STEP_50_MV), 374 regulator_desc_s2mps11_ldo(31, STEP_50_MV), 375 regulator_desc_s2mps11_ldo(32, STEP_50_MV), 376 regulator_desc_s2mps11_ldo(33, STEP_50_MV), 377 regulator_desc_s2mps11_ldo(34, STEP_50_MV), 378 regulator_desc_s2mps11_ldo(35, STEP_50_MV), 379 regulator_desc_s2mps11_ldo(36, STEP_50_MV), 380 regulator_desc_s2mps11_ldo(37, STEP_50_MV), 381 regulator_desc_s2mps11_ldo(38, STEP_50_MV), 382 regulator_desc_s2mps11_buck1_4(1), 383 regulator_desc_s2mps11_buck1_4(2), 384 regulator_desc_s2mps11_buck1_4(3), 385 regulator_desc_s2mps11_buck1_4(4), 386 regulator_desc_s2mps11_buck5, 387 regulator_desc_s2mps11_buck67810(6, MIN_600_MV, STEP_6_25_MV), 388 regulator_desc_s2mps11_buck67810(7, MIN_600_MV, STEP_6_25_MV), 389 regulator_desc_s2mps11_buck67810(8, MIN_600_MV, STEP_6_25_MV), 390 regulator_desc_s2mps11_buck9, 391 regulator_desc_s2mps11_buck67810(10, MIN_750_MV, STEP_12_5_MV), 392 }; 393 394 static const struct regulator_ops s2mps14_reg_ops; 395 396 #define regulator_desc_s2mps13_ldo(num, min, step, min_sel) { \ 397 .name = "LDO"#num, \ 398 .id = S2MPS13_LDO##num, \ 399 .ops = &s2mps14_reg_ops, \ 400 .type = REGULATOR_VOLTAGE, \ 401 .owner = THIS_MODULE, \ 402 .min_uV = min, \ 403 .uV_step = step, \ 404 .linear_min_sel = min_sel, \ 405 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \ 406 .vsel_reg = S2MPS13_REG_L1CTRL + num - 1, \ 407 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \ 408 .enable_reg = S2MPS13_REG_L1CTRL + num - 1, \ 409 .enable_mask = S2MPS14_ENABLE_MASK \ 410 } 411 412 #define regulator_desc_s2mps13_buck(num, min, step, min_sel) { \ 413 .name = "BUCK"#num, \ 414 .id = S2MPS13_BUCK##num, \ 415 .ops = &s2mps14_reg_ops, \ 416 .type = REGULATOR_VOLTAGE, \ 417 .owner = THIS_MODULE, \ 418 .min_uV = min, \ 419 .uV_step = step, \ 420 .linear_min_sel = min_sel, \ 421 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \ 422 .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \ 423 .vsel_reg = S2MPS13_REG_B1OUT + (num - 1) * 2, \ 424 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \ 425 .enable_reg = S2MPS13_REG_B1CTRL + (num - 1) * 2, \ 426 .enable_mask = S2MPS14_ENABLE_MASK \ 427 } 428 429 #define regulator_desc_s2mps13_buck7(num, min, step, min_sel) { \ 430 .name = "BUCK"#num, \ 431 .id = S2MPS13_BUCK##num, \ 432 .ops = &s2mps14_reg_ops, \ 433 .type = REGULATOR_VOLTAGE, \ 434 .owner = THIS_MODULE, \ 435 .min_uV = min, \ 436 .uV_step = step, \ 437 .linear_min_sel = min_sel, \ 438 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \ 439 .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \ 440 .vsel_reg = S2MPS13_REG_B1OUT + (num) * 2 - 1, \ 441 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \ 442 .enable_reg = S2MPS13_REG_B1CTRL + (num - 1) * 2, \ 443 .enable_mask = S2MPS14_ENABLE_MASK \ 444 } 445 446 #define regulator_desc_s2mps13_buck8_10(num, min, step, min_sel) { \ 447 .name = "BUCK"#num, \ 448 .id = S2MPS13_BUCK##num, \ 449 .ops = &s2mps14_reg_ops, \ 450 .type = REGULATOR_VOLTAGE, \ 451 .owner = THIS_MODULE, \ 452 .min_uV = min, \ 453 .uV_step = step, \ 454 .linear_min_sel = min_sel, \ 455 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \ 456 .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \ 457 .vsel_reg = S2MPS13_REG_B1OUT + (num) * 2 - 1, \ 458 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \ 459 .enable_reg = S2MPS13_REG_B1CTRL + (num) * 2 - 1, \ 460 .enable_mask = S2MPS14_ENABLE_MASK \ 461 } 462 463 static const struct regulator_desc s2mps13_regulators[] = { 464 regulator_desc_s2mps13_ldo(1, MIN_800_MV, STEP_12_5_MV, 0x00), 465 regulator_desc_s2mps13_ldo(2, MIN_1400_MV, STEP_50_MV, 0x0C), 466 regulator_desc_s2mps13_ldo(3, MIN_1000_MV, STEP_25_MV, 0x08), 467 regulator_desc_s2mps13_ldo(4, MIN_800_MV, STEP_12_5_MV, 0x00), 468 regulator_desc_s2mps13_ldo(5, MIN_800_MV, STEP_12_5_MV, 0x00), 469 regulator_desc_s2mps13_ldo(6, MIN_800_MV, STEP_12_5_MV, 0x00), 470 regulator_desc_s2mps13_ldo(7, MIN_1000_MV, STEP_25_MV, 0x08), 471 regulator_desc_s2mps13_ldo(8, MIN_1000_MV, STEP_25_MV, 0x08), 472 regulator_desc_s2mps13_ldo(9, MIN_1000_MV, STEP_25_MV, 0x08), 473 regulator_desc_s2mps13_ldo(10, MIN_1400_MV, STEP_50_MV, 0x0C), 474 regulator_desc_s2mps13_ldo(11, MIN_800_MV, STEP_25_MV, 0x10), 475 regulator_desc_s2mps13_ldo(12, MIN_800_MV, STEP_25_MV, 0x10), 476 regulator_desc_s2mps13_ldo(13, MIN_800_MV, STEP_25_MV, 0x10), 477 regulator_desc_s2mps13_ldo(14, MIN_800_MV, STEP_12_5_MV, 0x00), 478 regulator_desc_s2mps13_ldo(15, MIN_800_MV, STEP_12_5_MV, 0x00), 479 regulator_desc_s2mps13_ldo(16, MIN_1400_MV, STEP_50_MV, 0x0C), 480 regulator_desc_s2mps13_ldo(17, MIN_1400_MV, STEP_50_MV, 0x0C), 481 regulator_desc_s2mps13_ldo(18, MIN_1000_MV, STEP_25_MV, 0x08), 482 regulator_desc_s2mps13_ldo(19, MIN_1000_MV, STEP_25_MV, 0x08), 483 regulator_desc_s2mps13_ldo(20, MIN_1400_MV, STEP_50_MV, 0x0C), 484 regulator_desc_s2mps13_ldo(21, MIN_1000_MV, STEP_25_MV, 0x08), 485 regulator_desc_s2mps13_ldo(22, MIN_1000_MV, STEP_25_MV, 0x08), 486 regulator_desc_s2mps13_ldo(23, MIN_800_MV, STEP_12_5_MV, 0x00), 487 regulator_desc_s2mps13_ldo(24, MIN_800_MV, STEP_12_5_MV, 0x00), 488 regulator_desc_s2mps13_ldo(25, MIN_1400_MV, STEP_50_MV, 0x0C), 489 regulator_desc_s2mps13_ldo(26, MIN_1400_MV, STEP_50_MV, 0x0C), 490 regulator_desc_s2mps13_ldo(27, MIN_1400_MV, STEP_50_MV, 0x0C), 491 regulator_desc_s2mps13_ldo(28, MIN_1000_MV, STEP_25_MV, 0x08), 492 regulator_desc_s2mps13_ldo(29, MIN_1400_MV, STEP_50_MV, 0x0C), 493 regulator_desc_s2mps13_ldo(30, MIN_1400_MV, STEP_50_MV, 0x0C), 494 regulator_desc_s2mps13_ldo(31, MIN_1000_MV, STEP_25_MV, 0x08), 495 regulator_desc_s2mps13_ldo(32, MIN_1000_MV, STEP_25_MV, 0x08), 496 regulator_desc_s2mps13_ldo(33, MIN_1400_MV, STEP_50_MV, 0x0C), 497 regulator_desc_s2mps13_ldo(34, MIN_1000_MV, STEP_25_MV, 0x08), 498 regulator_desc_s2mps13_ldo(35, MIN_1400_MV, STEP_50_MV, 0x0C), 499 regulator_desc_s2mps13_ldo(36, MIN_800_MV, STEP_12_5_MV, 0x00), 500 regulator_desc_s2mps13_ldo(37, MIN_1000_MV, STEP_25_MV, 0x08), 501 regulator_desc_s2mps13_ldo(38, MIN_1400_MV, STEP_50_MV, 0x0C), 502 regulator_desc_s2mps13_ldo(39, MIN_1000_MV, STEP_25_MV, 0x08), 503 regulator_desc_s2mps13_ldo(40, MIN_1400_MV, STEP_50_MV, 0x0C), 504 regulator_desc_s2mps13_buck(1, MIN_500_MV, STEP_6_25_MV, 0x10), 505 regulator_desc_s2mps13_buck(2, MIN_500_MV, STEP_6_25_MV, 0x10), 506 regulator_desc_s2mps13_buck(3, MIN_500_MV, STEP_6_25_MV, 0x10), 507 regulator_desc_s2mps13_buck(4, MIN_500_MV, STEP_6_25_MV, 0x10), 508 regulator_desc_s2mps13_buck(5, MIN_500_MV, STEP_6_25_MV, 0x10), 509 regulator_desc_s2mps13_buck(6, MIN_500_MV, STEP_6_25_MV, 0x10), 510 regulator_desc_s2mps13_buck7(7, MIN_500_MV, STEP_6_25_MV, 0x10), 511 regulator_desc_s2mps13_buck8_10(8, MIN_1000_MV, STEP_12_5_MV, 0x20), 512 regulator_desc_s2mps13_buck8_10(9, MIN_1000_MV, STEP_12_5_MV, 0x20), 513 regulator_desc_s2mps13_buck8_10(10, MIN_500_MV, STEP_6_25_MV, 0x10), 514 }; 515 516 static int s2mps14_regulator_enable(struct regulator_dev *rdev) 517 { 518 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev); 519 unsigned int val; 520 521 switch (s2mps11->dev_type) { 522 case S2MPS13X: 523 case S2MPS14X: 524 if (test_bit(rdev_get_id(rdev), s2mps11->suspend_state)) 525 val = S2MPS14_ENABLE_SUSPEND; 526 else if (s2mps11->ext_control_gpiod[rdev_get_id(rdev)]) 527 val = S2MPS14_ENABLE_EXT_CONTROL; 528 else 529 val = rdev->desc->enable_mask; 530 break; 531 case S2MPU02: 532 if (test_bit(rdev_get_id(rdev), s2mps11->suspend_state)) 533 val = S2MPU02_ENABLE_SUSPEND; 534 else 535 val = rdev->desc->enable_mask; 536 break; 537 default: 538 return -EINVAL; 539 } 540 541 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 542 rdev->desc->enable_mask, val); 543 } 544 545 static int s2mps14_regulator_set_suspend_disable(struct regulator_dev *rdev) 546 { 547 int ret; 548 unsigned int val, state; 549 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev); 550 int rdev_id = rdev_get_id(rdev); 551 552 /* Below LDO should be always on or does not support suspend mode. */ 553 switch (s2mps11->dev_type) { 554 case S2MPS13X: 555 case S2MPS14X: 556 switch (rdev_id) { 557 case S2MPS14_LDO3: 558 return 0; 559 default: 560 state = S2MPS14_ENABLE_SUSPEND; 561 break; 562 } 563 break; 564 case S2MPU02: 565 switch (rdev_id) { 566 case S2MPU02_LDO13: 567 case S2MPU02_LDO14: 568 case S2MPU02_LDO15: 569 case S2MPU02_LDO17: 570 case S2MPU02_BUCK7: 571 state = S2MPU02_DISABLE_SUSPEND; 572 break; 573 default: 574 state = S2MPU02_ENABLE_SUSPEND; 575 break; 576 } 577 break; 578 default: 579 return -EINVAL; 580 } 581 582 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val); 583 if (ret < 0) 584 return ret; 585 586 set_bit(rdev_get_id(rdev), s2mps11->suspend_state); 587 /* 588 * Don't enable suspend mode if regulator is already disabled because 589 * this would effectively for a short time turn on the regulator after 590 * resuming. 591 * However we still want to toggle the suspend_state bit for regulator 592 * in case if it got enabled before suspending the system. 593 */ 594 if (!(val & rdev->desc->enable_mask)) 595 return 0; 596 597 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 598 rdev->desc->enable_mask, state); 599 } 600 601 static const struct regulator_ops s2mps14_reg_ops = { 602 .list_voltage = regulator_list_voltage_linear, 603 .map_voltage = regulator_map_voltage_linear, 604 .is_enabled = regulator_is_enabled_regmap, 605 .enable = s2mps14_regulator_enable, 606 .disable = regulator_disable_regmap, 607 .get_voltage_sel = regulator_get_voltage_sel_regmap, 608 .set_voltage_sel = regulator_set_voltage_sel_regmap, 609 .set_voltage_time_sel = regulator_set_voltage_time_sel, 610 .set_suspend_disable = s2mps14_regulator_set_suspend_disable, 611 }; 612 613 #define regulator_desc_s2mps14_ldo(num, min, step) { \ 614 .name = "LDO"#num, \ 615 .id = S2MPS14_LDO##num, \ 616 .ops = &s2mps14_reg_ops, \ 617 .type = REGULATOR_VOLTAGE, \ 618 .owner = THIS_MODULE, \ 619 .min_uV = min, \ 620 .uV_step = step, \ 621 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \ 622 .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \ 623 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \ 624 .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \ 625 .enable_mask = S2MPS14_ENABLE_MASK \ 626 } 627 628 #define regulator_desc_s2mps14_buck(num, min, step, min_sel) { \ 629 .name = "BUCK"#num, \ 630 .id = S2MPS14_BUCK##num, \ 631 .ops = &s2mps14_reg_ops, \ 632 .type = REGULATOR_VOLTAGE, \ 633 .owner = THIS_MODULE, \ 634 .min_uV = min, \ 635 .uV_step = step, \ 636 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \ 637 .linear_min_sel = min_sel, \ 638 .ramp_delay = S2MPS14_BUCK_RAMP_DELAY, \ 639 .vsel_reg = S2MPS14_REG_B1CTRL2 + (num - 1) * 2, \ 640 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \ 641 .enable_reg = S2MPS14_REG_B1CTRL1 + (num - 1) * 2, \ 642 .enable_mask = S2MPS14_ENABLE_MASK \ 643 } 644 645 static const struct regulator_desc s2mps14_regulators[] = { 646 regulator_desc_s2mps14_ldo(1, MIN_800_MV, STEP_12_5_MV), 647 regulator_desc_s2mps14_ldo(2, MIN_800_MV, STEP_12_5_MV), 648 regulator_desc_s2mps14_ldo(3, MIN_800_MV, STEP_25_MV), 649 regulator_desc_s2mps14_ldo(4, MIN_800_MV, STEP_25_MV), 650 regulator_desc_s2mps14_ldo(5, MIN_800_MV, STEP_12_5_MV), 651 regulator_desc_s2mps14_ldo(6, MIN_800_MV, STEP_12_5_MV), 652 regulator_desc_s2mps14_ldo(7, MIN_800_MV, STEP_25_MV), 653 regulator_desc_s2mps14_ldo(8, MIN_1800_MV, STEP_25_MV), 654 regulator_desc_s2mps14_ldo(9, MIN_800_MV, STEP_12_5_MV), 655 regulator_desc_s2mps14_ldo(10, MIN_800_MV, STEP_12_5_MV), 656 regulator_desc_s2mps14_ldo(11, MIN_800_MV, STEP_25_MV), 657 regulator_desc_s2mps14_ldo(12, MIN_1800_MV, STEP_25_MV), 658 regulator_desc_s2mps14_ldo(13, MIN_1800_MV, STEP_25_MV), 659 regulator_desc_s2mps14_ldo(14, MIN_1800_MV, STEP_25_MV), 660 regulator_desc_s2mps14_ldo(15, MIN_1800_MV, STEP_25_MV), 661 regulator_desc_s2mps14_ldo(16, MIN_1800_MV, STEP_25_MV), 662 regulator_desc_s2mps14_ldo(17, MIN_1800_MV, STEP_25_MV), 663 regulator_desc_s2mps14_ldo(18, MIN_1800_MV, STEP_25_MV), 664 regulator_desc_s2mps14_ldo(19, MIN_800_MV, STEP_25_MV), 665 regulator_desc_s2mps14_ldo(20, MIN_800_MV, STEP_25_MV), 666 regulator_desc_s2mps14_ldo(21, MIN_800_MV, STEP_25_MV), 667 regulator_desc_s2mps14_ldo(22, MIN_800_MV, STEP_12_5_MV), 668 regulator_desc_s2mps14_ldo(23, MIN_800_MV, STEP_25_MV), 669 regulator_desc_s2mps14_ldo(24, MIN_1800_MV, STEP_25_MV), 670 regulator_desc_s2mps14_ldo(25, MIN_1800_MV, STEP_25_MV), 671 regulator_desc_s2mps14_buck(1, MIN_600_MV, STEP_6_25_MV, 672 S2MPS14_BUCK1235_START_SEL), 673 regulator_desc_s2mps14_buck(2, MIN_600_MV, STEP_6_25_MV, 674 S2MPS14_BUCK1235_START_SEL), 675 regulator_desc_s2mps14_buck(3, MIN_600_MV, STEP_6_25_MV, 676 S2MPS14_BUCK1235_START_SEL), 677 regulator_desc_s2mps14_buck(4, MIN_1400_MV, STEP_12_5_MV, 678 S2MPS14_BUCK4_START_SEL), 679 regulator_desc_s2mps14_buck(5, MIN_600_MV, STEP_6_25_MV, 680 S2MPS14_BUCK1235_START_SEL), 681 }; 682 683 static const struct regulator_ops s2mps15_reg_ldo_ops = { 684 .list_voltage = regulator_list_voltage_linear_range, 685 .map_voltage = regulator_map_voltage_linear_range, 686 .is_enabled = regulator_is_enabled_regmap, 687 .enable = regulator_enable_regmap, 688 .disable = regulator_disable_regmap, 689 .get_voltage_sel = regulator_get_voltage_sel_regmap, 690 .set_voltage_sel = regulator_set_voltage_sel_regmap, 691 }; 692 693 static const struct regulator_ops s2mps15_reg_buck_ops = { 694 .list_voltage = regulator_list_voltage_linear_range, 695 .map_voltage = regulator_map_voltage_linear_range, 696 .is_enabled = regulator_is_enabled_regmap, 697 .enable = regulator_enable_regmap, 698 .disable = regulator_disable_regmap, 699 .get_voltage_sel = regulator_get_voltage_sel_regmap, 700 .set_voltage_sel = regulator_set_voltage_sel_regmap, 701 .set_voltage_time_sel = regulator_set_voltage_time_sel, 702 }; 703 704 #define regulator_desc_s2mps15_ldo(num, range) { \ 705 .name = "LDO"#num, \ 706 .id = S2MPS15_LDO##num, \ 707 .ops = &s2mps15_reg_ldo_ops, \ 708 .type = REGULATOR_VOLTAGE, \ 709 .owner = THIS_MODULE, \ 710 .linear_ranges = range, \ 711 .n_linear_ranges = ARRAY_SIZE(range), \ 712 .n_voltages = S2MPS15_LDO_N_VOLTAGES, \ 713 .vsel_reg = S2MPS15_REG_L1CTRL + num - 1, \ 714 .vsel_mask = S2MPS15_LDO_VSEL_MASK, \ 715 .enable_reg = S2MPS15_REG_L1CTRL + num - 1, \ 716 .enable_mask = S2MPS15_ENABLE_MASK \ 717 } 718 719 #define regulator_desc_s2mps15_buck(num, range) { \ 720 .name = "BUCK"#num, \ 721 .id = S2MPS15_BUCK##num, \ 722 .ops = &s2mps15_reg_buck_ops, \ 723 .type = REGULATOR_VOLTAGE, \ 724 .owner = THIS_MODULE, \ 725 .linear_ranges = range, \ 726 .n_linear_ranges = ARRAY_SIZE(range), \ 727 .ramp_delay = 12500, \ 728 .n_voltages = S2MPS15_BUCK_N_VOLTAGES, \ 729 .vsel_reg = S2MPS15_REG_B1CTRL2 + ((num - 1) * 2), \ 730 .vsel_mask = S2MPS15_BUCK_VSEL_MASK, \ 731 .enable_reg = S2MPS15_REG_B1CTRL1 + ((num - 1) * 2), \ 732 .enable_mask = S2MPS15_ENABLE_MASK \ 733 } 734 735 /* voltage range for s2mps15 LDO 3, 5, 15, 16, 18, 20, 23 and 27 */ 736 static const struct regulator_linear_range s2mps15_ldo_voltage_ranges1[] = { 737 REGULATOR_LINEAR_RANGE(1000000, 0xc, 0x38, 25000), 738 }; 739 740 /* voltage range for s2mps15 LDO 2, 6, 14, 17, 19, 21, 24 and 25 */ 741 static const struct regulator_linear_range s2mps15_ldo_voltage_ranges2[] = { 742 REGULATOR_LINEAR_RANGE(1800000, 0x0, 0x3f, 25000), 743 }; 744 745 /* voltage range for s2mps15 LDO 4, 11, 12, 13, 22 and 26 */ 746 static const struct regulator_linear_range s2mps15_ldo_voltage_ranges3[] = { 747 REGULATOR_LINEAR_RANGE(700000, 0x0, 0x34, 12500), 748 }; 749 750 /* voltage range for s2mps15 LDO 7, 8, 9 and 10 */ 751 static const struct regulator_linear_range s2mps15_ldo_voltage_ranges4[] = { 752 REGULATOR_LINEAR_RANGE(700000, 0x10, 0x20, 25000), 753 }; 754 755 /* voltage range for s2mps15 LDO 1 */ 756 static const struct regulator_linear_range s2mps15_ldo_voltage_ranges5[] = { 757 REGULATOR_LINEAR_RANGE(500000, 0x0, 0x20, 12500), 758 }; 759 760 /* voltage range for s2mps15 BUCK 1, 2, 3, 4, 5, 6 and 7 */ 761 static const struct regulator_linear_range s2mps15_buck_voltage_ranges1[] = { 762 REGULATOR_LINEAR_RANGE(500000, 0x20, 0xc0, 6250), 763 }; 764 765 /* voltage range for s2mps15 BUCK 8, 9 and 10 */ 766 static const struct regulator_linear_range s2mps15_buck_voltage_ranges2[] = { 767 REGULATOR_LINEAR_RANGE(1000000, 0x20, 0x78, 12500), 768 }; 769 770 static const struct regulator_desc s2mps15_regulators[] = { 771 regulator_desc_s2mps15_ldo(1, s2mps15_ldo_voltage_ranges5), 772 regulator_desc_s2mps15_ldo(2, s2mps15_ldo_voltage_ranges2), 773 regulator_desc_s2mps15_ldo(3, s2mps15_ldo_voltage_ranges1), 774 regulator_desc_s2mps15_ldo(4, s2mps15_ldo_voltage_ranges3), 775 regulator_desc_s2mps15_ldo(5, s2mps15_ldo_voltage_ranges1), 776 regulator_desc_s2mps15_ldo(6, s2mps15_ldo_voltage_ranges2), 777 regulator_desc_s2mps15_ldo(7, s2mps15_ldo_voltage_ranges4), 778 regulator_desc_s2mps15_ldo(8, s2mps15_ldo_voltage_ranges4), 779 regulator_desc_s2mps15_ldo(9, s2mps15_ldo_voltage_ranges4), 780 regulator_desc_s2mps15_ldo(10, s2mps15_ldo_voltage_ranges4), 781 regulator_desc_s2mps15_ldo(11, s2mps15_ldo_voltage_ranges3), 782 regulator_desc_s2mps15_ldo(12, s2mps15_ldo_voltage_ranges3), 783 regulator_desc_s2mps15_ldo(13, s2mps15_ldo_voltage_ranges3), 784 regulator_desc_s2mps15_ldo(14, s2mps15_ldo_voltage_ranges2), 785 regulator_desc_s2mps15_ldo(15, s2mps15_ldo_voltage_ranges1), 786 regulator_desc_s2mps15_ldo(16, s2mps15_ldo_voltage_ranges1), 787 regulator_desc_s2mps15_ldo(17, s2mps15_ldo_voltage_ranges2), 788 regulator_desc_s2mps15_ldo(18, s2mps15_ldo_voltage_ranges1), 789 regulator_desc_s2mps15_ldo(19, s2mps15_ldo_voltage_ranges2), 790 regulator_desc_s2mps15_ldo(20, s2mps15_ldo_voltage_ranges1), 791 regulator_desc_s2mps15_ldo(21, s2mps15_ldo_voltage_ranges2), 792 regulator_desc_s2mps15_ldo(22, s2mps15_ldo_voltage_ranges3), 793 regulator_desc_s2mps15_ldo(23, s2mps15_ldo_voltage_ranges1), 794 regulator_desc_s2mps15_ldo(24, s2mps15_ldo_voltage_ranges2), 795 regulator_desc_s2mps15_ldo(25, s2mps15_ldo_voltage_ranges2), 796 regulator_desc_s2mps15_ldo(26, s2mps15_ldo_voltage_ranges3), 797 regulator_desc_s2mps15_ldo(27, s2mps15_ldo_voltage_ranges1), 798 regulator_desc_s2mps15_buck(1, s2mps15_buck_voltage_ranges1), 799 regulator_desc_s2mps15_buck(2, s2mps15_buck_voltage_ranges1), 800 regulator_desc_s2mps15_buck(3, s2mps15_buck_voltage_ranges1), 801 regulator_desc_s2mps15_buck(4, s2mps15_buck_voltage_ranges1), 802 regulator_desc_s2mps15_buck(5, s2mps15_buck_voltage_ranges1), 803 regulator_desc_s2mps15_buck(6, s2mps15_buck_voltage_ranges1), 804 regulator_desc_s2mps15_buck(7, s2mps15_buck_voltage_ranges1), 805 regulator_desc_s2mps15_buck(8, s2mps15_buck_voltage_ranges2), 806 regulator_desc_s2mps15_buck(9, s2mps15_buck_voltage_ranges2), 807 regulator_desc_s2mps15_buck(10, s2mps15_buck_voltage_ranges2), 808 }; 809 810 static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11, 811 struct regulator_dev *rdev) 812 { 813 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 814 rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL); 815 } 816 817 static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev, 818 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11) 819 { 820 struct gpio_desc **gpio = s2mps11->ext_control_gpiod; 821 unsigned int i; 822 unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11, 823 S2MPS14_LDO12 }; 824 825 for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) { 826 unsigned int reg = valid_regulators[i]; 827 828 if (!rdata[reg].init_data || !rdata[reg].of_node) 829 continue; 830 831 gpio[reg] = devm_gpiod_get_from_of_node(&pdev->dev, 832 rdata[reg].of_node, 833 "samsung,ext-control-gpios", 834 0, 835 GPIOD_OUT_HIGH, 836 "s2mps11-LDO"); 837 if (IS_ERR(gpio[reg])) { 838 dev_err(&pdev->dev, "Failed to get control GPIO for %d/%s\n", 839 reg, rdata[reg].name); 840 continue; 841 } 842 if (gpio[reg]) 843 dev_dbg(&pdev->dev, "Using GPIO for ext-control over %d/%s\n", 844 reg, rdata[reg].name); 845 } 846 } 847 848 static int s2mps11_pmic_dt_parse(struct platform_device *pdev, 849 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11, 850 unsigned int rdev_num) 851 { 852 struct device_node *reg_np; 853 854 reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators"); 855 if (!reg_np) { 856 dev_err(&pdev->dev, "could not find regulators sub-node\n"); 857 return -EINVAL; 858 } 859 860 of_regulator_match(&pdev->dev, reg_np, rdata, rdev_num); 861 if (s2mps11->dev_type == S2MPS14X) 862 s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11); 863 864 of_node_put(reg_np); 865 866 return 0; 867 } 868 869 static int s2mpu02_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay) 870 { 871 unsigned int ramp_val, ramp_shift, ramp_reg; 872 873 switch (rdev_get_id(rdev)) { 874 case S2MPU02_BUCK1: 875 ramp_shift = S2MPU02_BUCK1_RAMP_SHIFT; 876 break; 877 case S2MPU02_BUCK2: 878 ramp_shift = S2MPU02_BUCK2_RAMP_SHIFT; 879 break; 880 case S2MPU02_BUCK3: 881 ramp_shift = S2MPU02_BUCK3_RAMP_SHIFT; 882 break; 883 case S2MPU02_BUCK4: 884 ramp_shift = S2MPU02_BUCK4_RAMP_SHIFT; 885 break; 886 default: 887 return 0; 888 } 889 ramp_reg = S2MPU02_REG_RAMP1; 890 ramp_val = get_ramp_delay(ramp_delay); 891 892 return regmap_update_bits(rdev->regmap, ramp_reg, 893 S2MPU02_BUCK1234_RAMP_MASK << ramp_shift, 894 ramp_val << ramp_shift); 895 } 896 897 static const struct regulator_ops s2mpu02_ldo_ops = { 898 .list_voltage = regulator_list_voltage_linear, 899 .map_voltage = regulator_map_voltage_linear, 900 .is_enabled = regulator_is_enabled_regmap, 901 .enable = s2mps14_regulator_enable, 902 .disable = regulator_disable_regmap, 903 .get_voltage_sel = regulator_get_voltage_sel_regmap, 904 .set_voltage_sel = regulator_set_voltage_sel_regmap, 905 .set_voltage_time_sel = regulator_set_voltage_time_sel, 906 .set_suspend_disable = s2mps14_regulator_set_suspend_disable, 907 }; 908 909 static const struct regulator_ops s2mpu02_buck_ops = { 910 .list_voltage = regulator_list_voltage_linear, 911 .map_voltage = regulator_map_voltage_linear, 912 .is_enabled = regulator_is_enabled_regmap, 913 .enable = s2mps14_regulator_enable, 914 .disable = regulator_disable_regmap, 915 .get_voltage_sel = regulator_get_voltage_sel_regmap, 916 .set_voltage_sel = regulator_set_voltage_sel_regmap, 917 .set_voltage_time_sel = regulator_set_voltage_time_sel, 918 .set_suspend_disable = s2mps14_regulator_set_suspend_disable, 919 .set_ramp_delay = s2mpu02_set_ramp_delay, 920 }; 921 922 #define regulator_desc_s2mpu02_ldo1(num) { \ 923 .name = "LDO"#num, \ 924 .id = S2MPU02_LDO##num, \ 925 .ops = &s2mpu02_ldo_ops, \ 926 .type = REGULATOR_VOLTAGE, \ 927 .owner = THIS_MODULE, \ 928 .min_uV = S2MPU02_LDO_MIN_900MV, \ 929 .uV_step = S2MPU02_LDO_STEP_12_5MV, \ 930 .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \ 931 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \ 932 .vsel_reg = S2MPU02_REG_L1CTRL, \ 933 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \ 934 .enable_reg = S2MPU02_REG_L1CTRL, \ 935 .enable_mask = S2MPU02_ENABLE_MASK \ 936 } 937 #define regulator_desc_s2mpu02_ldo2(num) { \ 938 .name = "LDO"#num, \ 939 .id = S2MPU02_LDO##num, \ 940 .ops = &s2mpu02_ldo_ops, \ 941 .type = REGULATOR_VOLTAGE, \ 942 .owner = THIS_MODULE, \ 943 .min_uV = S2MPU02_LDO_MIN_1050MV, \ 944 .uV_step = S2MPU02_LDO_STEP_25MV, \ 945 .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \ 946 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \ 947 .vsel_reg = S2MPU02_REG_L2CTRL1, \ 948 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \ 949 .enable_reg = S2MPU02_REG_L2CTRL1, \ 950 .enable_mask = S2MPU02_ENABLE_MASK \ 951 } 952 #define regulator_desc_s2mpu02_ldo3(num) { \ 953 .name = "LDO"#num, \ 954 .id = S2MPU02_LDO##num, \ 955 .ops = &s2mpu02_ldo_ops, \ 956 .type = REGULATOR_VOLTAGE, \ 957 .owner = THIS_MODULE, \ 958 .min_uV = S2MPU02_LDO_MIN_900MV, \ 959 .uV_step = S2MPU02_LDO_STEP_12_5MV, \ 960 .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \ 961 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \ 962 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \ 963 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \ 964 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \ 965 .enable_mask = S2MPU02_ENABLE_MASK \ 966 } 967 #define regulator_desc_s2mpu02_ldo4(num) { \ 968 .name = "LDO"#num, \ 969 .id = S2MPU02_LDO##num, \ 970 .ops = &s2mpu02_ldo_ops, \ 971 .type = REGULATOR_VOLTAGE, \ 972 .owner = THIS_MODULE, \ 973 .min_uV = S2MPU02_LDO_MIN_1050MV, \ 974 .uV_step = S2MPU02_LDO_STEP_25MV, \ 975 .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \ 976 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \ 977 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \ 978 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \ 979 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \ 980 .enable_mask = S2MPU02_ENABLE_MASK \ 981 } 982 #define regulator_desc_s2mpu02_ldo5(num) { \ 983 .name = "LDO"#num, \ 984 .id = S2MPU02_LDO##num, \ 985 .ops = &s2mpu02_ldo_ops, \ 986 .type = REGULATOR_VOLTAGE, \ 987 .owner = THIS_MODULE, \ 988 .min_uV = S2MPU02_LDO_MIN_1600MV, \ 989 .uV_step = S2MPU02_LDO_STEP_50MV, \ 990 .linear_min_sel = S2MPU02_LDO_GROUP3_START_SEL, \ 991 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \ 992 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \ 993 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \ 994 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \ 995 .enable_mask = S2MPU02_ENABLE_MASK \ 996 } 997 998 #define regulator_desc_s2mpu02_buck1234(num) { \ 999 .name = "BUCK"#num, \ 1000 .id = S2MPU02_BUCK##num, \ 1001 .ops = &s2mpu02_buck_ops, \ 1002 .type = REGULATOR_VOLTAGE, \ 1003 .owner = THIS_MODULE, \ 1004 .min_uV = S2MPU02_BUCK1234_MIN_600MV, \ 1005 .uV_step = S2MPU02_BUCK1234_STEP_6_25MV, \ 1006 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \ 1007 .linear_min_sel = S2MPU02_BUCK1234_START_SEL, \ 1008 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \ 1009 .vsel_reg = S2MPU02_REG_B1CTRL2 + (num - 1) * 2, \ 1010 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \ 1011 .enable_reg = S2MPU02_REG_B1CTRL1 + (num - 1) * 2, \ 1012 .enable_mask = S2MPU02_ENABLE_MASK \ 1013 } 1014 #define regulator_desc_s2mpu02_buck5(num) { \ 1015 .name = "BUCK"#num, \ 1016 .id = S2MPU02_BUCK##num, \ 1017 .ops = &s2mpu02_ldo_ops, \ 1018 .type = REGULATOR_VOLTAGE, \ 1019 .owner = THIS_MODULE, \ 1020 .min_uV = S2MPU02_BUCK5_MIN_1081_25MV, \ 1021 .uV_step = S2MPU02_BUCK5_STEP_6_25MV, \ 1022 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \ 1023 .linear_min_sel = S2MPU02_BUCK5_START_SEL, \ 1024 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \ 1025 .vsel_reg = S2MPU02_REG_B5CTRL2, \ 1026 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \ 1027 .enable_reg = S2MPU02_REG_B5CTRL1, \ 1028 .enable_mask = S2MPU02_ENABLE_MASK \ 1029 } 1030 #define regulator_desc_s2mpu02_buck6(num) { \ 1031 .name = "BUCK"#num, \ 1032 .id = S2MPU02_BUCK##num, \ 1033 .ops = &s2mpu02_ldo_ops, \ 1034 .type = REGULATOR_VOLTAGE, \ 1035 .owner = THIS_MODULE, \ 1036 .min_uV = S2MPU02_BUCK6_MIN_1700MV, \ 1037 .uV_step = S2MPU02_BUCK6_STEP_2_50MV, \ 1038 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \ 1039 .linear_min_sel = S2MPU02_BUCK6_START_SEL, \ 1040 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \ 1041 .vsel_reg = S2MPU02_REG_B6CTRL2, \ 1042 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \ 1043 .enable_reg = S2MPU02_REG_B6CTRL1, \ 1044 .enable_mask = S2MPU02_ENABLE_MASK \ 1045 } 1046 #define regulator_desc_s2mpu02_buck7(num) { \ 1047 .name = "BUCK"#num, \ 1048 .id = S2MPU02_BUCK##num, \ 1049 .ops = &s2mpu02_ldo_ops, \ 1050 .type = REGULATOR_VOLTAGE, \ 1051 .owner = THIS_MODULE, \ 1052 .min_uV = S2MPU02_BUCK7_MIN_900MV, \ 1053 .uV_step = S2MPU02_BUCK7_STEP_6_25MV, \ 1054 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \ 1055 .linear_min_sel = S2MPU02_BUCK7_START_SEL, \ 1056 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \ 1057 .vsel_reg = S2MPU02_REG_B7CTRL2, \ 1058 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \ 1059 .enable_reg = S2MPU02_REG_B7CTRL1, \ 1060 .enable_mask = S2MPU02_ENABLE_MASK \ 1061 } 1062 1063 static const struct regulator_desc s2mpu02_regulators[] = { 1064 regulator_desc_s2mpu02_ldo1(1), 1065 regulator_desc_s2mpu02_ldo2(2), 1066 regulator_desc_s2mpu02_ldo4(3), 1067 regulator_desc_s2mpu02_ldo5(4), 1068 regulator_desc_s2mpu02_ldo4(5), 1069 regulator_desc_s2mpu02_ldo3(6), 1070 regulator_desc_s2mpu02_ldo3(7), 1071 regulator_desc_s2mpu02_ldo4(8), 1072 regulator_desc_s2mpu02_ldo5(9), 1073 regulator_desc_s2mpu02_ldo3(10), 1074 regulator_desc_s2mpu02_ldo4(11), 1075 regulator_desc_s2mpu02_ldo5(12), 1076 regulator_desc_s2mpu02_ldo5(13), 1077 regulator_desc_s2mpu02_ldo5(14), 1078 regulator_desc_s2mpu02_ldo5(15), 1079 regulator_desc_s2mpu02_ldo5(16), 1080 regulator_desc_s2mpu02_ldo4(17), 1081 regulator_desc_s2mpu02_ldo5(18), 1082 regulator_desc_s2mpu02_ldo3(19), 1083 regulator_desc_s2mpu02_ldo4(20), 1084 regulator_desc_s2mpu02_ldo5(21), 1085 regulator_desc_s2mpu02_ldo5(22), 1086 regulator_desc_s2mpu02_ldo5(23), 1087 regulator_desc_s2mpu02_ldo4(24), 1088 regulator_desc_s2mpu02_ldo5(25), 1089 regulator_desc_s2mpu02_ldo4(26), 1090 regulator_desc_s2mpu02_ldo5(27), 1091 regulator_desc_s2mpu02_ldo5(28), 1092 regulator_desc_s2mpu02_buck1234(1), 1093 regulator_desc_s2mpu02_buck1234(2), 1094 regulator_desc_s2mpu02_buck1234(3), 1095 regulator_desc_s2mpu02_buck1234(4), 1096 regulator_desc_s2mpu02_buck5(5), 1097 regulator_desc_s2mpu02_buck6(6), 1098 regulator_desc_s2mpu02_buck7(7), 1099 }; 1100 1101 static int s2mps11_pmic_probe(struct platform_device *pdev) 1102 { 1103 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent); 1104 struct sec_platform_data *pdata = NULL; 1105 struct of_regulator_match *rdata = NULL; 1106 struct regulator_config config = { }; 1107 struct s2mps11_info *s2mps11; 1108 unsigned int rdev_num = 0; 1109 int i, ret = 0; 1110 const struct regulator_desc *regulators; 1111 1112 s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info), 1113 GFP_KERNEL); 1114 if (!s2mps11) 1115 return -ENOMEM; 1116 1117 s2mps11->dev_type = platform_get_device_id(pdev)->driver_data; 1118 switch (s2mps11->dev_type) { 1119 case S2MPS11X: 1120 rdev_num = ARRAY_SIZE(s2mps11_regulators); 1121 regulators = s2mps11_regulators; 1122 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps11_regulators)); 1123 break; 1124 case S2MPS13X: 1125 rdev_num = ARRAY_SIZE(s2mps13_regulators); 1126 regulators = s2mps13_regulators; 1127 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps13_regulators)); 1128 break; 1129 case S2MPS14X: 1130 rdev_num = ARRAY_SIZE(s2mps14_regulators); 1131 regulators = s2mps14_regulators; 1132 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps14_regulators)); 1133 break; 1134 case S2MPS15X: 1135 rdev_num = ARRAY_SIZE(s2mps15_regulators); 1136 regulators = s2mps15_regulators; 1137 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps15_regulators)); 1138 break; 1139 case S2MPU02: 1140 rdev_num = ARRAY_SIZE(s2mpu02_regulators); 1141 regulators = s2mpu02_regulators; 1142 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mpu02_regulators)); 1143 break; 1144 default: 1145 dev_err(&pdev->dev, "Invalid device type: %u\n", 1146 s2mps11->dev_type); 1147 return -EINVAL; 1148 } 1149 1150 s2mps11->ext_control_gpiod = devm_kmalloc(&pdev->dev, 1151 sizeof(*s2mps11->ext_control_gpiod) * rdev_num, 1152 GFP_KERNEL); 1153 if (!s2mps11->ext_control_gpiod) 1154 return -ENOMEM; 1155 1156 if (!iodev->dev->of_node) { 1157 if (iodev->pdata) { 1158 pdata = iodev->pdata; 1159 goto common_reg; 1160 } else { 1161 dev_err(pdev->dev.parent, 1162 "Platform data or DT node not supplied\n"); 1163 return -ENODEV; 1164 } 1165 } 1166 1167 rdata = kzalloc(sizeof(*rdata) * rdev_num, GFP_KERNEL); 1168 if (!rdata) 1169 return -ENOMEM; 1170 1171 for (i = 0; i < rdev_num; i++) 1172 rdata[i].name = regulators[i].name; 1173 1174 ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11, rdev_num); 1175 if (ret) 1176 goto out; 1177 1178 common_reg: 1179 platform_set_drvdata(pdev, s2mps11); 1180 1181 config.dev = &pdev->dev; 1182 config.regmap = iodev->regmap_pmic; 1183 config.driver_data = s2mps11; 1184 for (i = 0; i < rdev_num; i++) { 1185 struct regulator_dev *regulator; 1186 1187 if (pdata) { 1188 config.init_data = pdata->regulators[i].initdata; 1189 config.of_node = pdata->regulators[i].reg_node; 1190 } else { 1191 config.init_data = rdata[i].init_data; 1192 config.of_node = rdata[i].of_node; 1193 } 1194 config.ena_gpiod = s2mps11->ext_control_gpiod[i]; 1195 1196 regulator = devm_regulator_register(&pdev->dev, 1197 ®ulators[i], &config); 1198 if (IS_ERR(regulator)) { 1199 ret = PTR_ERR(regulator); 1200 dev_err(&pdev->dev, "regulator init failed for %d\n", 1201 i); 1202 goto out; 1203 } 1204 1205 if (s2mps11->ext_control_gpiod[i]) { 1206 ret = s2mps14_pmic_enable_ext_control(s2mps11, 1207 regulator); 1208 if (ret < 0) { 1209 dev_err(&pdev->dev, 1210 "failed to enable GPIO control over %s: %d\n", 1211 regulator->desc->name, ret); 1212 goto out; 1213 } 1214 } 1215 } 1216 1217 out: 1218 kfree(rdata); 1219 1220 return ret; 1221 } 1222 1223 static const struct platform_device_id s2mps11_pmic_id[] = { 1224 { "s2mps11-regulator", S2MPS11X}, 1225 { "s2mps13-regulator", S2MPS13X}, 1226 { "s2mps14-regulator", S2MPS14X}, 1227 { "s2mps15-regulator", S2MPS15X}, 1228 { "s2mpu02-regulator", S2MPU02}, 1229 { }, 1230 }; 1231 MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id); 1232 1233 static struct platform_driver s2mps11_pmic_driver = { 1234 .driver = { 1235 .name = "s2mps11-pmic", 1236 }, 1237 .probe = s2mps11_pmic_probe, 1238 .id_table = s2mps11_pmic_id, 1239 }; 1240 1241 module_platform_driver(s2mps11_pmic_driver); 1242 1243 /* Module information */ 1244 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>"); 1245 MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14/S2MPS15/S2MPU02 Regulator Driver"); 1246 MODULE_LICENSE("GPL"); 1247