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