1 /* 2 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 and 6 * only version 2 as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 */ 13 14 #include <linux/module.h> 15 #include <linux/delay.h> 16 #include <linux/err.h> 17 #include <linux/kernel.h> 18 #include <linux/interrupt.h> 19 #include <linux/bitops.h> 20 #include <linux/slab.h> 21 #include <linux/of.h> 22 #include <linux/of_device.h> 23 #include <linux/platform_device.h> 24 #include <linux/ktime.h> 25 #include <linux/regulator/driver.h> 26 #include <linux/regmap.h> 27 #include <linux/list.h> 28 29 /* These types correspond to unique register layouts. */ 30 enum spmi_regulator_logical_type { 31 SPMI_REGULATOR_LOGICAL_TYPE_SMPS, 32 SPMI_REGULATOR_LOGICAL_TYPE_LDO, 33 SPMI_REGULATOR_LOGICAL_TYPE_VS, 34 SPMI_REGULATOR_LOGICAL_TYPE_BOOST, 35 SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS, 36 SPMI_REGULATOR_LOGICAL_TYPE_BOOST_BYP, 37 SPMI_REGULATOR_LOGICAL_TYPE_LN_LDO, 38 SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS, 39 SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS, 40 SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO, 41 }; 42 43 enum spmi_regulator_type { 44 SPMI_REGULATOR_TYPE_BUCK = 0x03, 45 SPMI_REGULATOR_TYPE_LDO = 0x04, 46 SPMI_REGULATOR_TYPE_VS = 0x05, 47 SPMI_REGULATOR_TYPE_BOOST = 0x1b, 48 SPMI_REGULATOR_TYPE_FTS = 0x1c, 49 SPMI_REGULATOR_TYPE_BOOST_BYP = 0x1f, 50 SPMI_REGULATOR_TYPE_ULT_LDO = 0x21, 51 SPMI_REGULATOR_TYPE_ULT_BUCK = 0x22, 52 }; 53 54 enum spmi_regulator_subtype { 55 SPMI_REGULATOR_SUBTYPE_GP_CTL = 0x08, 56 SPMI_REGULATOR_SUBTYPE_RF_CTL = 0x09, 57 SPMI_REGULATOR_SUBTYPE_N50 = 0x01, 58 SPMI_REGULATOR_SUBTYPE_N150 = 0x02, 59 SPMI_REGULATOR_SUBTYPE_N300 = 0x03, 60 SPMI_REGULATOR_SUBTYPE_N600 = 0x04, 61 SPMI_REGULATOR_SUBTYPE_N1200 = 0x05, 62 SPMI_REGULATOR_SUBTYPE_N600_ST = 0x06, 63 SPMI_REGULATOR_SUBTYPE_N1200_ST = 0x07, 64 SPMI_REGULATOR_SUBTYPE_N900_ST = 0x14, 65 SPMI_REGULATOR_SUBTYPE_N300_ST = 0x15, 66 SPMI_REGULATOR_SUBTYPE_P50 = 0x08, 67 SPMI_REGULATOR_SUBTYPE_P150 = 0x09, 68 SPMI_REGULATOR_SUBTYPE_P300 = 0x0a, 69 SPMI_REGULATOR_SUBTYPE_P600 = 0x0b, 70 SPMI_REGULATOR_SUBTYPE_P1200 = 0x0c, 71 SPMI_REGULATOR_SUBTYPE_LN = 0x10, 72 SPMI_REGULATOR_SUBTYPE_LV_P50 = 0x28, 73 SPMI_REGULATOR_SUBTYPE_LV_P150 = 0x29, 74 SPMI_REGULATOR_SUBTYPE_LV_P300 = 0x2a, 75 SPMI_REGULATOR_SUBTYPE_LV_P600 = 0x2b, 76 SPMI_REGULATOR_SUBTYPE_LV_P1200 = 0x2c, 77 SPMI_REGULATOR_SUBTYPE_LV_P450 = 0x2d, 78 SPMI_REGULATOR_SUBTYPE_LV100 = 0x01, 79 SPMI_REGULATOR_SUBTYPE_LV300 = 0x02, 80 SPMI_REGULATOR_SUBTYPE_MV300 = 0x08, 81 SPMI_REGULATOR_SUBTYPE_MV500 = 0x09, 82 SPMI_REGULATOR_SUBTYPE_HDMI = 0x10, 83 SPMI_REGULATOR_SUBTYPE_OTG = 0x11, 84 SPMI_REGULATOR_SUBTYPE_5V_BOOST = 0x01, 85 SPMI_REGULATOR_SUBTYPE_FTS_CTL = 0x08, 86 SPMI_REGULATOR_SUBTYPE_FTS2p5_CTL = 0x09, 87 SPMI_REGULATOR_SUBTYPE_BB_2A = 0x01, 88 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL1 = 0x0d, 89 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL2 = 0x0e, 90 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL3 = 0x0f, 91 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL4 = 0x10, 92 }; 93 94 enum spmi_common_regulator_registers { 95 SPMI_COMMON_REG_DIG_MAJOR_REV = 0x01, 96 SPMI_COMMON_REG_TYPE = 0x04, 97 SPMI_COMMON_REG_SUBTYPE = 0x05, 98 SPMI_COMMON_REG_VOLTAGE_RANGE = 0x40, 99 SPMI_COMMON_REG_VOLTAGE_SET = 0x41, 100 SPMI_COMMON_REG_MODE = 0x45, 101 SPMI_COMMON_REG_ENABLE = 0x46, 102 SPMI_COMMON_REG_PULL_DOWN = 0x48, 103 SPMI_COMMON_REG_SOFT_START = 0x4c, 104 SPMI_COMMON_REG_STEP_CTRL = 0x61, 105 }; 106 107 enum spmi_vs_registers { 108 SPMI_VS_REG_OCP = 0x4a, 109 SPMI_VS_REG_SOFT_START = 0x4c, 110 }; 111 112 enum spmi_boost_registers { 113 SPMI_BOOST_REG_CURRENT_LIMIT = 0x4a, 114 }; 115 116 enum spmi_boost_byp_registers { 117 SPMI_BOOST_BYP_REG_CURRENT_LIMIT = 0x4b, 118 }; 119 120 /* Used for indexing into ctrl_reg. These are offets from 0x40 */ 121 enum spmi_common_control_register_index { 122 SPMI_COMMON_IDX_VOLTAGE_RANGE = 0, 123 SPMI_COMMON_IDX_VOLTAGE_SET = 1, 124 SPMI_COMMON_IDX_MODE = 5, 125 SPMI_COMMON_IDX_ENABLE = 6, 126 }; 127 128 /* Common regulator control register layout */ 129 #define SPMI_COMMON_ENABLE_MASK 0x80 130 #define SPMI_COMMON_ENABLE 0x80 131 #define SPMI_COMMON_DISABLE 0x00 132 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08 133 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04 134 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02 135 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01 136 #define SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0f 137 138 /* Common regulator mode register layout */ 139 #define SPMI_COMMON_MODE_HPM_MASK 0x80 140 #define SPMI_COMMON_MODE_AUTO_MASK 0x40 141 #define SPMI_COMMON_MODE_BYPASS_MASK 0x20 142 #define SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10 143 #define SPMI_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08 144 #define SPMI_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04 145 #define SPMI_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02 146 #define SPMI_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01 147 #define SPMI_COMMON_MODE_FOLLOW_ALL_MASK 0x1f 148 149 /* Common regulator pull down control register layout */ 150 #define SPMI_COMMON_PULL_DOWN_ENABLE_MASK 0x80 151 152 /* LDO regulator current limit control register layout */ 153 #define SPMI_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80 154 155 /* LDO regulator soft start control register layout */ 156 #define SPMI_LDO_SOFT_START_ENABLE_MASK 0x80 157 158 /* VS regulator over current protection control register layout */ 159 #define SPMI_VS_OCP_OVERRIDE 0x01 160 #define SPMI_VS_OCP_NO_OVERRIDE 0x00 161 162 /* VS regulator soft start control register layout */ 163 #define SPMI_VS_SOFT_START_ENABLE_MASK 0x80 164 #define SPMI_VS_SOFT_START_SEL_MASK 0x03 165 166 /* Boost regulator current limit control register layout */ 167 #define SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80 168 #define SPMI_BOOST_CURRENT_LIMIT_MASK 0x07 169 170 #define SPMI_VS_OCP_DEFAULT_MAX_RETRIES 10 171 #define SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS 30 172 #define SPMI_VS_OCP_FALL_DELAY_US 90 173 #define SPMI_VS_OCP_FAULT_DELAY_US 20000 174 175 #define SPMI_FTSMPS_STEP_CTRL_STEP_MASK 0x18 176 #define SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT 3 177 #define SPMI_FTSMPS_STEP_CTRL_DELAY_MASK 0x07 178 #define SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT 0 179 180 /* Clock rate in kHz of the FTSMPS regulator reference clock. */ 181 #define SPMI_FTSMPS_CLOCK_RATE 19200 182 183 /* Minimum voltage stepper delay for each step. */ 184 #define SPMI_FTSMPS_STEP_DELAY 8 185 186 /* 187 * The ratio SPMI_FTSMPS_STEP_MARGIN_NUM/SPMI_FTSMPS_STEP_MARGIN_DEN is used to 188 * adjust the step rate in order to account for oscillator variance. 189 */ 190 #define SPMI_FTSMPS_STEP_MARGIN_NUM 4 191 #define SPMI_FTSMPS_STEP_MARGIN_DEN 5 192 193 /* 194 * This voltage in uV is returned by get_voltage functions when there is no way 195 * to determine the current voltage level. It is needed because the regulator 196 * framework treats a 0 uV voltage as an error. 197 */ 198 #define VOLTAGE_UNKNOWN 1 199 200 /* VSET value to decide the range of ULT SMPS */ 201 #define ULT_SMPS_RANGE_SPLIT 0x60 202 203 /** 204 * struct spmi_voltage_range - regulator set point voltage mapping description 205 * @min_uV: Minimum programmable output voltage resulting from 206 * set point register value 0x00 207 * @max_uV: Maximum programmable output voltage 208 * @step_uV: Output voltage increase resulting from the set point 209 * register value increasing by 1 210 * @set_point_min_uV: Minimum allowed voltage 211 * @set_point_max_uV: Maximum allowed voltage. This may be tweaked in order 212 * to pick which range should be used in the case of 213 * overlapping set points. 214 * @n_voltages: Number of preferred voltage set points present in this 215 * range 216 * @range_sel: Voltage range register value corresponding to this range 217 * 218 * The following relationships must be true for the values used in this struct: 219 * (max_uV - min_uV) % step_uV == 0 220 * (set_point_min_uV - min_uV) % step_uV == 0* 221 * (set_point_max_uV - min_uV) % step_uV == 0* 222 * n_voltages = (set_point_max_uV - set_point_min_uV) / step_uV + 1 223 * 224 * *Note, set_point_min_uV == set_point_max_uV == 0 is allowed in order to 225 * specify that the voltage range has meaning, but is not preferred. 226 */ 227 struct spmi_voltage_range { 228 int min_uV; 229 int max_uV; 230 int step_uV; 231 int set_point_min_uV; 232 int set_point_max_uV; 233 unsigned n_voltages; 234 u8 range_sel; 235 }; 236 237 /* 238 * The ranges specified in the spmi_voltage_set_points struct must be listed 239 * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV. 240 */ 241 struct spmi_voltage_set_points { 242 struct spmi_voltage_range *range; 243 int count; 244 unsigned n_voltages; 245 }; 246 247 struct spmi_regulator { 248 struct regulator_desc desc; 249 struct device *dev; 250 struct delayed_work ocp_work; 251 struct regmap *regmap; 252 struct spmi_voltage_set_points *set_points; 253 enum spmi_regulator_logical_type logical_type; 254 int ocp_irq; 255 int ocp_count; 256 int ocp_max_retries; 257 int ocp_retry_delay_ms; 258 int hpm_min_load; 259 int slew_rate; 260 ktime_t vs_enable_time; 261 u16 base; 262 struct list_head node; 263 }; 264 265 struct spmi_regulator_mapping { 266 enum spmi_regulator_type type; 267 enum spmi_regulator_subtype subtype; 268 enum spmi_regulator_logical_type logical_type; 269 u32 revision_min; 270 u32 revision_max; 271 struct regulator_ops *ops; 272 struct spmi_voltage_set_points *set_points; 273 int hpm_min_load; 274 }; 275 276 struct spmi_regulator_data { 277 const char *name; 278 u16 base; 279 const char *supply; 280 const char *ocp; 281 u16 force_type; 282 }; 283 284 #define SPMI_VREG(_type, _subtype, _dig_major_min, _dig_major_max, \ 285 _logical_type, _ops_val, _set_points_val, _hpm_min_load) \ 286 { \ 287 .type = SPMI_REGULATOR_TYPE_##_type, \ 288 .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \ 289 .revision_min = _dig_major_min, \ 290 .revision_max = _dig_major_max, \ 291 .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_##_logical_type, \ 292 .ops = &spmi_##_ops_val##_ops, \ 293 .set_points = &_set_points_val##_set_points, \ 294 .hpm_min_load = _hpm_min_load, \ 295 } 296 297 #define SPMI_VREG_VS(_subtype, _dig_major_min, _dig_major_max) \ 298 { \ 299 .type = SPMI_REGULATOR_TYPE_VS, \ 300 .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \ 301 .revision_min = _dig_major_min, \ 302 .revision_max = _dig_major_max, \ 303 .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_VS, \ 304 .ops = &spmi_vs_ops, \ 305 } 306 307 #define SPMI_VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, \ 308 _set_point_max_uV, _max_uV, _step_uV) \ 309 { \ 310 .min_uV = _min_uV, \ 311 .max_uV = _max_uV, \ 312 .set_point_min_uV = _set_point_min_uV, \ 313 .set_point_max_uV = _set_point_max_uV, \ 314 .step_uV = _step_uV, \ 315 .range_sel = _range_sel, \ 316 } 317 318 #define DEFINE_SPMI_SET_POINTS(name) \ 319 struct spmi_voltage_set_points name##_set_points = { \ 320 .range = name##_ranges, \ 321 .count = ARRAY_SIZE(name##_ranges), \ 322 } 323 324 /* 325 * These tables contain the physically available PMIC regulator voltage setpoint 326 * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed 327 * to ensure that the setpoints available to software are monotonically 328 * increasing and unique. The set_voltage callback functions expect these 329 * properties to hold. 330 */ 331 static struct spmi_voltage_range pldo_ranges[] = { 332 SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500), 333 SPMI_VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000), 334 SPMI_VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000), 335 }; 336 337 static struct spmi_voltage_range nldo1_ranges[] = { 338 SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500), 339 }; 340 341 static struct spmi_voltage_range nldo2_ranges[] = { 342 SPMI_VOLTAGE_RANGE(0, 375000, 0, 0, 1537500, 12500), 343 SPMI_VOLTAGE_RANGE(1, 375000, 375000, 768750, 768750, 6250), 344 SPMI_VOLTAGE_RANGE(2, 750000, 775000, 1537500, 1537500, 12500), 345 }; 346 347 static struct spmi_voltage_range nldo3_ranges[] = { 348 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500), 349 SPMI_VOLTAGE_RANGE(1, 375000, 0, 0, 1537500, 12500), 350 SPMI_VOLTAGE_RANGE(2, 750000, 0, 0, 1537500, 12500), 351 }; 352 353 static struct spmi_voltage_range ln_ldo_ranges[] = { 354 SPMI_VOLTAGE_RANGE(1, 690000, 690000, 1110000, 1110000, 60000), 355 SPMI_VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000), 356 }; 357 358 static struct spmi_voltage_range smps_ranges[] = { 359 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500), 360 SPMI_VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000), 361 }; 362 363 static struct spmi_voltage_range ftsmps_ranges[] = { 364 SPMI_VOLTAGE_RANGE(0, 0, 350000, 1275000, 1275000, 5000), 365 SPMI_VOLTAGE_RANGE(1, 0, 1280000, 2040000, 2040000, 10000), 366 }; 367 368 static struct spmi_voltage_range ftsmps2p5_ranges[] = { 369 SPMI_VOLTAGE_RANGE(0, 80000, 350000, 1355000, 1355000, 5000), 370 SPMI_VOLTAGE_RANGE(1, 160000, 1360000, 2200000, 2200000, 10000), 371 }; 372 373 static struct spmi_voltage_range boost_ranges[] = { 374 SPMI_VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000), 375 }; 376 377 static struct spmi_voltage_range boost_byp_ranges[] = { 378 SPMI_VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000), 379 }; 380 381 static struct spmi_voltage_range ult_lo_smps_ranges[] = { 382 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500), 383 SPMI_VOLTAGE_RANGE(1, 750000, 0, 0, 1525000, 25000), 384 }; 385 386 static struct spmi_voltage_range ult_ho_smps_ranges[] = { 387 SPMI_VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000), 388 }; 389 390 static struct spmi_voltage_range ult_nldo_ranges[] = { 391 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500), 392 }; 393 394 static struct spmi_voltage_range ult_pldo_ranges[] = { 395 SPMI_VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500), 396 }; 397 398 static DEFINE_SPMI_SET_POINTS(pldo); 399 static DEFINE_SPMI_SET_POINTS(nldo1); 400 static DEFINE_SPMI_SET_POINTS(nldo2); 401 static DEFINE_SPMI_SET_POINTS(nldo3); 402 static DEFINE_SPMI_SET_POINTS(ln_ldo); 403 static DEFINE_SPMI_SET_POINTS(smps); 404 static DEFINE_SPMI_SET_POINTS(ftsmps); 405 static DEFINE_SPMI_SET_POINTS(ftsmps2p5); 406 static DEFINE_SPMI_SET_POINTS(boost); 407 static DEFINE_SPMI_SET_POINTS(boost_byp); 408 static DEFINE_SPMI_SET_POINTS(ult_lo_smps); 409 static DEFINE_SPMI_SET_POINTS(ult_ho_smps); 410 static DEFINE_SPMI_SET_POINTS(ult_nldo); 411 static DEFINE_SPMI_SET_POINTS(ult_pldo); 412 413 static inline int spmi_vreg_read(struct spmi_regulator *vreg, u16 addr, u8 *buf, 414 int len) 415 { 416 return regmap_bulk_read(vreg->regmap, vreg->base + addr, buf, len); 417 } 418 419 static inline int spmi_vreg_write(struct spmi_regulator *vreg, u16 addr, 420 u8 *buf, int len) 421 { 422 return regmap_bulk_write(vreg->regmap, vreg->base + addr, buf, len); 423 } 424 425 static int spmi_vreg_update_bits(struct spmi_regulator *vreg, u16 addr, u8 val, 426 u8 mask) 427 { 428 return regmap_update_bits(vreg->regmap, vreg->base + addr, mask, val); 429 } 430 431 static int spmi_regulator_common_is_enabled(struct regulator_dev *rdev) 432 { 433 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 434 u8 reg; 435 436 spmi_vreg_read(vreg, SPMI_COMMON_REG_ENABLE, ®, 1); 437 438 return (reg & SPMI_COMMON_ENABLE_MASK) == SPMI_COMMON_ENABLE; 439 } 440 441 static int spmi_regulator_common_enable(struct regulator_dev *rdev) 442 { 443 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 444 445 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE, 446 SPMI_COMMON_ENABLE, SPMI_COMMON_ENABLE_MASK); 447 } 448 449 static int spmi_regulator_vs_enable(struct regulator_dev *rdev) 450 { 451 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 452 453 if (vreg->ocp_irq) { 454 vreg->ocp_count = 0; 455 vreg->vs_enable_time = ktime_get(); 456 } 457 458 return spmi_regulator_common_enable(rdev); 459 } 460 461 static int spmi_regulator_common_disable(struct regulator_dev *rdev) 462 { 463 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 464 465 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE, 466 SPMI_COMMON_DISABLE, SPMI_COMMON_ENABLE_MASK); 467 } 468 469 static int spmi_regulator_select_voltage(struct spmi_regulator *vreg, 470 int min_uV, int max_uV, u8 *range_sel, u8 *voltage_sel, 471 unsigned *selector) 472 { 473 const struct spmi_voltage_range *range; 474 int uV = min_uV; 475 int lim_min_uV, lim_max_uV, i, range_id, range_max_uV; 476 477 /* Check if request voltage is outside of physically settable range. */ 478 lim_min_uV = vreg->set_points->range[0].set_point_min_uV; 479 lim_max_uV = 480 vreg->set_points->range[vreg->set_points->count - 1].set_point_max_uV; 481 482 if (uV < lim_min_uV && max_uV >= lim_min_uV) 483 uV = lim_min_uV; 484 485 if (uV < lim_min_uV || uV > lim_max_uV) { 486 dev_err(vreg->dev, 487 "request v=[%d, %d] is outside possible v=[%d, %d]\n", 488 min_uV, max_uV, lim_min_uV, lim_max_uV); 489 return -EINVAL; 490 } 491 492 /* Find the range which uV is inside of. */ 493 for (i = vreg->set_points->count - 1; i > 0; i--) { 494 range_max_uV = vreg->set_points->range[i - 1].set_point_max_uV; 495 if (uV > range_max_uV && range_max_uV > 0) 496 break; 497 } 498 499 range_id = i; 500 range = &vreg->set_points->range[range_id]; 501 *range_sel = range->range_sel; 502 503 /* 504 * Force uV to be an allowed set point by applying a ceiling function to 505 * the uV value. 506 */ 507 *voltage_sel = (uV - range->min_uV + range->step_uV - 1) 508 / range->step_uV; 509 uV = *voltage_sel * range->step_uV + range->min_uV; 510 511 if (uV > max_uV) { 512 dev_err(vreg->dev, 513 "request v=[%d, %d] cannot be met by any set point; " 514 "next set point: %d\n", 515 min_uV, max_uV, uV); 516 return -EINVAL; 517 } 518 519 *selector = 0; 520 for (i = 0; i < range_id; i++) 521 *selector += vreg->set_points->range[i].n_voltages; 522 *selector += (uV - range->set_point_min_uV) / range->step_uV; 523 524 return 0; 525 } 526 527 static const struct spmi_voltage_range * 528 spmi_regulator_find_range(struct spmi_regulator *vreg) 529 { 530 u8 range_sel; 531 const struct spmi_voltage_range *range, *end; 532 533 range = vreg->set_points->range; 534 end = range + vreg->set_points->count; 535 536 spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, &range_sel, 1); 537 538 for (; range < end; range++) 539 if (range->range_sel == range_sel) 540 return range; 541 542 return NULL; 543 } 544 545 static int spmi_regulator_select_voltage_same_range(struct spmi_regulator *vreg, 546 int min_uV, int max_uV, u8 *range_sel, u8 *voltage_sel, 547 unsigned *selector) 548 { 549 const struct spmi_voltage_range *range; 550 int uV = min_uV; 551 int i; 552 553 range = spmi_regulator_find_range(vreg); 554 if (!range) 555 goto different_range; 556 557 if (uV < range->min_uV && max_uV >= range->min_uV) 558 uV = range->min_uV; 559 560 if (uV < range->min_uV || uV > range->max_uV) { 561 /* Current range doesn't support the requested voltage. */ 562 goto different_range; 563 } 564 565 /* 566 * Force uV to be an allowed set point by applying a ceiling function to 567 * the uV value. 568 */ 569 *voltage_sel = DIV_ROUND_UP(uV - range->min_uV, range->step_uV); 570 uV = *voltage_sel * range->step_uV + range->min_uV; 571 572 if (uV > max_uV) { 573 /* 574 * No set point in the current voltage range is within the 575 * requested min_uV to max_uV range. 576 */ 577 goto different_range; 578 } 579 580 *selector = 0; 581 for (i = 0; i < vreg->set_points->count; i++) { 582 if (uV >= vreg->set_points->range[i].set_point_min_uV 583 && uV <= vreg->set_points->range[i].set_point_max_uV) { 584 *selector += 585 (uV - vreg->set_points->range[i].set_point_min_uV) 586 / vreg->set_points->range[i].step_uV; 587 break; 588 } 589 590 *selector += vreg->set_points->range[i].n_voltages; 591 } 592 593 if (*selector >= vreg->set_points->n_voltages) 594 goto different_range; 595 596 return 0; 597 598 different_range: 599 return spmi_regulator_select_voltage(vreg, min_uV, max_uV, 600 range_sel, voltage_sel, selector); 601 } 602 603 static int spmi_regulator_common_set_voltage(struct regulator_dev *rdev, 604 int min_uV, int max_uV, unsigned *selector) 605 { 606 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 607 int ret; 608 u8 buf[2]; 609 u8 range_sel, voltage_sel; 610 611 /* 612 * Favor staying in the current voltage range if possible. This avoids 613 * voltage spikes that occur when changing the voltage range. 614 */ 615 ret = spmi_regulator_select_voltage_same_range(vreg, min_uV, max_uV, 616 &range_sel, &voltage_sel, selector); 617 if (ret) 618 return ret; 619 620 buf[0] = range_sel; 621 buf[1] = voltage_sel; 622 return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, buf, 2); 623 } 624 625 static int spmi_regulator_set_voltage_time_sel(struct regulator_dev *rdev, 626 unsigned int old_selector, unsigned int new_selector) 627 { 628 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 629 const struct spmi_voltage_range *range; 630 int diff_uV; 631 632 range = spmi_regulator_find_range(vreg); 633 if (!range) 634 return -EINVAL; 635 636 diff_uV = abs(new_selector - old_selector) * range->step_uV; 637 638 return DIV_ROUND_UP(diff_uV, vreg->slew_rate); 639 } 640 641 static int spmi_regulator_common_get_voltage(struct regulator_dev *rdev) 642 { 643 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 644 const struct spmi_voltage_range *range; 645 u8 voltage_sel; 646 647 spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1); 648 649 range = spmi_regulator_find_range(vreg); 650 if (!range) 651 return VOLTAGE_UNKNOWN; 652 653 return range->step_uV * voltage_sel + range->min_uV; 654 } 655 656 static int spmi_regulator_single_range_set_voltage(struct regulator_dev *rdev, 657 int min_uV, int max_uV, unsigned *selector) 658 { 659 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 660 int ret; 661 u8 range_sel, sel; 662 663 ret = spmi_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel, 664 &sel, selector); 665 if (ret) { 666 dev_err(vreg->dev, "could not set voltage, ret=%d\n", ret); 667 return ret; 668 } 669 670 /* 671 * Certain types of regulators do not have a range select register so 672 * only voltage set register needs to be written. 673 */ 674 return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &sel, 1); 675 } 676 677 static int spmi_regulator_single_range_get_voltage(struct regulator_dev *rdev) 678 { 679 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 680 const struct spmi_voltage_range *range = vreg->set_points->range; 681 u8 voltage_sel; 682 683 spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1); 684 685 return range->step_uV * voltage_sel + range->min_uV; 686 } 687 688 static int spmi_regulator_ult_lo_smps_set_voltage(struct regulator_dev *rdev, 689 int min_uV, int max_uV, unsigned *selector) 690 { 691 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 692 int ret; 693 u8 range_sel, voltage_sel; 694 695 /* 696 * Favor staying in the current voltage range if possible. This avoids 697 * voltage spikes that occur when changing the voltage range. 698 */ 699 ret = spmi_regulator_select_voltage_same_range(vreg, min_uV, max_uV, 700 &range_sel, &voltage_sel, selector); 701 if (ret) 702 return ret; 703 704 /* 705 * Calculate VSET based on range 706 * In case of range 0: voltage_sel is a 7 bit value, can be written 707 * witout any modification. 708 * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to 709 * [011]. 710 */ 711 if (range_sel == 1) 712 voltage_sel |= ULT_SMPS_RANGE_SPLIT; 713 714 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_VOLTAGE_SET, 715 voltage_sel, 0xff); 716 } 717 718 static int spmi_regulator_ult_lo_smps_get_voltage(struct regulator_dev *rdev) 719 { 720 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 721 const struct spmi_voltage_range *range; 722 u8 voltage_sel; 723 724 spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1); 725 726 range = spmi_regulator_find_range(vreg); 727 if (!range) 728 return VOLTAGE_UNKNOWN; 729 730 if (range->range_sel == 1) 731 voltage_sel &= ~ULT_SMPS_RANGE_SPLIT; 732 733 return range->step_uV * voltage_sel + range->min_uV; 734 } 735 736 static int spmi_regulator_common_list_voltage(struct regulator_dev *rdev, 737 unsigned selector) 738 { 739 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 740 int uV = 0; 741 int i; 742 743 if (selector >= vreg->set_points->n_voltages) 744 return 0; 745 746 for (i = 0; i < vreg->set_points->count; i++) { 747 if (selector < vreg->set_points->range[i].n_voltages) { 748 uV = selector * vreg->set_points->range[i].step_uV 749 + vreg->set_points->range[i].set_point_min_uV; 750 break; 751 } 752 753 selector -= vreg->set_points->range[i].n_voltages; 754 } 755 756 return uV; 757 } 758 759 static int 760 spmi_regulator_common_set_bypass(struct regulator_dev *rdev, bool enable) 761 { 762 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 763 u8 mask = SPMI_COMMON_MODE_BYPASS_MASK; 764 u8 val = 0; 765 766 if (enable) 767 val = mask; 768 769 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask); 770 } 771 772 static int 773 spmi_regulator_common_get_bypass(struct regulator_dev *rdev, bool *enable) 774 { 775 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 776 u8 val; 777 int ret; 778 779 ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &val, 1); 780 *enable = val & SPMI_COMMON_MODE_BYPASS_MASK; 781 782 return ret; 783 } 784 785 static unsigned int spmi_regulator_common_get_mode(struct regulator_dev *rdev) 786 { 787 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 788 u8 reg; 789 790 spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, ®, 1); 791 792 if (reg & SPMI_COMMON_MODE_HPM_MASK) 793 return REGULATOR_MODE_NORMAL; 794 795 return REGULATOR_MODE_IDLE; 796 } 797 798 static int 799 spmi_regulator_common_set_mode(struct regulator_dev *rdev, unsigned int mode) 800 { 801 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 802 u8 mask = SPMI_COMMON_MODE_HPM_MASK; 803 u8 val = 0; 804 805 if (mode == REGULATOR_MODE_NORMAL) 806 val = mask; 807 808 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask); 809 } 810 811 static int 812 spmi_regulator_common_set_load(struct regulator_dev *rdev, int load_uA) 813 { 814 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 815 unsigned int mode; 816 817 if (load_uA >= vreg->hpm_min_load) 818 mode = REGULATOR_MODE_NORMAL; 819 else 820 mode = REGULATOR_MODE_IDLE; 821 822 return spmi_regulator_common_set_mode(rdev, mode); 823 } 824 825 static int spmi_regulator_common_set_pull_down(struct regulator_dev *rdev) 826 { 827 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 828 unsigned int mask = SPMI_COMMON_PULL_DOWN_ENABLE_MASK; 829 830 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_PULL_DOWN, 831 mask, mask); 832 } 833 834 static int spmi_regulator_common_set_soft_start(struct regulator_dev *rdev) 835 { 836 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 837 unsigned int mask = SPMI_LDO_SOFT_START_ENABLE_MASK; 838 839 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_SOFT_START, 840 mask, mask); 841 } 842 843 static int spmi_regulator_set_ilim(struct regulator_dev *rdev, int ilim_uA) 844 { 845 struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 846 enum spmi_regulator_logical_type type = vreg->logical_type; 847 unsigned int current_reg; 848 u8 reg; 849 u8 mask = SPMI_BOOST_CURRENT_LIMIT_MASK | 850 SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK; 851 int max = (SPMI_BOOST_CURRENT_LIMIT_MASK + 1) * 500; 852 853 if (type == SPMI_REGULATOR_LOGICAL_TYPE_BOOST) 854 current_reg = SPMI_BOOST_REG_CURRENT_LIMIT; 855 else 856 current_reg = SPMI_BOOST_BYP_REG_CURRENT_LIMIT; 857 858 if (ilim_uA > max || ilim_uA <= 0) 859 return -EINVAL; 860 861 reg = (ilim_uA - 1) / 500; 862 reg |= SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK; 863 864 return spmi_vreg_update_bits(vreg, current_reg, reg, mask); 865 } 866 867 static int spmi_regulator_vs_clear_ocp(struct spmi_regulator *vreg) 868 { 869 int ret; 870 871 ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE, 872 SPMI_COMMON_DISABLE, SPMI_COMMON_ENABLE_MASK); 873 874 vreg->vs_enable_time = ktime_get(); 875 876 ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE, 877 SPMI_COMMON_ENABLE, SPMI_COMMON_ENABLE_MASK); 878 879 return ret; 880 } 881 882 static void spmi_regulator_vs_ocp_work(struct work_struct *work) 883 { 884 struct delayed_work *dwork = to_delayed_work(work); 885 struct spmi_regulator *vreg 886 = container_of(dwork, struct spmi_regulator, ocp_work); 887 888 spmi_regulator_vs_clear_ocp(vreg); 889 } 890 891 static irqreturn_t spmi_regulator_vs_ocp_isr(int irq, void *data) 892 { 893 struct spmi_regulator *vreg = data; 894 ktime_t ocp_irq_time; 895 s64 ocp_trigger_delay_us; 896 897 ocp_irq_time = ktime_get(); 898 ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time, 899 vreg->vs_enable_time); 900 901 /* 902 * Reset the OCP count if there is a large delay between switch enable 903 * and when OCP triggers. This is indicative of a hotplug event as 904 * opposed to a fault. 905 */ 906 if (ocp_trigger_delay_us > SPMI_VS_OCP_FAULT_DELAY_US) 907 vreg->ocp_count = 0; 908 909 /* Wait for switch output to settle back to 0 V after OCP triggered. */ 910 udelay(SPMI_VS_OCP_FALL_DELAY_US); 911 912 vreg->ocp_count++; 913 914 if (vreg->ocp_count == 1) { 915 /* Immediately clear the over current condition. */ 916 spmi_regulator_vs_clear_ocp(vreg); 917 } else if (vreg->ocp_count <= vreg->ocp_max_retries) { 918 /* Schedule the over current clear task to run later. */ 919 schedule_delayed_work(&vreg->ocp_work, 920 msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1); 921 } else { 922 dev_err(vreg->dev, 923 "OCP triggered %d times; no further retries\n", 924 vreg->ocp_count); 925 } 926 927 return IRQ_HANDLED; 928 } 929 930 static struct regulator_ops spmi_smps_ops = { 931 .enable = spmi_regulator_common_enable, 932 .disable = spmi_regulator_common_disable, 933 .is_enabled = spmi_regulator_common_is_enabled, 934 .set_voltage = spmi_regulator_common_set_voltage, 935 .get_voltage = spmi_regulator_common_get_voltage, 936 .list_voltage = spmi_regulator_common_list_voltage, 937 .set_mode = spmi_regulator_common_set_mode, 938 .get_mode = spmi_regulator_common_get_mode, 939 .set_load = spmi_regulator_common_set_load, 940 .set_pull_down = spmi_regulator_common_set_pull_down, 941 }; 942 943 static struct regulator_ops spmi_ldo_ops = { 944 .enable = spmi_regulator_common_enable, 945 .disable = spmi_regulator_common_disable, 946 .is_enabled = spmi_regulator_common_is_enabled, 947 .set_voltage = spmi_regulator_common_set_voltage, 948 .get_voltage = spmi_regulator_common_get_voltage, 949 .list_voltage = spmi_regulator_common_list_voltage, 950 .set_mode = spmi_regulator_common_set_mode, 951 .get_mode = spmi_regulator_common_get_mode, 952 .set_load = spmi_regulator_common_set_load, 953 .set_bypass = spmi_regulator_common_set_bypass, 954 .get_bypass = spmi_regulator_common_get_bypass, 955 .set_pull_down = spmi_regulator_common_set_pull_down, 956 .set_soft_start = spmi_regulator_common_set_soft_start, 957 }; 958 959 static struct regulator_ops spmi_ln_ldo_ops = { 960 .enable = spmi_regulator_common_enable, 961 .disable = spmi_regulator_common_disable, 962 .is_enabled = spmi_regulator_common_is_enabled, 963 .set_voltage = spmi_regulator_common_set_voltage, 964 .get_voltage = spmi_regulator_common_get_voltage, 965 .list_voltage = spmi_regulator_common_list_voltage, 966 .set_bypass = spmi_regulator_common_set_bypass, 967 .get_bypass = spmi_regulator_common_get_bypass, 968 }; 969 970 static struct regulator_ops spmi_vs_ops = { 971 .enable = spmi_regulator_vs_enable, 972 .disable = spmi_regulator_common_disable, 973 .is_enabled = spmi_regulator_common_is_enabled, 974 .set_pull_down = spmi_regulator_common_set_pull_down, 975 .set_soft_start = spmi_regulator_common_set_soft_start, 976 }; 977 978 static struct regulator_ops spmi_boost_ops = { 979 .enable = spmi_regulator_common_enable, 980 .disable = spmi_regulator_common_disable, 981 .is_enabled = spmi_regulator_common_is_enabled, 982 .set_voltage = spmi_regulator_single_range_set_voltage, 983 .get_voltage = spmi_regulator_single_range_get_voltage, 984 .list_voltage = spmi_regulator_common_list_voltage, 985 .set_input_current_limit = spmi_regulator_set_ilim, 986 }; 987 988 static struct regulator_ops spmi_ftsmps_ops = { 989 .enable = spmi_regulator_common_enable, 990 .disable = spmi_regulator_common_disable, 991 .is_enabled = spmi_regulator_common_is_enabled, 992 .set_voltage = spmi_regulator_common_set_voltage, 993 .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 994 .get_voltage = spmi_regulator_common_get_voltage, 995 .list_voltage = spmi_regulator_common_list_voltage, 996 .set_mode = spmi_regulator_common_set_mode, 997 .get_mode = spmi_regulator_common_get_mode, 998 .set_load = spmi_regulator_common_set_load, 999 .set_pull_down = spmi_regulator_common_set_pull_down, 1000 }; 1001 1002 static struct regulator_ops spmi_ult_lo_smps_ops = { 1003 .enable = spmi_regulator_common_enable, 1004 .disable = spmi_regulator_common_disable, 1005 .is_enabled = spmi_regulator_common_is_enabled, 1006 .set_voltage = spmi_regulator_ult_lo_smps_set_voltage, 1007 .get_voltage = spmi_regulator_ult_lo_smps_get_voltage, 1008 .list_voltage = spmi_regulator_common_list_voltage, 1009 .set_mode = spmi_regulator_common_set_mode, 1010 .get_mode = spmi_regulator_common_get_mode, 1011 .set_load = spmi_regulator_common_set_load, 1012 .set_pull_down = spmi_regulator_common_set_pull_down, 1013 }; 1014 1015 static struct regulator_ops spmi_ult_ho_smps_ops = { 1016 .enable = spmi_regulator_common_enable, 1017 .disable = spmi_regulator_common_disable, 1018 .is_enabled = spmi_regulator_common_is_enabled, 1019 .set_voltage = spmi_regulator_single_range_set_voltage, 1020 .get_voltage = spmi_regulator_single_range_get_voltage, 1021 .list_voltage = spmi_regulator_common_list_voltage, 1022 .set_mode = spmi_regulator_common_set_mode, 1023 .get_mode = spmi_regulator_common_get_mode, 1024 .set_load = spmi_regulator_common_set_load, 1025 .set_pull_down = spmi_regulator_common_set_pull_down, 1026 }; 1027 1028 static struct regulator_ops spmi_ult_ldo_ops = { 1029 .enable = spmi_regulator_common_enable, 1030 .disable = spmi_regulator_common_disable, 1031 .is_enabled = spmi_regulator_common_is_enabled, 1032 .set_voltage = spmi_regulator_single_range_set_voltage, 1033 .get_voltage = spmi_regulator_single_range_get_voltage, 1034 .list_voltage = spmi_regulator_common_list_voltage, 1035 .set_mode = spmi_regulator_common_set_mode, 1036 .get_mode = spmi_regulator_common_get_mode, 1037 .set_load = spmi_regulator_common_set_load, 1038 .set_bypass = spmi_regulator_common_set_bypass, 1039 .get_bypass = spmi_regulator_common_get_bypass, 1040 .set_pull_down = spmi_regulator_common_set_pull_down, 1041 .set_soft_start = spmi_regulator_common_set_soft_start, 1042 }; 1043 1044 /* Maximum possible digital major revision value */ 1045 #define INF 0xFF 1046 1047 static const struct spmi_regulator_mapping supported_regulators[] = { 1048 /* type subtype dig_min dig_max ltype ops setpoints hpm_min */ 1049 SPMI_VREG(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000), 1050 SPMI_VREG(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000), 1051 SPMI_VREG(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000), 1052 SPMI_VREG(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000), 1053 SPMI_VREG(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000), 1054 SPMI_VREG(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000), 1055 SPMI_VREG(LDO, N600_ST, 0, 0, LDO, ldo, nldo2, 10000), 1056 SPMI_VREG(LDO, N1200_ST, 0, 0, LDO, ldo, nldo2, 10000), 1057 SPMI_VREG(LDO, N600_ST, 1, INF, LDO, ldo, nldo3, 10000), 1058 SPMI_VREG(LDO, N1200_ST, 1, INF, LDO, ldo, nldo3, 10000), 1059 SPMI_VREG(LDO, P50, 0, INF, LDO, ldo, pldo, 5000), 1060 SPMI_VREG(LDO, P150, 0, INF, LDO, ldo, pldo, 10000), 1061 SPMI_VREG(LDO, P300, 0, INF, LDO, ldo, pldo, 10000), 1062 SPMI_VREG(LDO, P600, 0, INF, LDO, ldo, pldo, 10000), 1063 SPMI_VREG(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000), 1064 SPMI_VREG(LDO, LN, 0, INF, LN_LDO, ln_ldo, ln_ldo, 0), 1065 SPMI_VREG(LDO, LV_P50, 0, INF, LDO, ldo, pldo, 5000), 1066 SPMI_VREG(LDO, LV_P150, 0, INF, LDO, ldo, pldo, 10000), 1067 SPMI_VREG(LDO, LV_P300, 0, INF, LDO, ldo, pldo, 10000), 1068 SPMI_VREG(LDO, LV_P600, 0, INF, LDO, ldo, pldo, 10000), 1069 SPMI_VREG(LDO, LV_P1200, 0, INF, LDO, ldo, pldo, 10000), 1070 SPMI_VREG_VS(LV100, 0, INF), 1071 SPMI_VREG_VS(LV300, 0, INF), 1072 SPMI_VREG_VS(MV300, 0, INF), 1073 SPMI_VREG_VS(MV500, 0, INF), 1074 SPMI_VREG_VS(HDMI, 0, INF), 1075 SPMI_VREG_VS(OTG, 0, INF), 1076 SPMI_VREG(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0), 1077 SPMI_VREG(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000), 1078 SPMI_VREG(FTS, FTS2p5_CTL, 0, INF, FTSMPS, ftsmps, ftsmps2p5, 100000), 1079 SPMI_VREG(BOOST_BYP, BB_2A, 0, INF, BOOST_BYP, boost, boost_byp, 0), 1080 SPMI_VREG(ULT_BUCK, ULT_HF_CTL1, 0, INF, ULT_LO_SMPS, ult_lo_smps, 1081 ult_lo_smps, 100000), 1082 SPMI_VREG(ULT_BUCK, ULT_HF_CTL2, 0, INF, ULT_LO_SMPS, ult_lo_smps, 1083 ult_lo_smps, 100000), 1084 SPMI_VREG(ULT_BUCK, ULT_HF_CTL3, 0, INF, ULT_LO_SMPS, ult_lo_smps, 1085 ult_lo_smps, 100000), 1086 SPMI_VREG(ULT_BUCK, ULT_HF_CTL4, 0, INF, ULT_HO_SMPS, ult_ho_smps, 1087 ult_ho_smps, 100000), 1088 SPMI_VREG(ULT_LDO, N300_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1089 SPMI_VREG(ULT_LDO, N600_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1090 SPMI_VREG(ULT_LDO, N900_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1091 SPMI_VREG(ULT_LDO, N1200_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1092 SPMI_VREG(ULT_LDO, LV_P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1093 SPMI_VREG(ULT_LDO, LV_P300, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1094 SPMI_VREG(ULT_LDO, LV_P450, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1095 SPMI_VREG(ULT_LDO, P600, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1096 SPMI_VREG(ULT_LDO, P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1097 SPMI_VREG(ULT_LDO, P50, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 5000), 1098 }; 1099 1100 static void spmi_calculate_num_voltages(struct spmi_voltage_set_points *points) 1101 { 1102 unsigned int n; 1103 struct spmi_voltage_range *range = points->range; 1104 1105 for (; range < points->range + points->count; range++) { 1106 n = 0; 1107 if (range->set_point_max_uV) { 1108 n = range->set_point_max_uV - range->set_point_min_uV; 1109 n = (n / range->step_uV) + 1; 1110 } 1111 range->n_voltages = n; 1112 points->n_voltages += n; 1113 } 1114 } 1115 1116 static int spmi_regulator_match(struct spmi_regulator *vreg, u16 force_type) 1117 { 1118 const struct spmi_regulator_mapping *mapping; 1119 int ret, i; 1120 u32 dig_major_rev; 1121 u8 version[SPMI_COMMON_REG_SUBTYPE - SPMI_COMMON_REG_DIG_MAJOR_REV + 1]; 1122 u8 type, subtype; 1123 1124 ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_DIG_MAJOR_REV, version, 1125 ARRAY_SIZE(version)); 1126 if (ret) { 1127 dev_err(vreg->dev, "could not read version registers\n"); 1128 return ret; 1129 } 1130 dig_major_rev = version[SPMI_COMMON_REG_DIG_MAJOR_REV 1131 - SPMI_COMMON_REG_DIG_MAJOR_REV]; 1132 if (!force_type) { 1133 type = version[SPMI_COMMON_REG_TYPE - 1134 SPMI_COMMON_REG_DIG_MAJOR_REV]; 1135 subtype = version[SPMI_COMMON_REG_SUBTYPE - 1136 SPMI_COMMON_REG_DIG_MAJOR_REV]; 1137 } else { 1138 type = force_type >> 8; 1139 subtype = force_type; 1140 } 1141 1142 for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) { 1143 mapping = &supported_regulators[i]; 1144 if (mapping->type == type && mapping->subtype == subtype 1145 && mapping->revision_min <= dig_major_rev 1146 && mapping->revision_max >= dig_major_rev) 1147 goto found; 1148 } 1149 1150 dev_err(vreg->dev, 1151 "unsupported regulator: name=%s type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n", 1152 vreg->desc.name, type, subtype, dig_major_rev); 1153 1154 return -ENODEV; 1155 1156 found: 1157 vreg->logical_type = mapping->logical_type; 1158 vreg->set_points = mapping->set_points; 1159 vreg->hpm_min_load = mapping->hpm_min_load; 1160 vreg->desc.ops = mapping->ops; 1161 1162 if (mapping->set_points) { 1163 if (!mapping->set_points->n_voltages) 1164 spmi_calculate_num_voltages(mapping->set_points); 1165 vreg->desc.n_voltages = mapping->set_points->n_voltages; 1166 } 1167 1168 return 0; 1169 } 1170 1171 static int spmi_regulator_ftsmps_init_slew_rate(struct spmi_regulator *vreg) 1172 { 1173 int ret; 1174 u8 reg = 0; 1175 int step, delay, slew_rate; 1176 const struct spmi_voltage_range *range; 1177 1178 ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_STEP_CTRL, ®, 1); 1179 if (ret) { 1180 dev_err(vreg->dev, "spmi read failed, ret=%d\n", ret); 1181 return ret; 1182 } 1183 1184 range = spmi_regulator_find_range(vreg); 1185 if (!range) 1186 return -EINVAL; 1187 1188 step = reg & SPMI_FTSMPS_STEP_CTRL_STEP_MASK; 1189 step >>= SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT; 1190 1191 delay = reg & SPMI_FTSMPS_STEP_CTRL_DELAY_MASK; 1192 delay >>= SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT; 1193 1194 /* slew_rate has units of uV/us */ 1195 slew_rate = SPMI_FTSMPS_CLOCK_RATE * range->step_uV * (1 << step); 1196 slew_rate /= 1000 * (SPMI_FTSMPS_STEP_DELAY << delay); 1197 slew_rate *= SPMI_FTSMPS_STEP_MARGIN_NUM; 1198 slew_rate /= SPMI_FTSMPS_STEP_MARGIN_DEN; 1199 1200 /* Ensure that the slew rate is greater than 0 */ 1201 vreg->slew_rate = max(slew_rate, 1); 1202 1203 return ret; 1204 } 1205 1206 static unsigned int spmi_regulator_of_map_mode(unsigned int mode) 1207 { 1208 if (mode) 1209 return REGULATOR_MODE_NORMAL; 1210 1211 return REGULATOR_MODE_IDLE; 1212 } 1213 1214 static int spmi_regulator_of_parse(struct device_node *node, 1215 const struct regulator_desc *desc, 1216 struct regulator_config *config) 1217 { 1218 struct spmi_regulator *vreg = config->driver_data; 1219 struct device *dev = config->dev; 1220 int ret; 1221 1222 vreg->ocp_max_retries = SPMI_VS_OCP_DEFAULT_MAX_RETRIES; 1223 vreg->ocp_retry_delay_ms = SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS; 1224 1225 if (vreg->logical_type == SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS) { 1226 ret = spmi_regulator_ftsmps_init_slew_rate(vreg); 1227 if (ret) 1228 return ret; 1229 } 1230 1231 if (vreg->logical_type != SPMI_REGULATOR_LOGICAL_TYPE_VS) 1232 vreg->ocp_irq = 0; 1233 1234 if (vreg->ocp_irq) { 1235 ret = devm_request_irq(dev, vreg->ocp_irq, 1236 spmi_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp", 1237 vreg); 1238 if (ret < 0) { 1239 dev_err(dev, "failed to request irq %d, ret=%d\n", 1240 vreg->ocp_irq, ret); 1241 return ret; 1242 } 1243 1244 INIT_DELAYED_WORK(&vreg->ocp_work, spmi_regulator_vs_ocp_work); 1245 } 1246 1247 return 0; 1248 } 1249 1250 static const struct spmi_regulator_data pm8941_regulators[] = { 1251 { "s1", 0x1400, "vdd_s1", }, 1252 { "s2", 0x1700, "vdd_s2", }, 1253 { "s3", 0x1a00, "vdd_s3", }, 1254 { "l1", 0x4000, "vdd_l1_l3", }, 1255 { "l2", 0x4100, "vdd_l2_lvs_1_2_3", }, 1256 { "l3", 0x4200, "vdd_l1_l3", }, 1257 { "l4", 0x4300, "vdd_l4_l11", }, 1258 { "l5", 0x4400, "vdd_l5_l7", NULL, 0x0410 }, 1259 { "l6", 0x4500, "vdd_l6_l12_l14_l15", }, 1260 { "l7", 0x4600, "vdd_l5_l7", NULL, 0x0410 }, 1261 { "l8", 0x4700, "vdd_l8_l16_l18_19", }, 1262 { "l9", 0x4800, "vdd_l9_l10_l17_l22", }, 1263 { "l10", 0x4900, "vdd_l9_l10_l17_l22", }, 1264 { "l11", 0x4a00, "vdd_l4_l11", }, 1265 { "l12", 0x4b00, "vdd_l6_l12_l14_l15", }, 1266 { "l13", 0x4c00, "vdd_l13_l20_l23_l24", }, 1267 { "l14", 0x4d00, "vdd_l6_l12_l14_l15", }, 1268 { "l15", 0x4e00, "vdd_l6_l12_l14_l15", }, 1269 { "l16", 0x4f00, "vdd_l8_l16_l18_19", }, 1270 { "l17", 0x5000, "vdd_l9_l10_l17_l22", }, 1271 { "l18", 0x5100, "vdd_l8_l16_l18_19", }, 1272 { "l19", 0x5200, "vdd_l8_l16_l18_19", }, 1273 { "l20", 0x5300, "vdd_l13_l20_l23_l24", }, 1274 { "l21", 0x5400, "vdd_l21", }, 1275 { "l22", 0x5500, "vdd_l9_l10_l17_l22", }, 1276 { "l23", 0x5600, "vdd_l13_l20_l23_l24", }, 1277 { "l24", 0x5700, "vdd_l13_l20_l23_l24", }, 1278 { "lvs1", 0x8000, "vdd_l2_lvs_1_2_3", }, 1279 { "lvs2", 0x8100, "vdd_l2_lvs_1_2_3", }, 1280 { "lvs3", 0x8200, "vdd_l2_lvs_1_2_3", }, 1281 { "mvs1", 0x8300, "vin_5vs", }, 1282 { "mvs2", 0x8400, "vin_5vs", }, 1283 { } 1284 }; 1285 1286 static const struct spmi_regulator_data pm8841_regulators[] = { 1287 { "s1", 0x1400, "vdd_s1", }, 1288 { "s2", 0x1700, "vdd_s2", NULL, 0x1c08 }, 1289 { "s3", 0x1a00, "vdd_s3", }, 1290 { "s4", 0x1d00, "vdd_s4", NULL, 0x1c08 }, 1291 { "s5", 0x2000, "vdd_s5", NULL, 0x1c08 }, 1292 { "s6", 0x2300, "vdd_s6", NULL, 0x1c08 }, 1293 { "s7", 0x2600, "vdd_s7", NULL, 0x1c08 }, 1294 { "s8", 0x2900, "vdd_s8", NULL, 0x1c08 }, 1295 { } 1296 }; 1297 1298 static const struct spmi_regulator_data pm8916_regulators[] = { 1299 { "s1", 0x1400, "vdd_s1", }, 1300 { "s2", 0x1700, "vdd_s2", }, 1301 { "s3", 0x1a00, "vdd_s3", }, 1302 { "s4", 0x1d00, "vdd_s4", }, 1303 { "l1", 0x4000, "vdd_l1_l3", }, 1304 { "l2", 0x4100, "vdd_l2", }, 1305 { "l3", 0x4200, "vdd_l1_l3", }, 1306 { "l4", 0x4300, "vdd_l4_l5_l6", }, 1307 { "l5", 0x4400, "vdd_l4_l5_l6", }, 1308 { "l6", 0x4500, "vdd_l4_l5_l6", }, 1309 { "l7", 0x4600, "vdd_l7", }, 1310 { "l8", 0x4700, "vdd_l8_l11_l14_l15_l16", }, 1311 { "l9", 0x4800, "vdd_l9_l10_l12_l13_l17_l18", }, 1312 { "l10", 0x4900, "vdd_l9_l10_l12_l13_l17_l18", }, 1313 { "l11", 0x4a00, "vdd_l8_l11_l14_l15_l16", }, 1314 { "l12", 0x4b00, "vdd_l9_l10_l12_l13_l17_l18", }, 1315 { "l13", 0x4c00, "vdd_l9_l10_l12_l13_l17_l18", }, 1316 { "l14", 0x4d00, "vdd_l8_l11_l14_l15_l16", }, 1317 { "l15", 0x4e00, "vdd_l8_l11_l14_l15_l16", }, 1318 { "l16", 0x4f00, "vdd_l8_l11_l14_l15_l16", }, 1319 { "l17", 0x5000, "vdd_l9_l10_l12_l13_l17_l18", }, 1320 { "l18", 0x5100, "vdd_l9_l10_l12_l13_l17_l18", }, 1321 { } 1322 }; 1323 1324 static const struct of_device_id qcom_spmi_regulator_match[] = { 1325 { .compatible = "qcom,pm8841-regulators", .data = &pm8841_regulators }, 1326 { .compatible = "qcom,pm8916-regulators", .data = &pm8916_regulators }, 1327 { .compatible = "qcom,pm8941-regulators", .data = &pm8941_regulators }, 1328 { } 1329 }; 1330 MODULE_DEVICE_TABLE(of, qcom_spmi_regulator_match); 1331 1332 static int qcom_spmi_regulator_probe(struct platform_device *pdev) 1333 { 1334 const struct spmi_regulator_data *reg; 1335 const struct of_device_id *match; 1336 struct regulator_config config = { }; 1337 struct regulator_dev *rdev; 1338 struct spmi_regulator *vreg; 1339 struct regmap *regmap; 1340 const char *name; 1341 struct device *dev = &pdev->dev; 1342 int ret; 1343 struct list_head *vreg_list; 1344 1345 vreg_list = devm_kzalloc(dev, sizeof(*vreg_list), GFP_KERNEL); 1346 if (!vreg_list) 1347 return -ENOMEM; 1348 INIT_LIST_HEAD(vreg_list); 1349 platform_set_drvdata(pdev, vreg_list); 1350 1351 regmap = dev_get_regmap(dev->parent, NULL); 1352 if (!regmap) 1353 return -ENODEV; 1354 1355 match = of_match_device(qcom_spmi_regulator_match, &pdev->dev); 1356 if (!match) 1357 return -ENODEV; 1358 1359 for (reg = match->data; reg->name; reg++) { 1360 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL); 1361 if (!vreg) 1362 return -ENOMEM; 1363 1364 vreg->dev = dev; 1365 vreg->base = reg->base; 1366 vreg->regmap = regmap; 1367 1368 if (reg->ocp) { 1369 vreg->ocp_irq = platform_get_irq_byname(pdev, reg->ocp); 1370 if (vreg->ocp_irq < 0) { 1371 ret = vreg->ocp_irq; 1372 goto err; 1373 } 1374 } 1375 1376 vreg->desc.id = -1; 1377 vreg->desc.owner = THIS_MODULE; 1378 vreg->desc.type = REGULATOR_VOLTAGE; 1379 vreg->desc.name = name = reg->name; 1380 vreg->desc.supply_name = reg->supply; 1381 vreg->desc.of_match = reg->name; 1382 vreg->desc.of_parse_cb = spmi_regulator_of_parse; 1383 vreg->desc.of_map_mode = spmi_regulator_of_map_mode; 1384 1385 ret = spmi_regulator_match(vreg, reg->force_type); 1386 if (ret) 1387 goto err; 1388 1389 config.dev = dev; 1390 config.driver_data = vreg; 1391 rdev = devm_regulator_register(dev, &vreg->desc, &config); 1392 if (IS_ERR(rdev)) { 1393 dev_err(dev, "failed to register %s\n", name); 1394 ret = PTR_ERR(rdev); 1395 goto err; 1396 } 1397 1398 INIT_LIST_HEAD(&vreg->node); 1399 list_add(&vreg->node, vreg_list); 1400 } 1401 1402 return 0; 1403 1404 err: 1405 list_for_each_entry(vreg, vreg_list, node) 1406 if (vreg->ocp_irq) 1407 cancel_delayed_work_sync(&vreg->ocp_work); 1408 return ret; 1409 } 1410 1411 static int qcom_spmi_regulator_remove(struct platform_device *pdev) 1412 { 1413 struct spmi_regulator *vreg; 1414 struct list_head *vreg_list = platform_get_drvdata(pdev); 1415 1416 list_for_each_entry(vreg, vreg_list, node) 1417 if (vreg->ocp_irq) 1418 cancel_delayed_work_sync(&vreg->ocp_work); 1419 1420 return 0; 1421 } 1422 1423 static struct platform_driver qcom_spmi_regulator_driver = { 1424 .driver = { 1425 .name = "qcom-spmi-regulator", 1426 .of_match_table = qcom_spmi_regulator_match, 1427 }, 1428 .probe = qcom_spmi_regulator_probe, 1429 .remove = qcom_spmi_regulator_remove, 1430 }; 1431 module_platform_driver(qcom_spmi_regulator_driver); 1432 1433 MODULE_DESCRIPTION("Qualcomm SPMI PMIC regulator driver"); 1434 MODULE_LICENSE("GPL v2"); 1435 MODULE_ALIAS("platform:qcom-spmi-regulator"); 1436