David Collins | 7370f1a | 2017-01-18 16:21:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012-2017, 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 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/regmap.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/bitops.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/spmi.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/of.h> |
| 29 | #include <linux/of_device.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/ktime.h> |
| 32 | #include <linux/regulator/driver.h> |
| 33 | #include <linux/regulator/of_regulator.h> |
| 34 | #include <linux/regulator/qpnp-regulator.h> |
| 35 | |
| 36 | /* Debug Flag Definitions */ |
| 37 | enum { |
| 38 | QPNP_VREG_DEBUG_REQUEST = BIT(0), /* Show requests */ |
| 39 | QPNP_VREG_DEBUG_DUPLICATE = BIT(1), /* Show duplicate requests */ |
| 40 | QPNP_VREG_DEBUG_INIT = BIT(2), /* Show state after probe */ |
| 41 | QPNP_VREG_DEBUG_WRITES = BIT(3), /* Show SPMI writes */ |
| 42 | QPNP_VREG_DEBUG_READS = BIT(4), /* Show SPMI reads */ |
| 43 | QPNP_VREG_DEBUG_OCP = BIT(5), /* Show VS OCP IRQ events */ |
| 44 | }; |
| 45 | |
| 46 | static int qpnp_vreg_debug_mask; |
| 47 | module_param_named( |
| 48 | debug_mask, qpnp_vreg_debug_mask, int, 0600 |
| 49 | ); |
| 50 | |
| 51 | #define vreg_err(vreg, fmt, ...) \ |
| 52 | pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__) |
| 53 | |
| 54 | /* These types correspond to unique register layouts. */ |
| 55 | enum qpnp_regulator_logical_type { |
| 56 | QPNP_REGULATOR_LOGICAL_TYPE_SMPS, |
| 57 | QPNP_REGULATOR_LOGICAL_TYPE_LDO, |
| 58 | QPNP_REGULATOR_LOGICAL_TYPE_VS, |
| 59 | QPNP_REGULATOR_LOGICAL_TYPE_BOOST, |
| 60 | QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS, |
| 61 | QPNP_REGULATOR_LOGICAL_TYPE_BOOST_BYP, |
| 62 | QPNP_REGULATOR_LOGICAL_TYPE_LN_LDO, |
| 63 | QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS, |
| 64 | QPNP_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS, |
| 65 | QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO, |
| 66 | QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS2, |
| 67 | }; |
| 68 | |
| 69 | enum qpnp_regulator_type { |
| 70 | QPNP_REGULATOR_TYPE_BUCK = 0x03, |
| 71 | QPNP_REGULATOR_TYPE_LDO = 0x04, |
| 72 | QPNP_REGULATOR_TYPE_VS = 0x05, |
| 73 | QPNP_REGULATOR_TYPE_BOOST = 0x1B, |
| 74 | QPNP_REGULATOR_TYPE_FTS = 0x1C, |
| 75 | QPNP_REGULATOR_TYPE_BOOST_BYP = 0x1F, |
| 76 | QPNP_REGULATOR_TYPE_ULT_LDO = 0x21, |
| 77 | QPNP_REGULATOR_TYPE_ULT_BUCK = 0x22, |
| 78 | }; |
| 79 | |
| 80 | enum qpnp_regulator_subtype { |
| 81 | QPNP_REGULATOR_SUBTYPE_GP_CTL = 0x08, |
| 82 | QPNP_REGULATOR_SUBTYPE_RF_CTL = 0x09, |
| 83 | QPNP_REGULATOR_SUBTYPE_N50 = 0x01, |
| 84 | QPNP_REGULATOR_SUBTYPE_N150 = 0x02, |
| 85 | QPNP_REGULATOR_SUBTYPE_N300 = 0x03, |
| 86 | QPNP_REGULATOR_SUBTYPE_N600 = 0x04, |
| 87 | QPNP_REGULATOR_SUBTYPE_N1200 = 0x05, |
| 88 | QPNP_REGULATOR_SUBTYPE_N600_ST = 0x06, |
| 89 | QPNP_REGULATOR_SUBTYPE_N1200_ST = 0x07, |
| 90 | QPNP_REGULATOR_SUBTYPE_N300_ST = 0x15, |
| 91 | QPNP_REGULATOR_SUBTYPE_P50 = 0x08, |
| 92 | QPNP_REGULATOR_SUBTYPE_P150 = 0x09, |
| 93 | QPNP_REGULATOR_SUBTYPE_P300 = 0x0A, |
| 94 | QPNP_REGULATOR_SUBTYPE_P600 = 0x0B, |
| 95 | QPNP_REGULATOR_SUBTYPE_P1200 = 0x0C, |
| 96 | QPNP_REGULATOR_SUBTYPE_LN = 0x10, |
| 97 | QPNP_REGULATOR_SUBTYPE_LV_P50 = 0x28, |
| 98 | QPNP_REGULATOR_SUBTYPE_LV_P150 = 0x29, |
| 99 | QPNP_REGULATOR_SUBTYPE_LV_P300 = 0x2A, |
| 100 | QPNP_REGULATOR_SUBTYPE_LV_P600 = 0x2B, |
| 101 | QPNP_REGULATOR_SUBTYPE_LV_P1200 = 0x2C, |
| 102 | QPNP_REGULATOR_SUBTYPE_LV100 = 0x01, |
| 103 | QPNP_REGULATOR_SUBTYPE_LV300 = 0x02, |
| 104 | QPNP_REGULATOR_SUBTYPE_MV300 = 0x08, |
| 105 | QPNP_REGULATOR_SUBTYPE_MV500 = 0x09, |
| 106 | QPNP_REGULATOR_SUBTYPE_HDMI = 0x10, |
| 107 | QPNP_REGULATOR_SUBTYPE_OTG = 0x11, |
| 108 | QPNP_REGULATOR_SUBTYPE_5V_BOOST = 0x01, |
| 109 | QPNP_REGULATOR_SUBTYPE_FTS_CTL = 0x08, |
| 110 | QPNP_REGULATOR_SUBTYPE_FTS2p5_CTL = 0x09, |
| 111 | QPNP_REGULATOR_SUBTYPE_FTS426 = 0x0A, |
| 112 | QPNP_REGULATOR_SUBTYPE_BB_2A = 0x01, |
| 113 | QPNP_REGULATOR_SUBTYPE_ULT_HF_CTL1 = 0x0D, |
| 114 | QPNP_REGULATOR_SUBTYPE_ULT_HF_CTL2 = 0x0E, |
| 115 | QPNP_REGULATOR_SUBTYPE_ULT_HF_CTL3 = 0x0F, |
| 116 | QPNP_REGULATOR_SUBTYPE_ULT_HF_CTL4 = 0x10, |
| 117 | }; |
| 118 | |
| 119 | /* First common register layout used by older devices */ |
| 120 | enum qpnp_common_regulator_registers { |
| 121 | QPNP_COMMON_REG_DIG_MAJOR_REV = 0x01, |
| 122 | QPNP_COMMON_REG_TYPE = 0x04, |
| 123 | QPNP_COMMON_REG_SUBTYPE = 0x05, |
| 124 | QPNP_COMMON_REG_VOLTAGE_RANGE = 0x40, |
| 125 | QPNP_COMMON_REG_VOLTAGE_SET = 0x41, |
| 126 | QPNP_COMMON_REG_MODE = 0x45, |
| 127 | QPNP_COMMON_REG_ENABLE = 0x46, |
| 128 | QPNP_COMMON_REG_PULL_DOWN = 0x48, |
| 129 | QPNP_COMMON_REG_STEP_CTRL = 0x61, |
| 130 | }; |
| 131 | |
| 132 | /* |
| 133 | * Second common register layout used by newer devices |
| 134 | * Note that some of the registers from the first common layout remain |
| 135 | * unchanged and their definition is not duplicated. |
| 136 | */ |
| 137 | enum qpnp_common2_regulator_registers { |
| 138 | QPNP_COMMON2_REG_VOLTAGE_LSB = 0x40, |
| 139 | QPNP_COMMON2_REG_VOLTAGE_MSB = 0x41, |
| 140 | QPNP_COMMON2_REG_MODE = 0x45, |
| 141 | QPNP_COMMON2_REG_STEP_CTRL = 0x61, |
| 142 | }; |
| 143 | |
| 144 | enum qpnp_ldo_registers { |
| 145 | QPNP_LDO_REG_SOFT_START = 0x4C, |
| 146 | }; |
| 147 | |
| 148 | enum qpnp_vs_registers { |
| 149 | QPNP_VS_REG_OCP = 0x4A, |
| 150 | QPNP_VS_REG_SOFT_START = 0x4C, |
| 151 | }; |
| 152 | |
| 153 | enum qpnp_boost_registers { |
| 154 | QPNP_BOOST_REG_CURRENT_LIMIT = 0x4A, |
| 155 | }; |
| 156 | |
| 157 | enum qpnp_boost_byp_registers { |
| 158 | QPNP_BOOST_BYP_REG_CURRENT_LIMIT = 0x4B, |
| 159 | }; |
| 160 | |
| 161 | /* Used for indexing into ctrl_reg. These are offets from 0x40 */ |
| 162 | enum qpnp_common_control_register_index { |
| 163 | QPNP_COMMON_IDX_VOLTAGE_RANGE = 0, |
| 164 | QPNP_COMMON_IDX_VOLTAGE_SET = 1, |
| 165 | QPNP_COMMON_IDX_MODE = 5, |
| 166 | QPNP_COMMON_IDX_ENABLE = 6, |
| 167 | }; |
| 168 | |
| 169 | enum qpnp_common2_control_register_index { |
| 170 | QPNP_COMMON2_IDX_VOLTAGE_LSB = 0, |
| 171 | QPNP_COMMON2_IDX_VOLTAGE_MSB = 1, |
| 172 | QPNP_COMMON2_IDX_MODE = 5, |
| 173 | }; |
| 174 | |
| 175 | /* Common regulator control register layout */ |
| 176 | #define QPNP_COMMON_ENABLE_MASK 0x80 |
| 177 | #define QPNP_COMMON_ENABLE 0x80 |
| 178 | #define QPNP_COMMON_DISABLE 0x00 |
| 179 | #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08 |
| 180 | #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04 |
| 181 | #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02 |
| 182 | #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01 |
| 183 | #define QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0F |
| 184 | |
| 185 | /* First common regulator mode register layout */ |
| 186 | #define QPNP_COMMON_MODE_HPM_MASK 0x80 |
| 187 | #define QPNP_COMMON_MODE_AUTO_MASK 0x40 |
| 188 | #define QPNP_COMMON_MODE_BYPASS_MASK 0x20 |
| 189 | #define QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10 |
| 190 | #define QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08 |
| 191 | #define QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04 |
| 192 | #define QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02 |
| 193 | #define QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01 |
| 194 | #define QPNP_COMMON_MODE_FOLLOW_ALL_MASK 0x1F |
| 195 | |
| 196 | /* Second common regulator mode register values */ |
| 197 | #define QPNP_COMMON2_MODE_BYPASS 3 |
| 198 | #define QPNP_COMMON2_MODE_RETENTION 4 |
| 199 | #define QPNP_COMMON2_MODE_LPM 5 |
| 200 | #define QPNP_COMMON2_MODE_AUTO 6 |
| 201 | #define QPNP_COMMON2_MODE_HPM 7 |
| 202 | |
| 203 | #define QPNP_COMMON2_MODE_MASK 0x07 |
| 204 | |
| 205 | /* Common regulator pull down control register layout */ |
| 206 | #define QPNP_COMMON_PULL_DOWN_ENABLE_MASK 0x80 |
| 207 | |
| 208 | /* LDO regulator current limit control register layout */ |
| 209 | #define QPNP_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80 |
| 210 | |
| 211 | /* LDO regulator soft start control register layout */ |
| 212 | #define QPNP_LDO_SOFT_START_ENABLE_MASK 0x80 |
| 213 | |
| 214 | /* VS regulator over current protection control register layout */ |
| 215 | #define QPNP_VS_OCP_OVERRIDE 0x01 |
| 216 | #define QPNP_VS_OCP_NO_OVERRIDE 0x00 |
| 217 | |
| 218 | /* VS regulator soft start control register layout */ |
| 219 | #define QPNP_VS_SOFT_START_ENABLE_MASK 0x80 |
| 220 | #define QPNP_VS_SOFT_START_SEL_MASK 0x03 |
| 221 | |
| 222 | /* Boost regulator current limit control register layout */ |
| 223 | #define QPNP_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80 |
| 224 | #define QPNP_BOOST_CURRENT_LIMIT_MASK 0x07 |
| 225 | |
| 226 | #define QPNP_VS_OCP_DEFAULT_MAX_RETRIES 10 |
| 227 | #define QPNP_VS_OCP_DEFAULT_RETRY_DELAY_MS 30 |
| 228 | #define QPNP_VS_OCP_FALL_DELAY_US 90 |
| 229 | #define QPNP_VS_OCP_FAULT_DELAY_US 20000 |
| 230 | |
| 231 | #define QPNP_FTSMPS_STEP_CTRL_STEP_MASK 0x18 |
| 232 | #define QPNP_FTSMPS_STEP_CTRL_STEP_SHIFT 3 |
| 233 | #define QPNP_FTSMPS_STEP_CTRL_DELAY_MASK 0x07 |
| 234 | #define QPNP_FTSMPS_STEP_CTRL_DELAY_SHIFT 0 |
| 235 | |
| 236 | /* Clock rate in kHz of the FTSMPS regulator reference clock. */ |
| 237 | #define QPNP_FTSMPS_CLOCK_RATE 19200 |
| 238 | |
| 239 | /* Minimum voltage stepper delay for each step. */ |
| 240 | #define QPNP_FTSMPS_STEP_DELAY 8 |
| 241 | |
| 242 | /* |
| 243 | * The ratio QPNP_FTSMPS_STEP_MARGIN_NUM/QPNP_FTSMPS_STEP_MARGIN_DEN is used to |
| 244 | * adjust the step rate in order to account for oscillator variance. |
| 245 | */ |
| 246 | #define QPNP_FTSMPS_STEP_MARGIN_NUM 4 |
| 247 | #define QPNP_FTSMPS_STEP_MARGIN_DEN 5 |
| 248 | |
| 249 | #define QPNP_FTSMPS2_STEP_CTRL_DELAY_MASK 0x03 |
| 250 | #define QPNP_FTSMPS2_STEP_CTRL_DELAY_SHIFT 0 |
| 251 | |
| 252 | /* Clock rate in kHz of the FTSMPS2 regulator reference clock. */ |
| 253 | #define QPNP_FTSMPS2_CLOCK_RATE 4800 |
| 254 | |
| 255 | /* Minimum voltage stepper delay for each step. */ |
| 256 | #define QPNP_FTSMPS2_STEP_DELAY 2 |
| 257 | |
| 258 | /* |
| 259 | * The ratio QPNP_FTSMPS2_STEP_MARGIN_NUM/QPNP_FTSMPS2_STEP_MARGIN_DEN is used |
| 260 | * to adjust the step rate in order to account for oscillator variance. |
| 261 | */ |
| 262 | #define QPNP_FTSMPS2_STEP_MARGIN_NUM 10 |
| 263 | #define QPNP_FTSMPS2_STEP_MARGIN_DEN 11 |
| 264 | |
| 265 | /* |
| 266 | * This voltage in uV is returned by get_voltage functions when there is no way |
| 267 | * to determine the current voltage level. It is needed because the regulator |
| 268 | * framework treats a 0 uV voltage as an error. |
| 269 | */ |
| 270 | #define VOLTAGE_UNKNOWN 1 |
| 271 | |
| 272 | /* VSET value to decide the range of ULT SMPS */ |
| 273 | #define ULT_SMPS_RANGE_SPLIT 0x60 |
| 274 | |
| 275 | /** |
| 276 | * struct qpnp_voltage_range - regulator set point voltage mapping description |
| 277 | * @min_uV: Minimum programmable output voltage resulting from |
| 278 | * set point register value 0x00 |
| 279 | * @max_uV: Maximum programmable output voltage |
| 280 | * @step_uV: Output voltage increase resulting from the set point |
| 281 | * register value increasing by 1 |
| 282 | * @set_point_min_uV: Minimum allowed voltage |
| 283 | * @set_point_max_uV: Maximum allowed voltage. This may be tweaked in order |
| 284 | * to pick which range should be used in the case of |
| 285 | * overlapping set points. |
| 286 | * @n_voltages: Number of preferred voltage set points present in this |
| 287 | * range |
| 288 | * @range_sel: Voltage range register value corresponding to this range |
| 289 | * |
| 290 | * The following relationships must be true for the values used in this struct: |
| 291 | * (max_uV - min_uV) % step_uV == 0 |
| 292 | * (set_point_min_uV - min_uV) % step_uV == 0* |
| 293 | * (set_point_max_uV - min_uV) % step_uV == 0* |
| 294 | * n_voltages = (set_point_max_uV - set_point_min_uV) / step_uV + 1 |
| 295 | * |
| 296 | * *Note, set_point_min_uV == set_point_max_uV == 0 is allowed in order to |
| 297 | * specify that the voltage range has meaning, but is not preferred. |
| 298 | */ |
| 299 | struct qpnp_voltage_range { |
| 300 | int min_uV; |
| 301 | int max_uV; |
| 302 | int step_uV; |
| 303 | int set_point_min_uV; |
| 304 | int set_point_max_uV; |
| 305 | unsigned int n_voltages; |
| 306 | u8 range_sel; |
| 307 | }; |
| 308 | |
| 309 | /* |
| 310 | * The ranges specified in the qpnp_voltage_set_points struct must be listed |
| 311 | * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV. |
| 312 | */ |
| 313 | struct qpnp_voltage_set_points { |
| 314 | struct qpnp_voltage_range *range; |
| 315 | int count; |
| 316 | unsigned int n_voltages; |
| 317 | }; |
| 318 | |
| 319 | struct qpnp_regulator_mapping { |
| 320 | enum qpnp_regulator_type type; |
| 321 | enum qpnp_regulator_subtype subtype; |
| 322 | enum qpnp_regulator_logical_type logical_type; |
| 323 | u32 revision_min; |
| 324 | u32 revision_max; |
| 325 | struct regulator_ops *ops; |
| 326 | struct qpnp_voltage_set_points *set_points; |
| 327 | int hpm_min_load; |
| 328 | }; |
| 329 | |
| 330 | struct qpnp_regulator { |
| 331 | struct regulator_desc rdesc; |
| 332 | struct delayed_work ocp_work; |
| 333 | struct platform_device *pdev; |
| 334 | struct regmap *regmap; |
| 335 | struct regulator_dev *rdev; |
| 336 | struct qpnp_voltage_set_points *set_points; |
| 337 | enum qpnp_regulator_logical_type logical_type; |
| 338 | int enable_time; |
| 339 | int ocp_enable; |
| 340 | int ocp_irq; |
| 341 | int ocp_count; |
| 342 | int ocp_max_retries; |
| 343 | int ocp_retry_delay_ms; |
| 344 | int system_load; |
| 345 | int hpm_min_load; |
| 346 | int slew_rate; |
| 347 | u32 write_count; |
| 348 | u32 prev_write_count; |
| 349 | ktime_t vs_enable_time; |
| 350 | u16 base_addr; |
| 351 | /* ctrl_reg provides a shadow copy of register values 0x40 to 0x47. */ |
| 352 | u8 ctrl_reg[8]; |
| 353 | u8 init_mode; |
| 354 | }; |
| 355 | |
| 356 | #define QPNP_VREG_MAP(_type, _subtype, _dig_major_min, _dig_major_max, \ |
| 357 | _logical_type, _ops_val, _set_points_val, _hpm_min_load) \ |
| 358 | { \ |
| 359 | .type = QPNP_REGULATOR_TYPE_##_type, \ |
| 360 | .subtype = QPNP_REGULATOR_SUBTYPE_##_subtype, \ |
| 361 | .revision_min = _dig_major_min, \ |
| 362 | .revision_max = _dig_major_max, \ |
| 363 | .logical_type = QPNP_REGULATOR_LOGICAL_TYPE_##_logical_type, \ |
| 364 | .ops = &qpnp_##_ops_val##_ops, \ |
| 365 | .set_points = &_set_points_val##_set_points, \ |
| 366 | .hpm_min_load = _hpm_min_load, \ |
| 367 | } |
| 368 | |
| 369 | #define VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, \ |
| 370 | _set_point_max_uV, _max_uV, _step_uV) \ |
| 371 | { \ |
| 372 | .min_uV = _min_uV, \ |
| 373 | .max_uV = _max_uV, \ |
| 374 | .set_point_min_uV = _set_point_min_uV, \ |
| 375 | .set_point_max_uV = _set_point_max_uV, \ |
| 376 | .step_uV = _step_uV, \ |
| 377 | .range_sel = _range_sel, \ |
| 378 | } |
| 379 | |
| 380 | #define SET_POINTS(_ranges) \ |
| 381 | { \ |
| 382 | .range = _ranges, \ |
| 383 | .count = ARRAY_SIZE(_ranges), \ |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * These tables contain the physically available PMIC regulator voltage setpoint |
| 388 | * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed |
| 389 | * to ensure that the setpoints available to software are monotonically |
| 390 | * increasing and unique. The set_voltage callback functions expect these |
| 391 | * properties to hold. |
| 392 | */ |
| 393 | static struct qpnp_voltage_range pldo_ranges[] = { |
| 394 | VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500), |
| 395 | VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000), |
| 396 | VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000), |
| 397 | }; |
| 398 | |
| 399 | static struct qpnp_voltage_range nldo1_ranges[] = { |
| 400 | VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500), |
| 401 | }; |
| 402 | |
| 403 | static struct qpnp_voltage_range nldo2_ranges[] = { |
| 404 | VOLTAGE_RANGE(0, 375000, 0, 0, 1537500, 12500), |
| 405 | VOLTAGE_RANGE(1, 375000, 375000, 768750, 768750, 6250), |
| 406 | VOLTAGE_RANGE(2, 750000, 775000, 1537500, 1537500, 12500), |
| 407 | }; |
| 408 | |
| 409 | static struct qpnp_voltage_range nldo3_ranges[] = { |
| 410 | VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500), |
| 411 | VOLTAGE_RANGE(1, 375000, 0, 0, 1537500, 12500), |
| 412 | VOLTAGE_RANGE(2, 750000, 0, 0, 1537500, 12500), |
| 413 | }; |
| 414 | |
| 415 | static struct qpnp_voltage_range ln_ldo_ranges[] = { |
| 416 | VOLTAGE_RANGE(1, 690000, 690000, 1110000, 1110000, 60000), |
| 417 | VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000), |
| 418 | }; |
| 419 | |
| 420 | static struct qpnp_voltage_range smps_ranges[] = { |
| 421 | VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500), |
| 422 | VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000), |
| 423 | }; |
| 424 | |
| 425 | static struct qpnp_voltage_range ftsmps_ranges[] = { |
| 426 | VOLTAGE_RANGE(0, 0, 350000, 1275000, 1275000, 5000), |
| 427 | VOLTAGE_RANGE(1, 0, 1280000, 2040000, 2040000, 10000), |
| 428 | }; |
| 429 | |
| 430 | static struct qpnp_voltage_range ftsmps2p5_ranges[] = { |
| 431 | VOLTAGE_RANGE(0, 80000, 350000, 1355000, 1355000, 5000), |
| 432 | VOLTAGE_RANGE(1, 160000, 1360000, 2200000, 2200000, 10000), |
| 433 | }; |
| 434 | |
| 435 | static struct qpnp_voltage_range boost_ranges[] = { |
| 436 | VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000), |
| 437 | }; |
| 438 | |
| 439 | static struct qpnp_voltage_range boost_byp_ranges[] = { |
| 440 | VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000), |
| 441 | }; |
| 442 | |
| 443 | static struct qpnp_voltage_range ult_lo_smps_ranges[] = { |
| 444 | VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500), |
| 445 | VOLTAGE_RANGE(1, 750000, 0, 0, 1525000, 25000), |
| 446 | }; |
| 447 | |
| 448 | static struct qpnp_voltage_range ult_ho_smps_ranges[] = { |
| 449 | VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000), |
| 450 | }; |
| 451 | |
| 452 | static struct qpnp_voltage_range ult_nldo_ranges[] = { |
| 453 | VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500), |
| 454 | }; |
| 455 | |
| 456 | static struct qpnp_voltage_range ult_pldo_ranges[] = { |
| 457 | VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500), |
| 458 | }; |
| 459 | |
| 460 | static struct qpnp_voltage_range ftsmps426_ranges[] = { |
| 461 | VOLTAGE_RANGE(0, 0, 320000, 1352000, 1352000, 4000), |
| 462 | }; |
| 463 | |
| 464 | static struct qpnp_voltage_set_points pldo_set_points = SET_POINTS(pldo_ranges); |
| 465 | static struct qpnp_voltage_set_points nldo1_set_points |
| 466 | = SET_POINTS(nldo1_ranges); |
| 467 | static struct qpnp_voltage_set_points nldo2_set_points |
| 468 | = SET_POINTS(nldo2_ranges); |
| 469 | static struct qpnp_voltage_set_points nldo3_set_points |
| 470 | = SET_POINTS(nldo3_ranges); |
| 471 | static struct qpnp_voltage_set_points ln_ldo_set_points |
| 472 | = SET_POINTS(ln_ldo_ranges); |
| 473 | static struct qpnp_voltage_set_points smps_set_points = SET_POINTS(smps_ranges); |
| 474 | static struct qpnp_voltage_set_points ftsmps_set_points |
| 475 | = SET_POINTS(ftsmps_ranges); |
| 476 | static struct qpnp_voltage_set_points ftsmps2p5_set_points |
| 477 | = SET_POINTS(ftsmps2p5_ranges); |
| 478 | static struct qpnp_voltage_set_points boost_set_points |
| 479 | = SET_POINTS(boost_ranges); |
| 480 | static struct qpnp_voltage_set_points boost_byp_set_points |
| 481 | = SET_POINTS(boost_byp_ranges); |
| 482 | static struct qpnp_voltage_set_points ult_lo_smps_set_points |
| 483 | = SET_POINTS(ult_lo_smps_ranges); |
| 484 | static struct qpnp_voltage_set_points ult_ho_smps_set_points |
| 485 | = SET_POINTS(ult_ho_smps_ranges); |
| 486 | static struct qpnp_voltage_set_points ult_nldo_set_points |
| 487 | = SET_POINTS(ult_nldo_ranges); |
| 488 | static struct qpnp_voltage_set_points ult_pldo_set_points |
| 489 | = SET_POINTS(ult_pldo_ranges); |
| 490 | static struct qpnp_voltage_set_points ftsmps426_set_points |
| 491 | = SET_POINTS(ftsmps426_ranges); |
| 492 | static struct qpnp_voltage_set_points none_set_points; |
| 493 | |
| 494 | static struct qpnp_voltage_set_points *all_set_points[] = { |
| 495 | &pldo_set_points, |
| 496 | &nldo1_set_points, |
| 497 | &nldo2_set_points, |
| 498 | &nldo3_set_points, |
| 499 | &ln_ldo_set_points, |
| 500 | &smps_set_points, |
| 501 | &ftsmps_set_points, |
| 502 | &ftsmps2p5_set_points, |
| 503 | &boost_set_points, |
| 504 | &boost_byp_set_points, |
| 505 | &ult_lo_smps_set_points, |
| 506 | &ult_ho_smps_set_points, |
| 507 | &ult_nldo_set_points, |
| 508 | &ult_pldo_set_points, |
| 509 | &ftsmps426_set_points, |
| 510 | }; |
| 511 | |
| 512 | /* Determines which label to add to a debug print statement. */ |
| 513 | enum qpnp_regulator_action { |
| 514 | QPNP_REGULATOR_ACTION_INIT, |
| 515 | QPNP_REGULATOR_ACTION_ENABLE, |
| 516 | QPNP_REGULATOR_ACTION_DISABLE, |
| 517 | QPNP_REGULATOR_ACTION_VOLTAGE, |
| 518 | QPNP_REGULATOR_ACTION_MODE, |
| 519 | }; |
| 520 | |
| 521 | static void qpnp_vreg_show_state(struct regulator_dev *rdev, |
| 522 | enum qpnp_regulator_action action); |
| 523 | |
| 524 | #define DEBUG_PRINT_BUFFER_SIZE 64 |
| 525 | static void fill_string(char *str, size_t str_len, u8 *buf, int buf_len) |
| 526 | { |
| 527 | int pos = 0; |
| 528 | int i; |
| 529 | |
| 530 | for (i = 0; i < buf_len; i++) { |
| 531 | pos += scnprintf(str + pos, str_len - pos, "0x%02X", buf[i]); |
| 532 | if (i < buf_len - 1) |
| 533 | pos += scnprintf(str + pos, str_len - pos, ", "); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | static inline int qpnp_vreg_read(struct qpnp_regulator *vreg, u16 addr, u8 *buf, |
| 538 | int len) |
| 539 | { |
| 540 | char str[DEBUG_PRINT_BUFFER_SIZE]; |
| 541 | int rc = 0; |
| 542 | |
| 543 | rc = regmap_bulk_read(vreg->regmap, vreg->base_addr + addr, buf, len); |
| 544 | |
| 545 | if (!rc && (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_READS)) { |
| 546 | str[0] = '\0'; |
| 547 | fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len); |
| 548 | pr_info(" %-11s: read(0x%04X), sid=%d, len=%d; %s\n", |
| 549 | vreg->rdesc.name, vreg->base_addr + addr, |
| 550 | to_spmi_device(vreg->pdev->dev.parent)->usid, len, |
| 551 | str); |
| 552 | } |
| 553 | |
| 554 | return rc; |
| 555 | } |
| 556 | |
| 557 | static inline int qpnp_vreg_write(struct qpnp_regulator *vreg, u16 addr, |
| 558 | u8 *buf, int len) |
| 559 | { |
| 560 | char str[DEBUG_PRINT_BUFFER_SIZE]; |
| 561 | int rc = 0; |
| 562 | |
| 563 | if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_WRITES) { |
| 564 | str[0] = '\0'; |
| 565 | fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len); |
| 566 | pr_info("%-11s: write(0x%04X), sid=%d, len=%d; %s\n", |
| 567 | vreg->rdesc.name, vreg->base_addr + addr, |
| 568 | to_spmi_device(vreg->pdev->dev.parent)->usid, len, |
| 569 | str); |
| 570 | } |
| 571 | |
| 572 | rc = regmap_bulk_write(vreg->regmap, vreg->base_addr + addr, buf, len); |
| 573 | if (!rc) |
| 574 | vreg->write_count += len; |
| 575 | |
| 576 | return rc; |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * qpnp_vreg_write_optimized - write the minimum sized contiguous subset of buf |
| 581 | * @vreg: qpnp_regulator pointer for this regulator |
| 582 | * @addr: local SPMI address offset from this peripheral's base address |
| 583 | * @buf: new data to write into the SPMI registers |
| 584 | * @buf_save: old data in the registers |
| 585 | * @len: number of bytes to write |
| 586 | * |
| 587 | * This function checks for unchanged register values between buf and buf_save |
| 588 | * starting at both ends of buf. Only the contiguous subset in the middle of |
| 589 | * buf starting and ending with new values is sent. |
| 590 | * |
| 591 | * Consider the following example: |
| 592 | * buf offset: 0 1 2 3 4 5 6 7 |
| 593 | * reg state: U U C C U C U U |
| 594 | * (U = unchanged, C = changed) |
| 595 | * In this example registers 2 through 5 will be written with a single |
| 596 | * transaction. |
| 597 | */ |
| 598 | static inline int qpnp_vreg_write_optimized(struct qpnp_regulator *vreg, |
| 599 | u16 addr, u8 *buf, u8 *buf_save, int len) |
| 600 | { |
| 601 | int i, rc, start, end; |
| 602 | |
| 603 | for (i = 0; i < len; i++) |
| 604 | if (buf[i] != buf_save[i]) |
| 605 | break; |
| 606 | start = i; |
| 607 | |
| 608 | for (i = len - 1; i >= 0; i--) |
| 609 | if (buf[i] != buf_save[i]) |
| 610 | break; |
| 611 | end = i; |
| 612 | |
| 613 | if (start > end) { |
| 614 | /* No modified register values present. */ |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | rc = qpnp_vreg_write(vreg, addr + start, &buf[start], end - start + 1); |
| 619 | if (!rc) |
| 620 | for (i = start; i <= end; i++) |
| 621 | buf_save[i] = buf[i]; |
| 622 | |
| 623 | return rc; |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Perform a masked write to a PMIC register only if the new value differs |
| 628 | * from the last value written to the register. This removes redundant |
| 629 | * register writing. |
| 630 | */ |
| 631 | static int qpnp_vreg_masked_write(struct qpnp_regulator *vreg, u16 addr, u8 val, |
| 632 | u8 mask, u8 *reg_save) |
| 633 | { |
| 634 | int rc = 0; |
| 635 | u8 reg; |
| 636 | |
| 637 | reg = (*reg_save & ~mask) | (val & mask); |
| 638 | if (reg != *reg_save) { |
| 639 | rc = qpnp_vreg_write(vreg, addr, ®, 1); |
| 640 | |
| 641 | if (rc) { |
| 642 | vreg_err(vreg, "write failed; addr=0x%03X, rc=%d\n", |
| 643 | addr, rc); |
| 644 | } else { |
| 645 | *reg_save = reg; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | return rc; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Perform a masked read-modify-write to a PMIC register only if the new value |
| 654 | * differs from the value currently in the register. This removes redundant |
| 655 | * register writing. |
| 656 | */ |
| 657 | static int qpnp_vreg_masked_read_write(struct qpnp_regulator *vreg, u16 addr, |
| 658 | u8 val, u8 mask) |
| 659 | { |
| 660 | int rc; |
| 661 | u8 reg; |
| 662 | |
| 663 | rc = qpnp_vreg_read(vreg, addr, ®, 1); |
| 664 | if (rc) { |
| 665 | vreg_err(vreg, "read failed; addr=0x%03X, rc=%d\n", addr, rc); |
| 666 | return rc; |
| 667 | } |
| 668 | |
| 669 | return qpnp_vreg_masked_write(vreg, addr, val, mask, ®); |
| 670 | } |
| 671 | |
| 672 | static int qpnp_regulator_common_is_enabled(struct regulator_dev *rdev) |
| 673 | { |
| 674 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 675 | |
| 676 | return (vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE] |
| 677 | & QPNP_COMMON_ENABLE_MASK) |
| 678 | == QPNP_COMMON_ENABLE; |
| 679 | } |
| 680 | |
| 681 | static int qpnp_regulator_common_enable(struct regulator_dev *rdev) |
| 682 | { |
| 683 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 684 | int rc; |
| 685 | |
| 686 | rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE, |
| 687 | QPNP_COMMON_ENABLE, QPNP_COMMON_ENABLE_MASK, |
| 688 | &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]); |
| 689 | |
| 690 | if (rc) |
| 691 | vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc); |
| 692 | else |
| 693 | qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_ENABLE); |
| 694 | |
| 695 | return rc; |
| 696 | } |
| 697 | |
| 698 | static int qpnp_regulator_vs_enable(struct regulator_dev *rdev) |
| 699 | { |
| 700 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 701 | |
| 702 | if (vreg->ocp_irq) { |
| 703 | vreg->ocp_count = 0; |
| 704 | vreg->vs_enable_time = ktime_get(); |
| 705 | } |
| 706 | |
| 707 | return qpnp_regulator_common_enable(rdev); |
| 708 | } |
| 709 | |
| 710 | static int qpnp_regulator_common_disable(struct regulator_dev *rdev) |
| 711 | { |
| 712 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 713 | int rc; |
| 714 | |
| 715 | rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE, |
| 716 | QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK, |
| 717 | &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]); |
| 718 | |
| 719 | if (rc) |
| 720 | vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc); |
| 721 | else |
| 722 | qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_DISABLE); |
| 723 | |
| 724 | return rc; |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * Returns 1 if the voltage can be set in the current range, 0 if the voltage |
| 729 | * cannot be set in the current range, or errno if an error occurred. |
| 730 | */ |
| 731 | static int qpnp_regulator_select_voltage_same_range(struct qpnp_regulator *vreg, |
| 732 | int min_uV, int max_uV, int *range_sel, int *voltage_sel, |
| 733 | unsigned int *selector) |
| 734 | { |
| 735 | struct qpnp_voltage_range *range = NULL; |
| 736 | int uV = min_uV; |
| 737 | int i; |
| 738 | |
| 739 | *range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE]; |
| 740 | |
| 741 | for (i = 0; i < vreg->set_points->count; i++) { |
| 742 | if (vreg->set_points->range[i].range_sel == *range_sel) { |
| 743 | range = &vreg->set_points->range[i]; |
| 744 | break; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | if (!range) { |
| 749 | /* Unknown range */ |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | if (uV < range->min_uV && max_uV >= range->min_uV) |
| 754 | uV = range->min_uV; |
| 755 | |
| 756 | if (uV < range->min_uV || uV > range->max_uV) { |
| 757 | /* Current range doesn't support the requested voltage. */ |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Force uV to be an allowed set point by applying a ceiling function to |
| 763 | * the uV value. |
| 764 | */ |
| 765 | *voltage_sel = DIV_ROUND_UP(uV - range->min_uV, range->step_uV); |
| 766 | uV = *voltage_sel * range->step_uV + range->min_uV; |
| 767 | |
| 768 | if (uV > max_uV) { |
| 769 | /* |
| 770 | * No set point in the current voltage range is within the |
| 771 | * requested min_uV to max_uV range. |
| 772 | */ |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | *selector = 0; |
| 777 | for (i = 0; i < vreg->set_points->count; i++) { |
| 778 | if (uV >= vreg->set_points->range[i].set_point_min_uV |
| 779 | && uV <= vreg->set_points->range[i].set_point_max_uV) { |
| 780 | *selector += |
| 781 | (uV - vreg->set_points->range[i].set_point_min_uV) |
| 782 | / vreg->set_points->range[i].step_uV; |
| 783 | break; |
| 784 | } |
| 785 | |
| 786 | *selector += vreg->set_points->range[i].n_voltages; |
| 787 | } |
| 788 | |
| 789 | if (*selector >= vreg->set_points->n_voltages) |
| 790 | return 0; |
| 791 | |
| 792 | return 1; |
| 793 | } |
| 794 | |
| 795 | static int qpnp_regulator_select_voltage(struct qpnp_regulator *vreg, |
| 796 | int min_uV, int max_uV, int *range_sel, int *voltage_sel, |
| 797 | unsigned int *selector) |
| 798 | { |
| 799 | struct qpnp_voltage_range *range; |
| 800 | int uV = min_uV; |
| 801 | int lim_min_uV, lim_max_uV, i, range_id, range_max_uV; |
| 802 | |
| 803 | /* Check if request voltage is outside of physically settable range. */ |
| 804 | lim_min_uV = vreg->set_points->range[0].set_point_min_uV; |
| 805 | lim_max_uV = |
| 806 | vreg->set_points->range[vreg->set_points->count - 1].set_point_max_uV; |
| 807 | |
| 808 | if (uV < lim_min_uV && max_uV >= lim_min_uV) |
| 809 | uV = lim_min_uV; |
| 810 | |
| 811 | if (uV < lim_min_uV || uV > lim_max_uV) { |
| 812 | vreg_err(vreg, |
| 813 | "request v=[%d, %d] is outside possible v=[%d, %d]\n", |
| 814 | min_uV, max_uV, lim_min_uV, lim_max_uV); |
| 815 | return -EINVAL; |
| 816 | } |
| 817 | |
| 818 | /* Find the range which uV is inside of. */ |
| 819 | for (i = vreg->set_points->count - 1; i > 0; i--) { |
| 820 | range_max_uV = vreg->set_points->range[i - 1].set_point_max_uV; |
| 821 | if (uV > range_max_uV && range_max_uV > 0) |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | range_id = i; |
| 826 | range = &vreg->set_points->range[range_id]; |
| 827 | *range_sel = range->range_sel; |
| 828 | |
| 829 | /* |
| 830 | * Force uV to be an allowed set point by applying a ceiling function to |
| 831 | * the uV value. |
| 832 | */ |
| 833 | *voltage_sel = (uV - range->min_uV + range->step_uV - 1) |
| 834 | / range->step_uV; |
| 835 | uV = *voltage_sel * range->step_uV + range->min_uV; |
| 836 | |
| 837 | if (uV > max_uV) { |
| 838 | vreg_err(vreg, |
| 839 | "request v=[%d, %d] cannot be met by any set point; " |
| 840 | "next set point: %d\n", |
| 841 | min_uV, max_uV, uV); |
| 842 | return -EINVAL; |
| 843 | } |
| 844 | |
| 845 | *selector = 0; |
| 846 | for (i = 0; i < range_id; i++) |
| 847 | *selector += vreg->set_points->range[i].n_voltages; |
| 848 | *selector += (uV - range->set_point_min_uV) / range->step_uV; |
| 849 | |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | static int qpnp_regulator_delay_for_slewing(struct qpnp_regulator *vreg, |
| 854 | int prev_voltage) |
| 855 | { |
| 856 | int current_voltage; |
| 857 | |
| 858 | /* Delay for voltage slewing if a step rate is specified. */ |
| 859 | if (vreg->slew_rate && vreg->rdesc.ops->get_voltage) { |
| 860 | current_voltage = vreg->rdesc.ops->get_voltage(vreg->rdev); |
| 861 | if (current_voltage < 0) { |
| 862 | vreg_err(vreg, "could not get new voltage, rc=%d\n", |
| 863 | current_voltage); |
| 864 | return current_voltage; |
| 865 | } |
| 866 | |
| 867 | udelay(DIV_ROUND_UP(abs(current_voltage - prev_voltage), |
| 868 | vreg->slew_rate)); |
| 869 | } |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static int qpnp_regulator_common_set_voltage(struct regulator_dev *rdev, |
| 875 | int min_uV, int max_uV, unsigned int *selector) |
| 876 | { |
| 877 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 878 | int rc, range_sel, voltage_sel, voltage_old = 0; |
| 879 | u8 buf[2]; |
| 880 | |
| 881 | if (vreg->slew_rate && vreg->rdesc.ops->get_voltage) { |
| 882 | voltage_old = vreg->rdesc.ops->get_voltage(rdev); |
| 883 | if (voltage_old < 0) { |
| 884 | vreg_err(vreg, "could not get current voltage, rc=%d\n", |
| 885 | voltage_old); |
| 886 | return voltage_old; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Favor staying in the current voltage range if possible. This avoids |
| 892 | * voltage spikes that occur when changing the voltage range. |
| 893 | */ |
| 894 | rc = qpnp_regulator_select_voltage_same_range(vreg, min_uV, max_uV, |
| 895 | &range_sel, &voltage_sel, selector); |
| 896 | if (rc == 0) |
| 897 | rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, |
| 898 | &range_sel, &voltage_sel, selector); |
| 899 | if (rc < 0) { |
| 900 | vreg_err(vreg, "could not set voltage, rc=%d\n", rc); |
| 901 | return rc; |
| 902 | } |
| 903 | |
| 904 | buf[0] = range_sel; |
| 905 | buf[1] = voltage_sel; |
| 906 | if ((vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] != range_sel) |
| 907 | && (vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] == voltage_sel)) { |
| 908 | /* Handle latched range change. */ |
| 909 | rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE, |
| 910 | buf, 2); |
| 911 | if (!rc) { |
| 912 | vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] = buf[0]; |
| 913 | vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] = buf[1]; |
| 914 | } |
| 915 | } else { |
| 916 | /* Either write can be optimized away safely. */ |
| 917 | rc = qpnp_vreg_write_optimized(vreg, |
| 918 | QPNP_COMMON_REG_VOLTAGE_RANGE, buf, |
| 919 | &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE], 2); |
| 920 | } |
| 921 | |
| 922 | if (rc) { |
| 923 | vreg_err(vreg, "SPMI write failed, rc=%d\n", rc); |
| 924 | } else { |
| 925 | rc = qpnp_regulator_delay_for_slewing(vreg, voltage_old); |
| 926 | if (rc) |
| 927 | return rc; |
| 928 | |
| 929 | qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE); |
| 930 | } |
| 931 | |
| 932 | return rc; |
| 933 | } |
| 934 | |
| 935 | static int qpnp_regulator_common_get_voltage(struct regulator_dev *rdev) |
| 936 | { |
| 937 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 938 | struct qpnp_voltage_range *range = NULL; |
| 939 | int range_sel, voltage_sel, i; |
| 940 | |
| 941 | range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE]; |
| 942 | voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]; |
| 943 | |
| 944 | for (i = 0; i < vreg->set_points->count; i++) { |
| 945 | if (vreg->set_points->range[i].range_sel == range_sel) { |
| 946 | range = &vreg->set_points->range[i]; |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | if (!range) { |
| 952 | vreg_err(vreg, "voltage unknown, range %d is invalid\n", |
| 953 | range_sel); |
| 954 | return VOLTAGE_UNKNOWN; |
| 955 | } |
| 956 | |
| 957 | return range->step_uV * voltage_sel + range->min_uV; |
| 958 | } |
| 959 | |
| 960 | static int qpnp_regulator_single_range_set_voltage(struct regulator_dev *rdev, |
| 961 | int min_uV, int max_uV, unsigned int *selector) |
| 962 | { |
| 963 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 964 | int rc, range_sel, voltage_sel; |
| 965 | |
| 966 | rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel, |
| 967 | &voltage_sel, selector); |
| 968 | if (rc) { |
| 969 | vreg_err(vreg, "could not set voltage, rc=%d\n", rc); |
| 970 | return rc; |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * Certain types of regulators do not have a range select register so |
| 975 | * only voltage set register needs to be written. |
| 976 | */ |
| 977 | rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_VOLTAGE_SET, |
| 978 | voltage_sel, 0xFF, &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]); |
| 979 | |
| 980 | if (rc) |
| 981 | vreg_err(vreg, "SPMI write failed, rc=%d\n", rc); |
| 982 | else |
| 983 | qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE); |
| 984 | |
| 985 | return rc; |
| 986 | } |
| 987 | |
| 988 | static int qpnp_regulator_single_range_get_voltage(struct regulator_dev *rdev) |
| 989 | { |
| 990 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 991 | struct qpnp_voltage_range *range = &vreg->set_points->range[0]; |
| 992 | int voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]; |
| 993 | |
| 994 | return range->step_uV * voltage_sel + range->min_uV; |
| 995 | } |
| 996 | |
| 997 | static int qpnp_regulator_ult_lo_smps_set_voltage(struct regulator_dev *rdev, |
| 998 | int min_uV, int max_uV, unsigned int *selector) |
| 999 | { |
| 1000 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1001 | int rc, range_sel, voltage_sel; |
| 1002 | |
| 1003 | /* |
| 1004 | * Favor staying in the current voltage range if possible. This avoids |
| 1005 | * voltage spikes that occur when changing the voltage range. |
| 1006 | */ |
| 1007 | rc = qpnp_regulator_select_voltage_same_range(vreg, min_uV, max_uV, |
| 1008 | &range_sel, &voltage_sel, selector); |
| 1009 | if (rc == 0) |
| 1010 | rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, |
| 1011 | &range_sel, &voltage_sel, selector); |
| 1012 | if (rc < 0) { |
| 1013 | vreg_err(vreg, "could not set voltage, rc=%d\n", rc); |
| 1014 | return rc; |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * Calculate VSET based on range |
| 1019 | * In case of range 0: voltage_sel is a 7 bit value, can be written |
| 1020 | * witout any modification. |
| 1021 | * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to |
| 1022 | * [011]. |
| 1023 | */ |
| 1024 | if (range_sel == 1) |
| 1025 | voltage_sel |= ULT_SMPS_RANGE_SPLIT; |
| 1026 | |
| 1027 | rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_VOLTAGE_SET, |
| 1028 | voltage_sel, 0xFF, &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]); |
| 1029 | if (rc) { |
| 1030 | vreg_err(vreg, "SPMI write failed, rc=%d\n", rc); |
| 1031 | } else { |
| 1032 | vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] = range_sel; |
| 1033 | qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE); |
| 1034 | } |
| 1035 | |
| 1036 | return rc; |
| 1037 | } |
| 1038 | |
| 1039 | static int qpnp_regulator_ult_lo_smps_get_voltage(struct regulator_dev *rdev) |
| 1040 | { |
| 1041 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1042 | struct qpnp_voltage_range *range = NULL; |
| 1043 | int range_sel, voltage_sel, i; |
| 1044 | |
| 1045 | range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE]; |
| 1046 | voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]; |
| 1047 | |
| 1048 | for (i = 0; i < vreg->set_points->count; i++) { |
| 1049 | if (vreg->set_points->range[i].range_sel == range_sel) { |
| 1050 | range = &vreg->set_points->range[i]; |
| 1051 | break; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | if (!range) { |
| 1056 | vreg_err(vreg, "voltage unknown, range %d is invalid\n", |
| 1057 | range_sel); |
| 1058 | return VOLTAGE_UNKNOWN; |
| 1059 | } |
| 1060 | |
| 1061 | if (range_sel == 1) |
| 1062 | voltage_sel &= ~ULT_SMPS_RANGE_SPLIT; |
| 1063 | |
| 1064 | return range->step_uV * voltage_sel + range->min_uV; |
| 1065 | } |
| 1066 | |
| 1067 | static int qpnp_regulator_common_list_voltage(struct regulator_dev *rdev, |
| 1068 | unsigned int selector) |
| 1069 | { |
| 1070 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1071 | int uV = 0; |
| 1072 | int i; |
| 1073 | |
| 1074 | if (selector >= vreg->set_points->n_voltages) |
| 1075 | return 0; |
| 1076 | |
| 1077 | for (i = 0; i < vreg->set_points->count; i++) { |
| 1078 | if (selector < vreg->set_points->range[i].n_voltages) { |
| 1079 | uV = selector * vreg->set_points->range[i].step_uV |
| 1080 | + vreg->set_points->range[i].set_point_min_uV; |
| 1081 | break; |
| 1082 | } |
| 1083 | |
| 1084 | selector -= vreg->set_points->range[i].n_voltages; |
| 1085 | } |
| 1086 | |
| 1087 | return uV; |
| 1088 | } |
| 1089 | |
| 1090 | static int qpnp_regulator_common2_set_voltage(struct regulator_dev *rdev, |
| 1091 | int min_uV, int max_uV, unsigned int *selector) |
| 1092 | { |
| 1093 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1094 | int rc, range_sel, voltage_sel, voltage_old = 0; |
| 1095 | int voltage_uV, voltage_mV; |
| 1096 | u8 buf[2]; |
| 1097 | |
| 1098 | if (vreg->slew_rate && vreg->rdesc.ops->get_voltage) { |
| 1099 | voltage_old = vreg->rdesc.ops->get_voltage(rdev); |
| 1100 | if (voltage_old < 0) { |
| 1101 | vreg_err(vreg, "could not get current voltage, rc=%d\n", |
| 1102 | voltage_old); |
| 1103 | return voltage_old; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel, |
| 1108 | &voltage_sel, selector); |
| 1109 | if (rc < 0) { |
| 1110 | vreg_err(vreg, "could not set voltage, rc=%d\n", rc); |
| 1111 | return rc; |
| 1112 | } |
| 1113 | |
| 1114 | voltage_uV = qpnp_regulator_common_list_voltage(rdev, *selector); |
| 1115 | voltage_mV = voltage_uV / 1000; |
| 1116 | buf[0] = voltage_mV & 0xFF; |
| 1117 | buf[1] = (voltage_mV >> 8) & 0xFF; |
| 1118 | |
| 1119 | if (vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_LSB] != buf[0] |
| 1120 | || vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_MSB] != buf[1]) { |
| 1121 | /* MSB must always be written even if it is unchanged. */ |
| 1122 | rc = qpnp_vreg_write(vreg, QPNP_COMMON2_REG_VOLTAGE_LSB, |
| 1123 | buf, 2); |
| 1124 | if (rc) { |
| 1125 | vreg_err(vreg, "SPMI write failed, rc=%d\n", rc); |
| 1126 | return rc; |
| 1127 | } |
| 1128 | |
| 1129 | vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_LSB] = buf[0]; |
| 1130 | vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_MSB] = buf[1]; |
| 1131 | |
| 1132 | rc = qpnp_regulator_delay_for_slewing(vreg, voltage_old); |
| 1133 | if (rc) |
| 1134 | return rc; |
| 1135 | |
| 1136 | qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE); |
| 1137 | } |
| 1138 | |
| 1139 | return rc; |
| 1140 | } |
| 1141 | |
| 1142 | static int qpnp_regulator_common2_get_voltage(struct regulator_dev *rdev) |
| 1143 | { |
| 1144 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1145 | |
| 1146 | return (((int)vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_MSB] << 8) |
| 1147 | | (int)vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_LSB]) * 1000; |
| 1148 | } |
| 1149 | |
| 1150 | static unsigned int qpnp_regulator_common_get_mode(struct regulator_dev *rdev) |
| 1151 | { |
| 1152 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1153 | |
| 1154 | return (vreg->ctrl_reg[QPNP_COMMON_IDX_MODE] |
| 1155 | & QPNP_COMMON_MODE_HPM_MASK) |
| 1156 | ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE; |
| 1157 | } |
| 1158 | |
| 1159 | static int qpnp_regulator_common_set_mode(struct regulator_dev *rdev, |
| 1160 | unsigned int mode) |
| 1161 | { |
| 1162 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1163 | int rc = 0; |
| 1164 | u8 val; |
| 1165 | |
| 1166 | if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) { |
| 1167 | vreg_err(vreg, "invalid mode: %u\n", mode); |
| 1168 | return -EINVAL; |
| 1169 | } |
| 1170 | |
| 1171 | val = (mode == REGULATOR_MODE_NORMAL ? QPNP_COMMON_MODE_HPM_MASK : 0); |
| 1172 | |
| 1173 | rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_MODE, val, |
| 1174 | QPNP_COMMON_MODE_HPM_MASK, |
| 1175 | &vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]); |
| 1176 | |
| 1177 | if (rc) |
| 1178 | vreg_err(vreg, "SPMI write failed, rc=%d\n", rc); |
| 1179 | else |
| 1180 | qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_MODE); |
| 1181 | |
| 1182 | return rc; |
| 1183 | } |
| 1184 | |
| 1185 | static unsigned int qpnp_regulator_common_get_optimum_mode( |
| 1186 | struct regulator_dev *rdev, int input_uV, int output_uV, |
| 1187 | int load_uA) |
| 1188 | { |
| 1189 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1190 | unsigned int mode; |
| 1191 | |
| 1192 | if (load_uA + vreg->system_load >= vreg->hpm_min_load) |
| 1193 | mode = REGULATOR_MODE_NORMAL; |
| 1194 | else |
| 1195 | mode = REGULATOR_MODE_IDLE; |
| 1196 | |
| 1197 | return mode; |
| 1198 | } |
| 1199 | |
| 1200 | static unsigned int qpnp_regulator_common2_get_mode(struct regulator_dev *rdev) |
| 1201 | { |
| 1202 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1203 | |
| 1204 | return vreg->ctrl_reg[QPNP_COMMON2_IDX_MODE] == QPNP_COMMON2_MODE_HPM |
| 1205 | ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE; |
| 1206 | } |
| 1207 | |
| 1208 | static int qpnp_regulator_common2_set_mode(struct regulator_dev *rdev, |
| 1209 | unsigned int mode) |
| 1210 | { |
| 1211 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1212 | int rc = 0; |
| 1213 | u8 val = QPNP_COMMON2_MODE_HPM; |
| 1214 | |
| 1215 | if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) { |
| 1216 | vreg_err(vreg, "invalid mode: %u\n", mode); |
| 1217 | return -EINVAL; |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * Use init_mode as the low power mode unless it is equal to HPM. This |
| 1222 | * ensures that AUTO mode is re-asserted after switching away from |
| 1223 | * forced HPM if it was configured initially. |
| 1224 | */ |
| 1225 | if (mode == REGULATOR_MODE_NORMAL) |
| 1226 | val = QPNP_COMMON2_MODE_HPM; |
| 1227 | else if (vreg->init_mode == QPNP_COMMON2_MODE_HPM) |
| 1228 | val = QPNP_COMMON2_MODE_LPM; |
| 1229 | else |
| 1230 | val = vreg->init_mode; |
| 1231 | |
| 1232 | rc = qpnp_vreg_write_optimized(vreg, QPNP_COMMON2_REG_MODE, &val, |
| 1233 | &vreg->ctrl_reg[QPNP_COMMON2_IDX_MODE], 1); |
| 1234 | if (rc) |
| 1235 | vreg_err(vreg, "SPMI write failed, rc=%d\n", rc); |
| 1236 | else |
| 1237 | qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_MODE); |
| 1238 | |
| 1239 | return rc; |
| 1240 | } |
| 1241 | |
| 1242 | static int qpnp_regulator_common_enable_time(struct regulator_dev *rdev) |
| 1243 | { |
| 1244 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1245 | |
| 1246 | return vreg->enable_time; |
| 1247 | } |
| 1248 | |
| 1249 | static int qpnp_regulator_vs_clear_ocp(struct qpnp_regulator *vreg) |
| 1250 | { |
| 1251 | int rc; |
| 1252 | |
| 1253 | rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE, |
| 1254 | QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK, |
| 1255 | &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]); |
| 1256 | if (rc) |
| 1257 | vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc); |
| 1258 | |
| 1259 | vreg->vs_enable_time = ktime_get(); |
| 1260 | |
| 1261 | rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE, |
| 1262 | QPNP_COMMON_ENABLE, QPNP_COMMON_ENABLE_MASK, |
| 1263 | &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]); |
| 1264 | if (rc) |
| 1265 | vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc); |
| 1266 | |
| 1267 | if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_OCP) { |
| 1268 | pr_info("%s: switch state toggled after OCP event\n", |
| 1269 | vreg->rdesc.name); |
| 1270 | } |
| 1271 | |
| 1272 | return rc; |
| 1273 | } |
| 1274 | |
| 1275 | static void qpnp_regulator_vs_ocp_work(struct work_struct *work) |
| 1276 | { |
| 1277 | struct delayed_work *dwork |
| 1278 | = container_of(work, struct delayed_work, work); |
| 1279 | struct qpnp_regulator *vreg |
| 1280 | = container_of(dwork, struct qpnp_regulator, ocp_work); |
| 1281 | |
| 1282 | qpnp_regulator_vs_clear_ocp(vreg); |
| 1283 | } |
| 1284 | |
| 1285 | static irqreturn_t qpnp_regulator_vs_ocp_isr(int irq, void *data) |
| 1286 | { |
| 1287 | struct qpnp_regulator *vreg = data; |
| 1288 | ktime_t ocp_irq_time; |
| 1289 | s64 ocp_trigger_delay_us; |
| 1290 | |
| 1291 | ocp_irq_time = ktime_get(); |
| 1292 | ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time, |
| 1293 | vreg->vs_enable_time); |
| 1294 | |
| 1295 | /* |
| 1296 | * Reset the OCP count if there is a large delay between switch enable |
| 1297 | * and when OCP triggers. This is indicative of a hotplug event as |
| 1298 | * opposed to a fault. |
| 1299 | */ |
| 1300 | if (ocp_trigger_delay_us > QPNP_VS_OCP_FAULT_DELAY_US) |
| 1301 | vreg->ocp_count = 0; |
| 1302 | |
| 1303 | /* Wait for switch output to settle back to 0 V after OCP triggered. */ |
| 1304 | udelay(QPNP_VS_OCP_FALL_DELAY_US); |
| 1305 | |
| 1306 | vreg->ocp_count++; |
| 1307 | |
| 1308 | if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_OCP) { |
| 1309 | pr_info("%s: VS OCP triggered, count = %d, delay = %lld us\n", |
| 1310 | vreg->rdesc.name, vreg->ocp_count, |
| 1311 | ocp_trigger_delay_us); |
| 1312 | } |
| 1313 | |
| 1314 | if (vreg->ocp_count == 1) { |
| 1315 | /* Immediately clear the over current condition. */ |
| 1316 | qpnp_regulator_vs_clear_ocp(vreg); |
| 1317 | } else if (vreg->ocp_count <= vreg->ocp_max_retries) { |
| 1318 | /* Schedule the over current clear task to run later. */ |
| 1319 | schedule_delayed_work(&vreg->ocp_work, |
| 1320 | msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1); |
| 1321 | } else { |
| 1322 | vreg_err(vreg, "OCP triggered %d times; no further retries\n", |
| 1323 | vreg->ocp_count); |
| 1324 | } |
| 1325 | |
| 1326 | return IRQ_HANDLED; |
| 1327 | } |
| 1328 | |
| 1329 | static const char * const qpnp_print_actions[] = { |
| 1330 | [QPNP_REGULATOR_ACTION_INIT] = "initial ", |
| 1331 | [QPNP_REGULATOR_ACTION_ENABLE] = "enable ", |
| 1332 | [QPNP_REGULATOR_ACTION_DISABLE] = "disable ", |
| 1333 | [QPNP_REGULATOR_ACTION_VOLTAGE] = "set voltage", |
| 1334 | [QPNP_REGULATOR_ACTION_MODE] = "set mode ", |
| 1335 | }; |
| 1336 | |
| 1337 | static const char * const qpnp_common2_mode_label[] = { |
| 1338 | [0] = "RSV", |
| 1339 | [1] = "RSV", |
| 1340 | [2] = "RSV", |
| 1341 | [QPNP_COMMON2_MODE_BYPASS] = "BYP", |
| 1342 | [QPNP_COMMON2_MODE_RETENTION] = "RET", |
| 1343 | [QPNP_COMMON2_MODE_LPM] = "LPM", |
| 1344 | [QPNP_COMMON2_MODE_AUTO] = "AUTO", |
| 1345 | [QPNP_COMMON2_MODE_HPM] = "HPM", |
| 1346 | }; |
| 1347 | |
| 1348 | static void qpnp_vreg_show_state(struct regulator_dev *rdev, |
| 1349 | enum qpnp_regulator_action action) |
| 1350 | { |
| 1351 | struct qpnp_regulator *vreg = rdev_get_drvdata(rdev); |
| 1352 | const char *action_label = qpnp_print_actions[action]; |
| 1353 | unsigned int mode = 0; |
| 1354 | int uV = 0; |
| 1355 | const char *mode_label = ""; |
| 1356 | enum qpnp_regulator_logical_type type; |
| 1357 | const char *enable_label = ""; |
| 1358 | char pc_enable_label[5] = {'\0'}; |
| 1359 | char pc_mode_label[8] = {'\0'}; |
| 1360 | bool show_req, show_dupe, show_init, has_changed; |
| 1361 | u8 en_reg, mode_reg; |
| 1362 | |
| 1363 | /* Do not print unless appropriate flags are set. */ |
| 1364 | show_req = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_REQUEST; |
| 1365 | show_dupe = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_DUPLICATE; |
| 1366 | show_init = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_INIT; |
| 1367 | has_changed = vreg->write_count != vreg->prev_write_count; |
| 1368 | if (!((show_init && action == QPNP_REGULATOR_ACTION_INIT) |
| 1369 | || (show_req && (has_changed || show_dupe)))) { |
| 1370 | return; |
| 1371 | } |
| 1372 | |
| 1373 | vreg->prev_write_count = vreg->write_count; |
| 1374 | |
| 1375 | type = vreg->logical_type; |
| 1376 | |
| 1377 | if (vreg->rdesc.ops->is_enabled) |
| 1378 | enable_label = vreg->rdesc.ops->is_enabled(rdev) |
| 1379 | ? "on " : "off"; |
| 1380 | |
| 1381 | if (vreg->rdesc.ops->get_voltage) |
| 1382 | uV = vreg->rdesc.ops->get_voltage(rdev); |
| 1383 | |
| 1384 | if (vreg->rdesc.ops->get_mode) { |
| 1385 | mode = vreg->rdesc.ops->get_mode(rdev); |
| 1386 | mode_label = mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"; |
| 1387 | } |
| 1388 | |
| 1389 | if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS |
| 1390 | || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO |
| 1391 | || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) { |
| 1392 | en_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]; |
| 1393 | pc_enable_label[0] = |
| 1394 | en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK ? '3' : '_'; |
| 1395 | pc_enable_label[1] = |
| 1396 | en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK ? '2' : '_'; |
| 1397 | pc_enable_label[2] = |
| 1398 | en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK ? '1' : '_'; |
| 1399 | pc_enable_label[3] = |
| 1400 | en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK ? '0' : '_'; |
| 1401 | } |
| 1402 | |
| 1403 | switch (type) { |
| 1404 | case QPNP_REGULATOR_LOGICAL_TYPE_SMPS: |
| 1405 | mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]; |
| 1406 | pc_mode_label[0] = |
| 1407 | mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_'; |
| 1408 | pc_mode_label[1] = |
| 1409 | mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_'; |
| 1410 | pc_mode_label[2] = |
| 1411 | mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_'; |
| 1412 | pc_mode_label[3] = |
| 1413 | mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_'; |
| 1414 | pc_mode_label[4] = |
| 1415 | mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_'; |
| 1416 | pc_mode_label[5] = |
| 1417 | mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_'; |
| 1418 | |
| 1419 | pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, alt_mode=%s\n", |
| 1420 | action_label, vreg->rdesc.name, enable_label, uV, |
| 1421 | mode_label, pc_enable_label, pc_mode_label); |
| 1422 | break; |
| 1423 | case QPNP_REGULATOR_LOGICAL_TYPE_LDO: |
| 1424 | mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]; |
| 1425 | pc_mode_label[0] = |
| 1426 | mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_'; |
| 1427 | pc_mode_label[1] = |
| 1428 | mode_reg & QPNP_COMMON_MODE_BYPASS_MASK ? 'B' : '_'; |
| 1429 | pc_mode_label[2] = |
| 1430 | mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_'; |
| 1431 | pc_mode_label[3] = |
| 1432 | mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_'; |
| 1433 | pc_mode_label[4] = |
| 1434 | mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_'; |
| 1435 | pc_mode_label[5] = |
| 1436 | mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_'; |
| 1437 | pc_mode_label[6] = |
| 1438 | mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_'; |
| 1439 | |
| 1440 | pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, alt_mode=%s\n", |
| 1441 | action_label, vreg->rdesc.name, enable_label, uV, |
| 1442 | mode_label, pc_enable_label, pc_mode_label); |
| 1443 | break; |
| 1444 | case QPNP_REGULATOR_LOGICAL_TYPE_LN_LDO: |
| 1445 | mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]; |
| 1446 | pc_mode_label[0] = |
| 1447 | mode_reg & QPNP_COMMON_MODE_BYPASS_MASK ? 'B' : '_'; |
| 1448 | |
| 1449 | pr_info("%s %-11s: %s, v=%7d uV, alt_mode=%s\n", |
| 1450 | action_label, vreg->rdesc.name, enable_label, uV, |
| 1451 | pc_mode_label); |
| 1452 | break; |
| 1453 | case QPNP_REGULATOR_LOGICAL_TYPE_VS: |
| 1454 | mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]; |
| 1455 | pc_mode_label[0] = |
| 1456 | mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_'; |
| 1457 | pc_mode_label[1] = |
| 1458 | mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_'; |
| 1459 | |
| 1460 | pr_info("%s %-11s: %s, mode=%s, pc_en=%s, alt_mode=%s\n", |
| 1461 | action_label, vreg->rdesc.name, enable_label, |
| 1462 | mode_label, pc_enable_label, pc_mode_label); |
| 1463 | break; |
| 1464 | case QPNP_REGULATOR_LOGICAL_TYPE_BOOST: |
| 1465 | pr_info("%s %-11s: %s, v=%7d uV\n", |
| 1466 | action_label, vreg->rdesc.name, enable_label, uV); |
| 1467 | break; |
| 1468 | case QPNP_REGULATOR_LOGICAL_TYPE_BOOST_BYP: |
| 1469 | pr_info("%s %-11s: %s, v=%7d uV\n", |
| 1470 | action_label, vreg->rdesc.name, enable_label, uV); |
| 1471 | break; |
| 1472 | case QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS: |
| 1473 | mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]; |
| 1474 | pc_mode_label[0] = |
| 1475 | mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_'; |
| 1476 | |
| 1477 | pr_info("%s %-11s: %s, v=%7d uV, mode=%s, alt_mode=%s\n", |
| 1478 | action_label, vreg->rdesc.name, enable_label, uV, |
| 1479 | mode_label, pc_mode_label); |
| 1480 | break; |
| 1481 | case QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS: |
| 1482 | case QPNP_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS: |
| 1483 | mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]; |
| 1484 | pc_mode_label[0] = |
| 1485 | mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_'; |
| 1486 | pr_info("%s %-11s: %s, v=%7d uV, mode=%s, alt_mode=%s\n", |
| 1487 | action_label, vreg->rdesc.name, enable_label, uV, |
| 1488 | mode_label, pc_mode_label); |
| 1489 | break; |
| 1490 | case QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO: |
| 1491 | mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]; |
| 1492 | pc_mode_label[0] = |
| 1493 | mode_reg & QPNP_COMMON_MODE_BYPASS_MASK ? 'B' : '_'; |
| 1494 | pc_mode_label[1] = |
| 1495 | mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_'; |
| 1496 | pr_info("%s %-11s: %s, v=%7d uV, mode=%s, alt_mode=%s\n", |
| 1497 | action_label, vreg->rdesc.name, enable_label, uV, |
| 1498 | mode_label, pc_mode_label); |
| 1499 | break; |
| 1500 | case QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS2: |
| 1501 | mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]; |
| 1502 | mode_label = qpnp_common2_mode_label[mode_reg |
| 1503 | & QPNP_COMMON2_MODE_MASK]; |
| 1504 | pr_info("%s %-11s: %s, v=%7d uV, mode=%s\n", |
| 1505 | action_label, vreg->rdesc.name, enable_label, uV, |
| 1506 | mode_label); |
| 1507 | break; |
| 1508 | default: |
| 1509 | break; |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | static struct regulator_ops qpnp_smps_ops = { |
| 1514 | .enable = qpnp_regulator_common_enable, |
| 1515 | .disable = qpnp_regulator_common_disable, |
| 1516 | .is_enabled = qpnp_regulator_common_is_enabled, |
| 1517 | .set_voltage = qpnp_regulator_common_set_voltage, |
| 1518 | .get_voltage = qpnp_regulator_common_get_voltage, |
| 1519 | .list_voltage = qpnp_regulator_common_list_voltage, |
| 1520 | .set_mode = qpnp_regulator_common_set_mode, |
| 1521 | .get_mode = qpnp_regulator_common_get_mode, |
| 1522 | .get_optimum_mode = qpnp_regulator_common_get_optimum_mode, |
| 1523 | .enable_time = qpnp_regulator_common_enable_time, |
| 1524 | }; |
| 1525 | |
| 1526 | static struct regulator_ops qpnp_ldo_ops = { |
| 1527 | .enable = qpnp_regulator_common_enable, |
| 1528 | .disable = qpnp_regulator_common_disable, |
| 1529 | .is_enabled = qpnp_regulator_common_is_enabled, |
| 1530 | .set_voltage = qpnp_regulator_common_set_voltage, |
| 1531 | .get_voltage = qpnp_regulator_common_get_voltage, |
| 1532 | .list_voltage = qpnp_regulator_common_list_voltage, |
| 1533 | .set_mode = qpnp_regulator_common_set_mode, |
| 1534 | .get_mode = qpnp_regulator_common_get_mode, |
| 1535 | .get_optimum_mode = qpnp_regulator_common_get_optimum_mode, |
| 1536 | .enable_time = qpnp_regulator_common_enable_time, |
| 1537 | }; |
| 1538 | |
| 1539 | static struct regulator_ops qpnp_ln_ldo_ops = { |
| 1540 | .enable = qpnp_regulator_common_enable, |
| 1541 | .disable = qpnp_regulator_common_disable, |
| 1542 | .is_enabled = qpnp_regulator_common_is_enabled, |
| 1543 | .set_voltage = qpnp_regulator_common_set_voltage, |
| 1544 | .get_voltage = qpnp_regulator_common_get_voltage, |
| 1545 | .list_voltage = qpnp_regulator_common_list_voltage, |
| 1546 | .enable_time = qpnp_regulator_common_enable_time, |
| 1547 | }; |
| 1548 | |
| 1549 | static struct regulator_ops qpnp_vs_ops = { |
| 1550 | .enable = qpnp_regulator_vs_enable, |
| 1551 | .disable = qpnp_regulator_common_disable, |
| 1552 | .is_enabled = qpnp_regulator_common_is_enabled, |
| 1553 | .enable_time = qpnp_regulator_common_enable_time, |
| 1554 | }; |
| 1555 | |
| 1556 | static struct regulator_ops qpnp_boost_ops = { |
| 1557 | .enable = qpnp_regulator_common_enable, |
| 1558 | .disable = qpnp_regulator_common_disable, |
| 1559 | .is_enabled = qpnp_regulator_common_is_enabled, |
| 1560 | .set_voltage = qpnp_regulator_single_range_set_voltage, |
| 1561 | .get_voltage = qpnp_regulator_single_range_get_voltage, |
| 1562 | .list_voltage = qpnp_regulator_common_list_voltage, |
| 1563 | .enable_time = qpnp_regulator_common_enable_time, |
| 1564 | }; |
| 1565 | |
| 1566 | static struct regulator_ops qpnp_ftsmps_ops = { |
| 1567 | .enable = qpnp_regulator_common_enable, |
| 1568 | .disable = qpnp_regulator_common_disable, |
| 1569 | .is_enabled = qpnp_regulator_common_is_enabled, |
| 1570 | .set_voltage = qpnp_regulator_common_set_voltage, |
| 1571 | .get_voltage = qpnp_regulator_common_get_voltage, |
| 1572 | .list_voltage = qpnp_regulator_common_list_voltage, |
| 1573 | .set_mode = qpnp_regulator_common_set_mode, |
| 1574 | .get_mode = qpnp_regulator_common_get_mode, |
| 1575 | .get_optimum_mode = qpnp_regulator_common_get_optimum_mode, |
| 1576 | .enable_time = qpnp_regulator_common_enable_time, |
| 1577 | }; |
| 1578 | |
| 1579 | static struct regulator_ops qpnp_ult_lo_smps_ops = { |
| 1580 | .enable = qpnp_regulator_common_enable, |
| 1581 | .disable = qpnp_regulator_common_disable, |
| 1582 | .is_enabled = qpnp_regulator_common_is_enabled, |
| 1583 | .set_voltage = qpnp_regulator_ult_lo_smps_set_voltage, |
| 1584 | .get_voltage = qpnp_regulator_ult_lo_smps_get_voltage, |
| 1585 | .list_voltage = qpnp_regulator_common_list_voltage, |
| 1586 | .set_mode = qpnp_regulator_common_set_mode, |
| 1587 | .get_mode = qpnp_regulator_common_get_mode, |
| 1588 | .get_optimum_mode = qpnp_regulator_common_get_optimum_mode, |
| 1589 | .enable_time = qpnp_regulator_common_enable_time, |
| 1590 | }; |
| 1591 | |
| 1592 | static struct regulator_ops qpnp_ult_ho_smps_ops = { |
| 1593 | .enable = qpnp_regulator_common_enable, |
| 1594 | .disable = qpnp_regulator_common_disable, |
| 1595 | .is_enabled = qpnp_regulator_common_is_enabled, |
| 1596 | .set_voltage = qpnp_regulator_single_range_set_voltage, |
| 1597 | .get_voltage = qpnp_regulator_single_range_get_voltage, |
| 1598 | .list_voltage = qpnp_regulator_common_list_voltage, |
| 1599 | .set_mode = qpnp_regulator_common_set_mode, |
| 1600 | .get_mode = qpnp_regulator_common_get_mode, |
| 1601 | .get_optimum_mode = qpnp_regulator_common_get_optimum_mode, |
| 1602 | .enable_time = qpnp_regulator_common_enable_time, |
| 1603 | }; |
| 1604 | |
| 1605 | static struct regulator_ops qpnp_ult_ldo_ops = { |
| 1606 | .enable = qpnp_regulator_common_enable, |
| 1607 | .disable = qpnp_regulator_common_disable, |
| 1608 | .is_enabled = qpnp_regulator_common_is_enabled, |
| 1609 | .set_voltage = qpnp_regulator_single_range_set_voltage, |
| 1610 | .get_voltage = qpnp_regulator_single_range_get_voltage, |
| 1611 | .list_voltage = qpnp_regulator_common_list_voltage, |
| 1612 | .set_mode = qpnp_regulator_common_set_mode, |
| 1613 | .get_mode = qpnp_regulator_common_get_mode, |
| 1614 | .get_optimum_mode = qpnp_regulator_common_get_optimum_mode, |
| 1615 | .enable_time = qpnp_regulator_common_enable_time, |
| 1616 | }; |
| 1617 | |
| 1618 | static struct regulator_ops qpnp_ftsmps426_ops = { |
| 1619 | .enable = qpnp_regulator_common_enable, |
| 1620 | .disable = qpnp_regulator_common_disable, |
| 1621 | .is_enabled = qpnp_regulator_common_is_enabled, |
| 1622 | .set_voltage = qpnp_regulator_common2_set_voltage, |
| 1623 | .get_voltage = qpnp_regulator_common2_get_voltage, |
| 1624 | .list_voltage = qpnp_regulator_common_list_voltage, |
| 1625 | .set_mode = qpnp_regulator_common2_set_mode, |
| 1626 | .get_mode = qpnp_regulator_common2_get_mode, |
| 1627 | .get_optimum_mode = qpnp_regulator_common_get_optimum_mode, |
| 1628 | .enable_time = qpnp_regulator_common_enable_time, |
| 1629 | }; |
| 1630 | |
| 1631 | /* Maximum possible digital major revision value */ |
| 1632 | #define INF 0xFF |
| 1633 | |
| 1634 | static const struct qpnp_regulator_mapping supported_regulators[] = { |
| 1635 | /* type subtype dig_min dig_max ltype ops setpoints hpm_min */ |
| 1636 | QPNP_VREG_MAP(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000), |
| 1637 | QPNP_VREG_MAP(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000), |
| 1638 | QPNP_VREG_MAP(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000), |
| 1639 | QPNP_VREG_MAP(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000), |
| 1640 | QPNP_VREG_MAP(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000), |
| 1641 | QPNP_VREG_MAP(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000), |
| 1642 | QPNP_VREG_MAP(LDO, N600_ST, 0, 0, LDO, ldo, nldo2, 10000), |
| 1643 | QPNP_VREG_MAP(LDO, N1200_ST, 0, 0, LDO, ldo, nldo2, 10000), |
| 1644 | QPNP_VREG_MAP(LDO, N600_ST, 1, INF, LDO, ldo, nldo3, 10000), |
| 1645 | QPNP_VREG_MAP(LDO, N1200_ST, 1, INF, LDO, ldo, nldo3, 10000), |
| 1646 | QPNP_VREG_MAP(LDO, P50, 0, INF, LDO, ldo, pldo, 5000), |
| 1647 | QPNP_VREG_MAP(LDO, P150, 0, INF, LDO, ldo, pldo, 10000), |
| 1648 | QPNP_VREG_MAP(LDO, P300, 0, INF, LDO, ldo, pldo, 10000), |
| 1649 | QPNP_VREG_MAP(LDO, P600, 0, INF, LDO, ldo, pldo, 10000), |
| 1650 | QPNP_VREG_MAP(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000), |
| 1651 | QPNP_VREG_MAP(LDO, LN, 0, INF, LN_LDO, ln_ldo, ln_ldo, 0), |
| 1652 | QPNP_VREG_MAP(LDO, LV_P50, 0, INF, LDO, ldo, pldo, 5000), |
| 1653 | QPNP_VREG_MAP(LDO, LV_P150, 0, INF, LDO, ldo, pldo, 10000), |
| 1654 | QPNP_VREG_MAP(LDO, LV_P300, 0, INF, LDO, ldo, pldo, 10000), |
| 1655 | QPNP_VREG_MAP(LDO, LV_P600, 0, INF, LDO, ldo, pldo, 10000), |
| 1656 | QPNP_VREG_MAP(LDO, LV_P1200, 0, INF, LDO, ldo, pldo, 10000), |
| 1657 | QPNP_VREG_MAP(VS, LV100, 0, INF, VS, vs, none, 0), |
| 1658 | QPNP_VREG_MAP(VS, LV300, 0, INF, VS, vs, none, 0), |
| 1659 | QPNP_VREG_MAP(VS, MV300, 0, INF, VS, vs, none, 0), |
| 1660 | QPNP_VREG_MAP(VS, MV500, 0, INF, VS, vs, none, 0), |
| 1661 | QPNP_VREG_MAP(VS, HDMI, 0, INF, VS, vs, none, 0), |
| 1662 | QPNP_VREG_MAP(VS, OTG, 0, INF, VS, vs, none, 0), |
| 1663 | QPNP_VREG_MAP(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0), |
| 1664 | QPNP_VREG_MAP(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000), |
| 1665 | QPNP_VREG_MAP(FTS, FTS2p5_CTL, 0, INF, FTSMPS, ftsmps, ftsmps2p5, |
| 1666 | 100000), |
| 1667 | QPNP_VREG_MAP(BOOST_BYP, BB_2A, 0, INF, BOOST_BYP, boost, boost_byp, 0), |
| 1668 | QPNP_VREG_MAP(ULT_BUCK, ULT_HF_CTL1, 0, INF, ULT_LO_SMPS, ult_lo_smps, |
| 1669 | ult_lo_smps, 100000), |
| 1670 | QPNP_VREG_MAP(ULT_BUCK, ULT_HF_CTL2, 0, INF, ULT_LO_SMPS, ult_lo_smps, |
| 1671 | ult_lo_smps, 100000), |
| 1672 | QPNP_VREG_MAP(ULT_BUCK, ULT_HF_CTL3, 0, INF, ULT_LO_SMPS, ult_lo_smps, |
| 1673 | ult_lo_smps, 100000), |
| 1674 | QPNP_VREG_MAP(ULT_BUCK, ULT_HF_CTL4, 0, INF, ULT_HO_SMPS, ult_ho_smps, |
| 1675 | ult_ho_smps, 100000), |
| 1676 | QPNP_VREG_MAP(ULT_LDO, N300_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, |
| 1677 | 10000), |
| 1678 | QPNP_VREG_MAP(ULT_LDO, N600_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, |
| 1679 | 10000), |
| 1680 | QPNP_VREG_MAP(ULT_LDO, N1200_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, |
| 1681 | 10000), |
| 1682 | QPNP_VREG_MAP(ULT_LDO, LV_P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, |
| 1683 | 10000), |
| 1684 | QPNP_VREG_MAP(ULT_LDO, LV_P300, 0, INF, ULT_LDO, ult_ldo, ult_pldo, |
| 1685 | 10000), |
| 1686 | QPNP_VREG_MAP(ULT_LDO, P600, 0, INF, ULT_LDO, ult_ldo, ult_pldo, |
| 1687 | 10000), |
| 1688 | QPNP_VREG_MAP(ULT_LDO, P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, |
| 1689 | 10000), |
| 1690 | QPNP_VREG_MAP(ULT_LDO, P50, 0, INF, ULT_LDO, ult_ldo, ult_pldo, |
| 1691 | 5000), |
| 1692 | QPNP_VREG_MAP(FTS, FTS426, 0, INF, FTSMPS2, ftsmps426, ftsmps426, |
| 1693 | 100000), |
| 1694 | }; |
| 1695 | |
| 1696 | static int qpnp_regulator_match(struct qpnp_regulator *vreg) |
| 1697 | { |
| 1698 | const struct qpnp_regulator_mapping *mapping; |
| 1699 | struct device_node *node = vreg->pdev->dev.of_node; |
| 1700 | int rc, i; |
| 1701 | u32 type_reg[2], dig_major_rev; |
| 1702 | u8 version[QPNP_COMMON_REG_SUBTYPE - QPNP_COMMON_REG_DIG_MAJOR_REV + 1]; |
| 1703 | u8 type, subtype; |
| 1704 | |
| 1705 | rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_DIG_MAJOR_REV, version, |
| 1706 | ARRAY_SIZE(version)); |
| 1707 | if (rc) { |
| 1708 | vreg_err(vreg, "could not read version registers, rc=%d\n", rc); |
| 1709 | return rc; |
| 1710 | } |
| 1711 | dig_major_rev = version[QPNP_COMMON_REG_DIG_MAJOR_REV |
| 1712 | - QPNP_COMMON_REG_DIG_MAJOR_REV]; |
| 1713 | type = version[QPNP_COMMON_REG_TYPE |
| 1714 | - QPNP_COMMON_REG_DIG_MAJOR_REV]; |
| 1715 | subtype = version[QPNP_COMMON_REG_SUBTYPE |
| 1716 | - QPNP_COMMON_REG_DIG_MAJOR_REV]; |
| 1717 | |
| 1718 | /* |
| 1719 | * Override type and subtype register values if qcom,force-type is |
| 1720 | * present in the device tree node. |
| 1721 | */ |
| 1722 | rc = of_property_read_u32_array(node, "qcom,force-type", type_reg, 2); |
| 1723 | if (!rc) { |
| 1724 | type = type_reg[0]; |
| 1725 | subtype = type_reg[1]; |
| 1726 | } |
| 1727 | |
| 1728 | rc = -ENODEV; |
| 1729 | for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) { |
| 1730 | mapping = &supported_regulators[i]; |
| 1731 | if (mapping->type == type && mapping->subtype == subtype |
| 1732 | && mapping->revision_min <= dig_major_rev |
| 1733 | && mapping->revision_max >= dig_major_rev) { |
| 1734 | vreg->logical_type = mapping->logical_type; |
| 1735 | vreg->set_points = mapping->set_points; |
| 1736 | vreg->hpm_min_load = mapping->hpm_min_load; |
| 1737 | vreg->rdesc.ops = mapping->ops; |
| 1738 | vreg->rdesc.n_voltages |
| 1739 | = mapping->set_points->n_voltages; |
| 1740 | rc = 0; |
| 1741 | break; |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | if (rc) |
| 1746 | vreg_err(vreg, "unsupported regulator: type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n", |
| 1747 | type, subtype, dig_major_rev); |
| 1748 | |
| 1749 | return rc; |
| 1750 | } |
| 1751 | |
| 1752 | static int qpnp_regulator_ftsmps_init_slew_rate(struct qpnp_regulator *vreg) |
| 1753 | { |
| 1754 | int rc; |
| 1755 | u8 reg = 0; |
| 1756 | int step = 0, delay, i, range_sel; |
| 1757 | struct qpnp_voltage_range *range = NULL; |
| 1758 | |
| 1759 | rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_STEP_CTRL, ®, 1); |
| 1760 | if (rc) { |
| 1761 | vreg_err(vreg, "spmi read failed, rc=%d\n", rc); |
| 1762 | return rc; |
| 1763 | } |
| 1764 | |
| 1765 | range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE]; |
| 1766 | |
| 1767 | for (i = 0; i < vreg->set_points->count; i++) { |
| 1768 | if (vreg->set_points->range[i].range_sel == range_sel) { |
| 1769 | range = &vreg->set_points->range[i]; |
| 1770 | break; |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | if (!range) { |
| 1775 | vreg_err(vreg, "range %d is invalid\n", range_sel); |
| 1776 | return -EINVAL; |
| 1777 | } |
| 1778 | |
| 1779 | step = (reg & QPNP_FTSMPS_STEP_CTRL_STEP_MASK) |
| 1780 | >> QPNP_FTSMPS_STEP_CTRL_STEP_SHIFT; |
| 1781 | |
| 1782 | delay = (reg & QPNP_FTSMPS_STEP_CTRL_DELAY_MASK) |
| 1783 | >> QPNP_FTSMPS_STEP_CTRL_DELAY_SHIFT; |
| 1784 | |
| 1785 | /* slew_rate has units of uV/us. */ |
| 1786 | vreg->slew_rate = QPNP_FTSMPS_CLOCK_RATE * range->step_uV * (1 << step); |
| 1787 | |
| 1788 | vreg->slew_rate /= 1000 * (QPNP_FTSMPS_STEP_DELAY << delay); |
| 1789 | |
| 1790 | vreg->slew_rate = vreg->slew_rate * QPNP_FTSMPS_STEP_MARGIN_NUM |
| 1791 | / QPNP_FTSMPS_STEP_MARGIN_DEN; |
| 1792 | |
| 1793 | /* Ensure that the slew rate is greater than 0. */ |
| 1794 | vreg->slew_rate = max(vreg->slew_rate, 1); |
| 1795 | |
| 1796 | return rc; |
| 1797 | } |
| 1798 | |
| 1799 | static int qpnp_regulator_ftsmps2_init_slew_rate(struct qpnp_regulator *vreg) |
| 1800 | { |
| 1801 | struct qpnp_voltage_range *range = NULL; |
| 1802 | int i, rc, delay; |
| 1803 | u8 reg = 0; |
| 1804 | |
| 1805 | rc = qpnp_vreg_read(vreg, QPNP_COMMON2_REG_STEP_CTRL, ®, 1); |
| 1806 | if (rc) { |
| 1807 | vreg_err(vreg, "spmi read failed, rc=%d\n", rc); |
| 1808 | return rc; |
| 1809 | } |
| 1810 | |
| 1811 | /* |
| 1812 | * Regulators using the common #2 register layout do not have a voltage |
| 1813 | * range select register. Choose the lowest possible step size to be |
| 1814 | * conservative in the slew rate calculation. |
| 1815 | */ |
| 1816 | for (i = 0; i < vreg->set_points->count; i++) { |
| 1817 | if (!range || vreg->set_points->range[i].step_uV |
| 1818 | < range->step_uV) |
| 1819 | range = &vreg->set_points->range[i]; |
| 1820 | } |
| 1821 | |
| 1822 | if (!range) { |
| 1823 | vreg_err(vreg, "range is invalid\n"); |
| 1824 | return -EINVAL; |
| 1825 | } |
| 1826 | |
| 1827 | delay = (reg & QPNP_FTSMPS2_STEP_CTRL_DELAY_MASK) |
| 1828 | >> QPNP_FTSMPS2_STEP_CTRL_DELAY_SHIFT; |
| 1829 | |
| 1830 | /* slew_rate has units of uV/us. */ |
| 1831 | vreg->slew_rate = QPNP_FTSMPS2_CLOCK_RATE * range->step_uV; |
| 1832 | vreg->slew_rate /= 1000 * (QPNP_FTSMPS2_STEP_DELAY << delay); |
| 1833 | vreg->slew_rate = vreg->slew_rate * QPNP_FTSMPS2_STEP_MARGIN_NUM |
| 1834 | / QPNP_FTSMPS2_STEP_MARGIN_DEN; |
| 1835 | |
| 1836 | /* Ensure that the slew rate is greater than 0. */ |
| 1837 | vreg->slew_rate = max(vreg->slew_rate, 1); |
| 1838 | |
| 1839 | return rc; |
| 1840 | } |
| 1841 | |
| 1842 | static int qpnp_regulator_init_registers(struct qpnp_regulator *vreg, |
| 1843 | struct qpnp_regulator_platform_data *pdata) |
| 1844 | { |
| 1845 | int rc, i; |
| 1846 | enum qpnp_regulator_logical_type type; |
| 1847 | u8 ctrl_reg[8], reg, mask; |
| 1848 | |
| 1849 | type = vreg->logical_type; |
| 1850 | |
| 1851 | rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE, |
| 1852 | vreg->ctrl_reg, 8); |
| 1853 | if (rc) { |
| 1854 | vreg_err(vreg, "spmi read failed, rc=%d\n", rc); |
| 1855 | return rc; |
| 1856 | } |
| 1857 | |
| 1858 | for (i = 0; i < ARRAY_SIZE(ctrl_reg); i++) |
| 1859 | ctrl_reg[i] = vreg->ctrl_reg[i]; |
| 1860 | |
| 1861 | /* Set up enable pin control. */ |
| 1862 | if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS |
| 1863 | || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO |
| 1864 | || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) |
| 1865 | && !(pdata->pin_ctrl_enable |
| 1866 | & QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) { |
| 1867 | ctrl_reg[QPNP_COMMON_IDX_ENABLE] &= |
| 1868 | ~QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK; |
| 1869 | ctrl_reg[QPNP_COMMON_IDX_ENABLE] |= |
| 1870 | pdata->pin_ctrl_enable & QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK; |
| 1871 | } |
| 1872 | |
| 1873 | /* Set up HPM control. */ |
| 1874 | if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS |
| 1875 | || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS |
| 1876 | || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS |
| 1877 | || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO |
| 1878 | || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO |
| 1879 | || type == QPNP_REGULATOR_LOGICAL_TYPE_VS |
| 1880 | || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS) |
| 1881 | && (pdata->hpm_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) { |
| 1882 | ctrl_reg[QPNP_COMMON_IDX_MODE] &= ~QPNP_COMMON_MODE_HPM_MASK; |
| 1883 | ctrl_reg[QPNP_COMMON_IDX_MODE] |= |
| 1884 | (pdata->hpm_enable ? QPNP_COMMON_MODE_HPM_MASK : 0); |
| 1885 | } |
| 1886 | |
| 1887 | /* Set up auto mode control. */ |
| 1888 | if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS |
| 1889 | || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO |
| 1890 | || type == QPNP_REGULATOR_LOGICAL_TYPE_VS |
| 1891 | || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS) |
| 1892 | && (pdata->auto_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) { |
| 1893 | ctrl_reg[QPNP_COMMON_IDX_MODE] &= |
| 1894 | ~QPNP_COMMON_MODE_AUTO_MASK; |
| 1895 | ctrl_reg[QPNP_COMMON_IDX_MODE] |= |
| 1896 | (pdata->auto_mode_enable ? QPNP_COMMON_MODE_AUTO_MASK : 0); |
| 1897 | } |
| 1898 | |
| 1899 | if (type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS2) { |
| 1900 | if (pdata->hpm_enable == QPNP_REGULATOR_ENABLE) |
| 1901 | ctrl_reg[QPNP_COMMON2_IDX_MODE] |
| 1902 | = QPNP_COMMON2_MODE_HPM; |
| 1903 | else if (pdata->auto_mode_enable == QPNP_REGULATOR_ENABLE) |
| 1904 | ctrl_reg[QPNP_COMMON2_IDX_MODE] |
| 1905 | = QPNP_COMMON2_MODE_AUTO; |
| 1906 | else if (pdata->hpm_enable == QPNP_REGULATOR_DISABLE |
| 1907 | && ctrl_reg[QPNP_COMMON2_IDX_MODE] |
| 1908 | == QPNP_COMMON2_MODE_HPM) |
| 1909 | ctrl_reg[QPNP_COMMON2_IDX_MODE] |
| 1910 | = QPNP_COMMON2_MODE_LPM; |
| 1911 | else if (pdata->auto_mode_enable == QPNP_REGULATOR_DISABLE |
| 1912 | && ctrl_reg[QPNP_COMMON2_IDX_MODE] |
| 1913 | == QPNP_COMMON2_MODE_AUTO) |
| 1914 | ctrl_reg[QPNP_COMMON2_IDX_MODE] |
| 1915 | = QPNP_COMMON2_MODE_LPM; |
| 1916 | } |
| 1917 | |
| 1918 | /* Set up mode pin control. */ |
| 1919 | if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS |
| 1920 | || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO) |
| 1921 | && !(pdata->pin_ctrl_hpm |
| 1922 | & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) { |
| 1923 | ctrl_reg[QPNP_COMMON_IDX_MODE] &= |
| 1924 | ~QPNP_COMMON_MODE_FOLLOW_ALL_MASK; |
| 1925 | ctrl_reg[QPNP_COMMON_IDX_MODE] |= |
| 1926 | pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_ALL_MASK; |
| 1927 | } |
| 1928 | |
| 1929 | if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS |
| 1930 | && !(pdata->pin_ctrl_hpm & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) { |
| 1931 | ctrl_reg[QPNP_COMMON_IDX_MODE] &= |
| 1932 | ~QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK; |
| 1933 | ctrl_reg[QPNP_COMMON_IDX_MODE] |= |
| 1934 | pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK; |
| 1935 | } |
| 1936 | |
| 1937 | if ((type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS |
| 1938 | || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS |
| 1939 | || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO) |
| 1940 | && !(pdata->pin_ctrl_hpm |
| 1941 | & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) { |
| 1942 | ctrl_reg[QPNP_COMMON_IDX_MODE] &= |
| 1943 | ~QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK; |
| 1944 | ctrl_reg[QPNP_COMMON_IDX_MODE] |= |
| 1945 | pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK; |
| 1946 | } |
| 1947 | |
| 1948 | if ((type == QPNP_REGULATOR_LOGICAL_TYPE_LDO |
| 1949 | || type == QPNP_REGULATOR_LOGICAL_TYPE_LN_LDO |
| 1950 | || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO) |
| 1951 | && pdata->bypass_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT) { |
| 1952 | ctrl_reg[QPNP_COMMON_IDX_MODE] &= |
| 1953 | ~QPNP_COMMON_MODE_BYPASS_MASK; |
| 1954 | ctrl_reg[QPNP_COMMON_IDX_MODE] |= |
| 1955 | (pdata->bypass_mode_enable |
| 1956 | ? QPNP_COMMON_MODE_BYPASS_MASK : 0); |
| 1957 | } |
| 1958 | |
| 1959 | /* Set boost current limit. */ |
| 1960 | if ((type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST |
| 1961 | || type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST_BYP) |
| 1962 | && pdata->boost_current_limit |
| 1963 | != QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT) { |
| 1964 | reg = pdata->boost_current_limit; |
| 1965 | mask = QPNP_BOOST_CURRENT_LIMIT_MASK; |
| 1966 | rc = qpnp_vreg_masked_read_write(vreg, |
| 1967 | (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST |
| 1968 | ? QPNP_BOOST_REG_CURRENT_LIMIT |
| 1969 | : QPNP_BOOST_BYP_REG_CURRENT_LIMIT), |
| 1970 | reg, mask); |
| 1971 | if (rc) { |
| 1972 | vreg_err(vreg, "spmi write failed, rc=%d\n", rc); |
| 1973 | return rc; |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | /* Write back any control register values that were modified. */ |
| 1978 | rc = qpnp_vreg_write_optimized(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE, |
| 1979 | ctrl_reg, vreg->ctrl_reg, 8); |
| 1980 | if (rc) { |
| 1981 | vreg_err(vreg, "spmi write failed, rc=%d\n", rc); |
| 1982 | return rc; |
| 1983 | } |
| 1984 | |
| 1985 | /* Setup initial range for ULT_LO_SMPS */ |
| 1986 | if (type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS) { |
| 1987 | ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] = |
| 1988 | (ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] |
| 1989 | < ULT_SMPS_RANGE_SPLIT) ? 0 : 1; |
| 1990 | } |
| 1991 | |
| 1992 | /* Set pull down. */ |
| 1993 | if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS |
| 1994 | || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS |
| 1995 | || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS |
| 1996 | || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO |
| 1997 | || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO |
| 1998 | || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) |
| 1999 | && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) { |
| 2000 | reg = pdata->pull_down_enable |
| 2001 | ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0; |
| 2002 | rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_PULL_DOWN, ®, 1); |
| 2003 | if (rc) { |
| 2004 | vreg_err(vreg, "spmi write failed, rc=%d\n", rc); |
| 2005 | return rc; |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | if ((type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS |
| 2010 | || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS2) |
| 2011 | && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) { |
| 2012 | /* FTSMPS has other bits in the pull down control register. */ |
| 2013 | reg = pdata->pull_down_enable |
| 2014 | ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0; |
| 2015 | rc = qpnp_vreg_masked_read_write(vreg, |
| 2016 | QPNP_COMMON_REG_PULL_DOWN, reg, |
| 2017 | QPNP_COMMON_PULL_DOWN_ENABLE_MASK); |
| 2018 | if (rc) { |
| 2019 | vreg_err(vreg, "spmi write failed, rc=%d\n", rc); |
| 2020 | return rc; |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | /* Set soft start for LDO. */ |
| 2025 | if ((type == QPNP_REGULATOR_LOGICAL_TYPE_LDO |
| 2026 | || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO) |
| 2027 | && pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) { |
| 2028 | reg = pdata->soft_start_enable |
| 2029 | ? QPNP_LDO_SOFT_START_ENABLE_MASK : 0; |
| 2030 | rc = qpnp_vreg_write(vreg, QPNP_LDO_REG_SOFT_START, ®, 1); |
| 2031 | if (rc) { |
| 2032 | vreg_err(vreg, "spmi write failed, rc=%d\n", rc); |
| 2033 | return rc; |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | /* Set soft start strength and over current protection for VS. */ |
| 2038 | if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS) { |
| 2039 | reg = 0; |
| 2040 | mask = 0; |
| 2041 | if (pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) { |
| 2042 | reg |= pdata->soft_start_enable |
| 2043 | ? QPNP_VS_SOFT_START_ENABLE_MASK : 0; |
| 2044 | mask |= QPNP_VS_SOFT_START_ENABLE_MASK; |
| 2045 | } |
| 2046 | if (pdata->vs_soft_start_strength |
| 2047 | != QPNP_VS_SOFT_START_STR_HW_DEFAULT) { |
| 2048 | reg |= pdata->vs_soft_start_strength |
| 2049 | & QPNP_VS_SOFT_START_SEL_MASK; |
| 2050 | mask |= QPNP_VS_SOFT_START_SEL_MASK; |
| 2051 | } |
| 2052 | rc = qpnp_vreg_masked_read_write(vreg, QPNP_VS_REG_SOFT_START, |
| 2053 | reg, mask); |
| 2054 | if (rc) { |
| 2055 | vreg_err(vreg, "spmi write failed, rc=%d\n", rc); |
| 2056 | return rc; |
| 2057 | } |
| 2058 | |
| 2059 | if (pdata->ocp_enable != QPNP_REGULATOR_USE_HW_DEFAULT) { |
| 2060 | reg = pdata->ocp_enable ? QPNP_VS_OCP_NO_OVERRIDE |
| 2061 | : QPNP_VS_OCP_OVERRIDE; |
| 2062 | rc = qpnp_vreg_write(vreg, QPNP_VS_REG_OCP, ®, 1); |
| 2063 | if (rc) { |
| 2064 | vreg_err(vreg, "spmi write failed, rc=%d\n", |
| 2065 | rc); |
| 2066 | return rc; |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | /* Calculate the slew rate for FTSMPS regulators. */ |
| 2072 | if (type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS) { |
| 2073 | rc = qpnp_regulator_ftsmps_init_slew_rate(vreg); |
| 2074 | if (rc) { |
| 2075 | vreg_err(vreg, "failed to initialize step rate, rc=%d\n", |
| 2076 | rc); |
| 2077 | return rc; |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | /* Calculate the slew rate for FTSMPS2 regulators. */ |
| 2082 | if (type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS2) { |
| 2083 | rc = qpnp_regulator_ftsmps2_init_slew_rate(vreg); |
| 2084 | if (rc) { |
| 2085 | vreg_err(vreg, "failed to initialize step rate, rc=%d\n", |
| 2086 | rc); |
| 2087 | return rc; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | vreg->init_mode = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]; |
| 2092 | |
| 2093 | return rc; |
| 2094 | } |
| 2095 | |
| 2096 | /* Fill in pdata elements based on values found in device tree. */ |
| 2097 | static int qpnp_regulator_get_dt_config(struct platform_device *pdev, |
| 2098 | struct qpnp_regulator_platform_data *pdata) |
| 2099 | { |
| 2100 | unsigned int base; |
| 2101 | struct device_node *node = pdev->dev.of_node; |
| 2102 | int rc = 0; |
| 2103 | |
| 2104 | pdata->init_data.constraints.input_uV |
| 2105 | = pdata->init_data.constraints.max_uV; |
| 2106 | |
| 2107 | rc = of_property_read_u32(pdev->dev.of_node, "reg", &base); |
| 2108 | if (rc < 0) { |
| 2109 | dev_err(&pdev->dev, |
| 2110 | "Couldn't find reg in node = %s rc = %d\n", |
| 2111 | pdev->dev.of_node->full_name, rc); |
| 2112 | return rc; |
| 2113 | } |
| 2114 | pdata->base_addr = base; |
| 2115 | |
| 2116 | /* OCP IRQ is optional so ignore get errors. */ |
| 2117 | pdata->ocp_irq = platform_get_irq_byname(pdev, "ocp"); |
| 2118 | if (pdata->ocp_irq < 0) |
| 2119 | pdata->ocp_irq = 0; |
| 2120 | |
| 2121 | /* |
| 2122 | * Initialize configuration parameters to use hardware default in case |
| 2123 | * no value is specified via device tree. |
| 2124 | */ |
| 2125 | pdata->auto_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT; |
| 2126 | pdata->bypass_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT; |
| 2127 | pdata->ocp_enable = QPNP_REGULATOR_USE_HW_DEFAULT; |
| 2128 | pdata->pull_down_enable = QPNP_REGULATOR_USE_HW_DEFAULT; |
| 2129 | pdata->soft_start_enable = QPNP_REGULATOR_USE_HW_DEFAULT; |
| 2130 | pdata->boost_current_limit = QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT; |
| 2131 | pdata->pin_ctrl_enable = QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT; |
| 2132 | pdata->pin_ctrl_hpm = QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT; |
| 2133 | pdata->vs_soft_start_strength = QPNP_VS_SOFT_START_STR_HW_DEFAULT; |
| 2134 | pdata->hpm_enable = QPNP_REGULATOR_USE_HW_DEFAULT; |
| 2135 | |
| 2136 | /* These bindings are optional, so it is okay if they are not found. */ |
| 2137 | of_property_read_u32(node, "qcom,auto-mode-enable", |
| 2138 | &pdata->auto_mode_enable); |
| 2139 | of_property_read_u32(node, "qcom,bypass-mode-enable", |
| 2140 | &pdata->bypass_mode_enable); |
| 2141 | of_property_read_u32(node, "qcom,ocp-enable", &pdata->ocp_enable); |
| 2142 | of_property_read_u32(node, "qcom,ocp-max-retries", |
| 2143 | &pdata->ocp_max_retries); |
| 2144 | of_property_read_u32(node, "qcom,ocp-retry-delay", |
| 2145 | &pdata->ocp_retry_delay_ms); |
| 2146 | of_property_read_u32(node, "qcom,pull-down-enable", |
| 2147 | &pdata->pull_down_enable); |
| 2148 | of_property_read_u32(node, "qcom,soft-start-enable", |
| 2149 | &pdata->soft_start_enable); |
| 2150 | of_property_read_u32(node, "qcom,boost-current-limit", |
| 2151 | &pdata->boost_current_limit); |
| 2152 | of_property_read_u32(node, "qcom,pin-ctrl-enable", |
| 2153 | &pdata->pin_ctrl_enable); |
| 2154 | of_property_read_u32(node, "qcom,pin-ctrl-hpm", &pdata->pin_ctrl_hpm); |
| 2155 | of_property_read_u32(node, "qcom,hpm-enable", &pdata->hpm_enable); |
| 2156 | of_property_read_u32(node, "qcom,vs-soft-start-strength", |
| 2157 | &pdata->vs_soft_start_strength); |
| 2158 | of_property_read_u32(node, "qcom,system-load", &pdata->system_load); |
| 2159 | of_property_read_u32(node, "qcom,enable-time", &pdata->enable_time); |
| 2160 | |
| 2161 | return rc; |
| 2162 | } |
| 2163 | |
| 2164 | static const struct of_device_id spmi_match_table[]; |
| 2165 | |
| 2166 | #define MAX_NAME_LEN 127 |
| 2167 | |
| 2168 | static int qpnp_regulator_probe(struct platform_device *pdev) |
| 2169 | { |
| 2170 | struct regulator_config reg_config = {}; |
| 2171 | struct qpnp_regulator_platform_data *pdata; |
| 2172 | struct qpnp_regulator *vreg; |
| 2173 | struct regulator_desc *rdesc; |
| 2174 | struct qpnp_regulator_platform_data of_pdata; |
| 2175 | struct regulator_init_data *init_data; |
| 2176 | char *reg_name; |
| 2177 | int rc; |
| 2178 | bool is_dt; |
| 2179 | |
| 2180 | vreg = kzalloc(sizeof(struct qpnp_regulator), GFP_KERNEL); |
| 2181 | if (!vreg) |
| 2182 | return -ENOMEM; |
| 2183 | |
| 2184 | vreg->regmap = dev_get_regmap(pdev->dev.parent, NULL); |
| 2185 | if (!vreg->regmap) { |
| 2186 | dev_err(&pdev->dev, "Couldn't get parent's regmap\n"); |
| 2187 | return -EINVAL; |
| 2188 | } |
| 2189 | |
| 2190 | is_dt = of_match_device(spmi_match_table, &pdev->dev); |
| 2191 | |
| 2192 | /* Check if device tree is in use. */ |
| 2193 | if (is_dt) { |
| 2194 | init_data = of_get_regulator_init_data(&pdev->dev, |
| 2195 | pdev->dev.of_node, |
| 2196 | &vreg->rdesc); |
| 2197 | if (!init_data) { |
| 2198 | dev_err(&pdev->dev, "%s: unable to allocate memory\n", |
| 2199 | __func__); |
| 2200 | kfree(vreg); |
| 2201 | return -ENOMEM; |
| 2202 | } |
| 2203 | memset(&of_pdata, 0, |
| 2204 | sizeof(struct qpnp_regulator_platform_data)); |
| 2205 | memcpy(&of_pdata.init_data, init_data, |
| 2206 | sizeof(struct regulator_init_data)); |
| 2207 | |
| 2208 | if (of_get_property(pdev->dev.of_node, "parent-supply", NULL)) |
| 2209 | of_pdata.init_data.supply_regulator = "parent"; |
| 2210 | |
| 2211 | rc = qpnp_regulator_get_dt_config(pdev, &of_pdata); |
| 2212 | if (rc) { |
| 2213 | dev_err(&pdev->dev, "%s: DT parsing failed, rc=%d\n", |
| 2214 | __func__, rc); |
| 2215 | kfree(vreg); |
| 2216 | return -ENOMEM; |
| 2217 | } |
| 2218 | |
| 2219 | pdata = &of_pdata; |
| 2220 | } else { |
| 2221 | pdata = pdev->dev.platform_data; |
| 2222 | } |
| 2223 | |
| 2224 | if (pdata == NULL) { |
| 2225 | dev_err(&pdev->dev, "%s: no platform data specified\n", |
| 2226 | __func__); |
| 2227 | kfree(vreg); |
| 2228 | return -EINVAL; |
| 2229 | } |
| 2230 | |
| 2231 | vreg->pdev = pdev; |
| 2232 | vreg->prev_write_count = -1; |
| 2233 | vreg->write_count = 0; |
| 2234 | vreg->base_addr = pdata->base_addr; |
| 2235 | vreg->enable_time = pdata->enable_time; |
| 2236 | vreg->system_load = pdata->system_load; |
| 2237 | vreg->ocp_enable = pdata->ocp_enable; |
| 2238 | vreg->ocp_irq = pdata->ocp_irq; |
| 2239 | vreg->ocp_max_retries = pdata->ocp_max_retries; |
| 2240 | vreg->ocp_retry_delay_ms = pdata->ocp_retry_delay_ms; |
| 2241 | |
| 2242 | if (vreg->ocp_max_retries == 0) |
| 2243 | vreg->ocp_max_retries = QPNP_VS_OCP_DEFAULT_MAX_RETRIES; |
| 2244 | if (vreg->ocp_retry_delay_ms == 0) |
| 2245 | vreg->ocp_retry_delay_ms = QPNP_VS_OCP_DEFAULT_RETRY_DELAY_MS; |
| 2246 | |
| 2247 | rdesc = &vreg->rdesc; |
| 2248 | rdesc->id = to_spmi_device(pdev->dev.parent)->ctrl->nr; |
| 2249 | rdesc->owner = THIS_MODULE; |
| 2250 | rdesc->type = REGULATOR_VOLTAGE; |
| 2251 | |
| 2252 | reg_name = kzalloc(strnlen(pdata->init_data.constraints.name, |
| 2253 | MAX_NAME_LEN) + 1, GFP_KERNEL); |
| 2254 | if (!reg_name) { |
| 2255 | kfree(vreg); |
| 2256 | return -ENOMEM; |
| 2257 | } |
| 2258 | strlcpy(reg_name, pdata->init_data.constraints.name, |
| 2259 | strnlen(pdata->init_data.constraints.name, MAX_NAME_LEN) + 1); |
| 2260 | rdesc->name = reg_name; |
| 2261 | |
| 2262 | dev_set_drvdata(&pdev->dev, vreg); |
| 2263 | |
| 2264 | rc = qpnp_regulator_match(vreg); |
| 2265 | if (rc) |
| 2266 | goto bail; |
| 2267 | |
| 2268 | if (is_dt && rdesc->ops) { |
| 2269 | /* Fill in ops and mode masks when using device tree. */ |
| 2270 | if (rdesc->ops->enable) |
| 2271 | pdata->init_data.constraints.valid_ops_mask |
| 2272 | |= REGULATOR_CHANGE_STATUS; |
| 2273 | if (rdesc->ops->get_voltage) |
| 2274 | pdata->init_data.constraints.valid_ops_mask |
| 2275 | |= REGULATOR_CHANGE_VOLTAGE; |
| 2276 | if (rdesc->ops->get_mode) { |
| 2277 | pdata->init_data.constraints.valid_ops_mask |
| 2278 | |= REGULATOR_CHANGE_MODE |
| 2279 | | REGULATOR_CHANGE_DRMS; |
| 2280 | pdata->init_data.constraints.valid_modes_mask |
| 2281 | = REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE; |
| 2282 | } |
| 2283 | } |
| 2284 | |
| 2285 | rc = qpnp_regulator_init_registers(vreg, pdata); |
| 2286 | if (rc) { |
| 2287 | vreg_err(vreg, "common initialization failed, rc=%d\n", rc); |
| 2288 | goto bail; |
| 2289 | } |
| 2290 | |
| 2291 | if (vreg->logical_type != QPNP_REGULATOR_LOGICAL_TYPE_VS) |
| 2292 | vreg->ocp_irq = 0; |
| 2293 | |
| 2294 | if (vreg->ocp_irq) { |
| 2295 | rc = devm_request_irq(&pdev->dev, vreg->ocp_irq, |
| 2296 | qpnp_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp", |
| 2297 | vreg); |
| 2298 | if (rc < 0) { |
| 2299 | vreg_err(vreg, "failed to request irq %d, rc=%d\n", |
| 2300 | vreg->ocp_irq, rc); |
| 2301 | goto bail; |
| 2302 | } |
| 2303 | |
| 2304 | INIT_DELAYED_WORK(&vreg->ocp_work, qpnp_regulator_vs_ocp_work); |
| 2305 | } |
| 2306 | |
| 2307 | reg_config.dev = &pdev->dev; |
| 2308 | reg_config.init_data = &pdata->init_data; |
| 2309 | reg_config.driver_data = vreg; |
| 2310 | reg_config.of_node = pdev->dev.of_node; |
| 2311 | vreg->rdev = regulator_register(rdesc, ®_config); |
| 2312 | if (IS_ERR(vreg->rdev)) { |
| 2313 | rc = PTR_ERR(vreg->rdev); |
| 2314 | if (rc != -EPROBE_DEFER) |
| 2315 | vreg_err(vreg, "regulator_register failed, rc=%d\n", |
| 2316 | rc); |
| 2317 | goto cancel_ocp_work; |
| 2318 | } |
| 2319 | |
| 2320 | if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_INIT && vreg->slew_rate) |
| 2321 | pr_info("%-11s: step rate=%d uV/us\n", vreg->rdesc.name, |
| 2322 | vreg->slew_rate); |
| 2323 | |
| 2324 | qpnp_vreg_show_state(vreg->rdev, QPNP_REGULATOR_ACTION_INIT); |
| 2325 | |
| 2326 | return 0; |
| 2327 | |
| 2328 | cancel_ocp_work: |
| 2329 | if (vreg->ocp_irq) |
| 2330 | cancel_delayed_work_sync(&vreg->ocp_work); |
| 2331 | bail: |
| 2332 | if (rc && rc != -EPROBE_DEFER) |
| 2333 | vreg_err(vreg, "probe failed, rc=%d\n", rc); |
| 2334 | |
| 2335 | kfree(vreg->rdesc.name); |
| 2336 | kfree(vreg); |
| 2337 | |
| 2338 | return rc; |
| 2339 | } |
| 2340 | |
| 2341 | static int qpnp_regulator_remove(struct platform_device *pdev) |
| 2342 | { |
| 2343 | struct qpnp_regulator *vreg; |
| 2344 | |
| 2345 | vreg = dev_get_drvdata(&pdev->dev); |
| 2346 | dev_set_drvdata(&pdev->dev, NULL); |
| 2347 | |
| 2348 | if (vreg) { |
| 2349 | regulator_unregister(vreg->rdev); |
| 2350 | if (vreg->ocp_irq) |
| 2351 | cancel_delayed_work_sync(&vreg->ocp_work); |
| 2352 | kfree(vreg->rdesc.name); |
| 2353 | kfree(vreg); |
| 2354 | } |
| 2355 | |
| 2356 | return 0; |
| 2357 | } |
| 2358 | |
| 2359 | static const struct of_device_id spmi_match_table[] = { |
| 2360 | { .compatible = QPNP_REGULATOR_DRIVER_NAME, }, |
| 2361 | {} |
| 2362 | }; |
| 2363 | |
| 2364 | static const struct platform_device_id qpnp_regulator_id[] = { |
| 2365 | { QPNP_REGULATOR_DRIVER_NAME, 0 }, |
| 2366 | { } |
| 2367 | }; |
| 2368 | MODULE_DEVICE_TABLE(spmi, qpnp_regulator_id); |
| 2369 | |
| 2370 | static struct platform_driver qpnp_regulator_driver = { |
| 2371 | .driver = { |
| 2372 | .name = QPNP_REGULATOR_DRIVER_NAME, |
| 2373 | .of_match_table = spmi_match_table, |
| 2374 | .owner = THIS_MODULE, |
| 2375 | }, |
| 2376 | .probe = qpnp_regulator_probe, |
| 2377 | .remove = qpnp_regulator_remove, |
| 2378 | .id_table = qpnp_regulator_id, |
| 2379 | }; |
| 2380 | |
| 2381 | /* |
| 2382 | * Pre-compute the number of set points available for each regulator type to |
| 2383 | * avoid unnecessary calculations later in runtime. |
| 2384 | */ |
| 2385 | static void qpnp_regulator_set_point_init(void) |
| 2386 | { |
| 2387 | struct qpnp_voltage_set_points **set_points; |
| 2388 | int i, j, temp; |
| 2389 | |
| 2390 | set_points = all_set_points; |
| 2391 | |
| 2392 | for (i = 0; i < ARRAY_SIZE(all_set_points); i++) { |
| 2393 | temp = 0; |
| 2394 | for (j = 0; j < all_set_points[i]->count; j++) { |
| 2395 | all_set_points[i]->range[j].n_voltages |
| 2396 | = (all_set_points[i]->range[j].set_point_max_uV |
| 2397 | - all_set_points[i]->range[j].set_point_min_uV) |
| 2398 | / all_set_points[i]->range[j].step_uV + 1; |
| 2399 | if (all_set_points[i]->range[j].set_point_max_uV == 0) |
| 2400 | all_set_points[i]->range[j].n_voltages = 0; |
| 2401 | temp += all_set_points[i]->range[j].n_voltages; |
| 2402 | } |
| 2403 | all_set_points[i]->n_voltages = temp; |
| 2404 | } |
| 2405 | } |
| 2406 | |
| 2407 | /** |
| 2408 | * qpnp_regulator_init() - register spmi driver for qpnp-regulator |
| 2409 | * |
| 2410 | * This initialization function should be called in systems in which driver |
| 2411 | * registration ordering must be controlled precisely. |
| 2412 | */ |
| 2413 | int __init qpnp_regulator_init(void) |
| 2414 | { |
| 2415 | static bool has_registered; |
| 2416 | |
| 2417 | if (has_registered) |
| 2418 | return 0; |
| 2419 | has_registered = true; |
| 2420 | |
| 2421 | qpnp_regulator_set_point_init(); |
| 2422 | |
| 2423 | return platform_driver_register(&qpnp_regulator_driver); |
| 2424 | } |
| 2425 | EXPORT_SYMBOL(qpnp_regulator_init); |
| 2426 | |
| 2427 | static void __exit qpnp_regulator_exit(void) |
| 2428 | { |
| 2429 | platform_driver_unregister(&qpnp_regulator_driver); |
| 2430 | } |
| 2431 | |
| 2432 | MODULE_DESCRIPTION("QPNP PMIC regulator driver"); |
| 2433 | MODULE_LICENSE("GPL v2"); |
| 2434 | |
| 2435 | arch_initcall(qpnp_regulator_init); |
| 2436 | module_exit(qpnp_regulator_exit); |