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