Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 13 | #define pr_fmt(fmt) "PDN %s: " fmt, __func__ |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 14 | |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/of_device.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/regulator/driver.h> |
| 27 | #include <linux/regulator/machine.h> |
| 28 | #include <linux/regulator/of_regulator.h> |
| 29 | #include <linux/regulator/krait-regulator.h> |
Abhijeet Dharmapurikar | 37c47a8 | 2013-01-08 18:44:21 -0800 | [diff] [blame] | 30 | #include <linux/debugfs.h> |
Abhijeet Dharmapurikar | c8605a6 | 2013-03-11 21:24:25 -0700 | [diff] [blame] | 31 | #include <linux/syscore_ops.h> |
Abhijeet Dharmapurikar | 3ff7cba | 2012-10-10 17:48:29 -0700 | [diff] [blame] | 32 | #include <mach/msm_iomap.h> |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 33 | |
| 34 | #include "spm.h" |
Abhijeet Dharmapurikar | 295c4dc | 2013-01-08 12:59:01 -0800 | [diff] [blame] | 35 | #include "pm.h" |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 36 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 37 | /* |
| 38 | * supply |
| 39 | * from |
| 40 | * pmic |
| 41 | * gang |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 42 | * | |
| 43 | * |________________________________ |
| 44 | * | | | |
| 45 | * ___|___ | | |
| 46 | * | | | | |
| 47 | * | | / / |
| 48 | * | LDO | / /LDO BYP [6] |
| 49 | * | | / BHS[6] /(bypass is a weak BHS |
| 50 | * |_______| | | needs to be on when in |
| 51 | * | | | BHS mode) |
| 52 | * |________________|_______________| |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 53 | * | |
| 54 | * ________|________ |
| 55 | * | | |
| 56 | * | KRAIT | |
| 57 | * |_________________| |
| 58 | */ |
| 59 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 60 | #define PMIC_VOLTAGE_MIN 350000 |
| 61 | #define PMIC_VOLTAGE_MAX 1355000 |
| 62 | #define LV_RANGE_STEP 5000 |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 63 | |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 64 | #define CORE_VOLTAGE_BOOTUP 900000 |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 65 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 66 | #define KRAIT_LDO_VOLTAGE_MIN 465000 |
Abhijeet Dharmapurikar | e8571ef | 2012-08-14 20:44:58 -0700 | [diff] [blame] | 67 | #define KRAIT_LDO_VOLTAGE_OFFSET 465000 |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 68 | #define KRAIT_LDO_STEP 5000 |
| 69 | |
| 70 | #define BHS_SETTLING_DELAY_US 1 |
| 71 | #define LDO_SETTLING_DELAY_US 1 |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 72 | #define MDD_SETTLING_DELAY_US 5 |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 73 | |
| 74 | #define _KRAIT_MASK(BITS, POS) (((u32)(1 << (BITS)) - 1) << POS) |
| 75 | #define KRAIT_MASK(LEFT_BIT_POS, RIGHT_BIT_POS) \ |
| 76 | _KRAIT_MASK(LEFT_BIT_POS - RIGHT_BIT_POS + 1, RIGHT_BIT_POS) |
| 77 | |
| 78 | #define APC_SECURE 0x00000000 |
| 79 | #define CPU_PWR_CTL 0x00000004 |
| 80 | #define APC_PWR_STATUS 0x00000008 |
| 81 | #define APC_TEST_BUS_SEL 0x0000000C |
| 82 | #define CPU_TRGTD_DBG_RST 0x00000010 |
| 83 | #define APC_PWR_GATE_CTL 0x00000014 |
| 84 | #define APC_LDO_VREF_SET 0x00000018 |
Abhijeet Dharmapurikar | 3ff7cba | 2012-10-10 17:48:29 -0700 | [diff] [blame] | 85 | #define APC_PWR_GATE_MODE 0x0000001C |
| 86 | #define APC_PWR_GATE_DLY 0x00000020 |
| 87 | |
| 88 | #define PWR_GATE_CONFIG 0x00000044 |
| 89 | #define VERSION 0x00000FD0 |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 90 | |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 91 | /* MDD register group */ |
| 92 | #define MDD_CONFIG_CTL 0x00000000 |
| 93 | #define MDD_MODE 0x00000010 |
| 94 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 95 | /* bit definitions for APC_PWR_GATE_CTL */ |
| 96 | #define BHS_CNT_BIT_POS 24 |
| 97 | #define BHS_CNT_MASK KRAIT_MASK(31, 24) |
| 98 | #define BHS_CNT_DEFAULT 64 |
| 99 | |
| 100 | #define CLK_SRC_SEL_BIT_POS 15 |
| 101 | #define CLK_SRC_SEL_MASK KRAIT_MASK(15, 15) |
| 102 | #define CLK_SRC_DEFAULT 0 |
| 103 | |
| 104 | #define LDO_PWR_DWN_BIT_POS 16 |
| 105 | #define LDO_PWR_DWN_MASK KRAIT_MASK(21, 16) |
| 106 | |
| 107 | #define LDO_BYP_BIT_POS 8 |
| 108 | #define LDO_BYP_MASK KRAIT_MASK(13, 8) |
| 109 | |
| 110 | #define BHS_SEG_EN_BIT_POS 1 |
| 111 | #define BHS_SEG_EN_MASK KRAIT_MASK(6, 1) |
| 112 | #define BHS_SEG_EN_DEFAULT 0x3F |
| 113 | |
| 114 | #define BHS_EN_BIT_POS 0 |
| 115 | #define BHS_EN_MASK KRAIT_MASK(0, 0) |
| 116 | |
| 117 | /* bit definitions for APC_LDO_VREF_SET register */ |
| 118 | #define VREF_RET_POS 8 |
| 119 | #define VREF_RET_MASK KRAIT_MASK(14, 8) |
| 120 | |
| 121 | #define VREF_LDO_BIT_POS 0 |
| 122 | #define VREF_LDO_MASK KRAIT_MASK(6, 0) |
| 123 | |
Abhijeet Dharmapurikar | 37c47a8 | 2013-01-08 18:44:21 -0800 | [diff] [blame] | 124 | #define LDO_HDROOM_MIN 50000 |
| 125 | #define LDO_HDROOM_MAX 250000 |
| 126 | |
| 127 | #define LDO_UV_MIN 465000 |
| 128 | #define LDO_UV_MAX 750000 |
| 129 | |
| 130 | #define LDO_TH_MIN 600000 |
| 131 | #define LDO_TH_MAX 900000 |
| 132 | |
| 133 | #define LDO_DELTA_MIN 10000 |
| 134 | #define LDO_DELTA_MAX 100000 |
| 135 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 136 | /** |
| 137 | * struct pmic_gang_vreg - |
| 138 | * @name: the string used to represent the gang |
| 139 | * @pmic_vmax_uV: the current pmic gang voltage |
| 140 | * @pmic_phase_count: the number of phases turned on in the gang |
| 141 | * @krait_power_vregs: a list of krait consumers this gang supplies to |
| 142 | * @krait_power_vregs_lock: lock to prevent simultaneous access to the list |
| 143 | * and its nodes. This needs to be taken by each |
| 144 | * regulator's callback functions to prevent |
| 145 | * simultaneous updates to the pmic's phase |
| 146 | * voltage. |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 147 | * @apcs_gcc_base: virtual address of the APCS GCC registers |
| 148 | * @manage_phases: begin phase control |
| 149 | * @pfm_threshold: the sum of coefficients below which PFM can be |
| 150 | * enabled |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 151 | */ |
| 152 | struct pmic_gang_vreg { |
| 153 | const char *name; |
| 154 | int pmic_vmax_uV; |
| 155 | int pmic_phase_count; |
| 156 | struct list_head krait_power_vregs; |
| 157 | struct mutex krait_power_vregs_lock; |
Abhijeet Dharmapurikar | 6a42ef8 | 2012-10-18 19:37:36 -0700 | [diff] [blame] | 158 | bool pfm_mode; |
Abhijeet Dharmapurikar | 295c4dc | 2013-01-08 12:59:01 -0800 | [diff] [blame] | 159 | int pmic_min_uV_for_retention; |
| 160 | bool retention_enabled; |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 161 | bool use_phase_switching; |
Stepan Moskovchenko | 7663b78 | 2013-03-08 20:13:06 -0800 | [diff] [blame] | 162 | void __iomem *apcs_gcc_base; |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 163 | bool manage_phases; |
| 164 | int pfm_threshold; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | static struct pmic_gang_vreg *the_gang; |
| 168 | |
| 169 | enum krait_supply_mode { |
| 170 | HS_MODE = REGULATOR_MODE_NORMAL, |
| 171 | LDO_MODE = REGULATOR_MODE_IDLE, |
| 172 | }; |
| 173 | |
| 174 | struct krait_power_vreg { |
| 175 | struct list_head link; |
| 176 | struct regulator_desc desc; |
| 177 | struct regulator_dev *rdev; |
| 178 | const char *name; |
| 179 | struct pmic_gang_vreg *pvreg; |
| 180 | int uV; |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 181 | int load; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 182 | enum krait_supply_mode mode; |
| 183 | void __iomem *reg_base; |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 184 | void __iomem *mdd_base; |
Abhijeet Dharmapurikar | 34f0d64 | 2012-10-25 12:35:23 -0700 | [diff] [blame] | 185 | int ldo_default_uV; |
| 186 | int retention_uV; |
| 187 | int headroom_uV; |
| 188 | int ldo_threshold_uV; |
Abhijeet Dharmapurikar | 2269db0 | 2012-12-05 15:49:20 -0800 | [diff] [blame] | 189 | int ldo_delta_uV; |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 190 | int cpu_num; |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 191 | int coeff1; |
| 192 | int coeff2; |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 193 | bool online; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 196 | DEFINE_PER_CPU(struct krait_power_vreg *, krait_vregs); |
| 197 | |
Abhijeet Dharmapurikar | 3ff7cba | 2012-10-10 17:48:29 -0700 | [diff] [blame] | 198 | static u32 version; |
| 199 | |
Abhijeet Dharmapurikar | 37c47a8 | 2013-01-08 18:44:21 -0800 | [diff] [blame] | 200 | static int is_between(int left, int right, int value) |
| 201 | { |
| 202 | if (left >= right && left >= value && value >= right) |
| 203 | return 1; |
| 204 | if (left <= right && left <= value && value <= right) |
| 205 | return 1; |
| 206 | return 0; |
| 207 | } |
| 208 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 209 | static void krait_masked_write(struct krait_power_vreg *kvreg, |
| 210 | int reg, uint32_t mask, uint32_t val) |
| 211 | { |
| 212 | uint32_t reg_val; |
| 213 | |
| 214 | reg_val = readl_relaxed(kvreg->reg_base + reg); |
| 215 | reg_val &= ~mask; |
| 216 | reg_val |= (val & mask); |
| 217 | writel_relaxed(reg_val, kvreg->reg_base + reg); |
| 218 | |
| 219 | /* |
| 220 | * Barrier to ensure that the reads and writes from |
| 221 | * other regulator regions (they are 1k apart) execute in |
| 222 | * order to the above write. |
| 223 | */ |
| 224 | mb(); |
| 225 | } |
| 226 | |
Abhijeet Dharmapurikar | 37c47a8 | 2013-01-08 18:44:21 -0800 | [diff] [blame] | 227 | static int get_krait_retention_ldo_uv(struct krait_power_vreg *kvreg) |
| 228 | { |
| 229 | uint32_t reg_val; |
| 230 | int uV; |
| 231 | |
| 232 | reg_val = readl_relaxed(kvreg->reg_base + APC_LDO_VREF_SET); |
| 233 | reg_val &= VREF_RET_MASK; |
| 234 | reg_val >>= VREF_RET_POS; |
| 235 | |
| 236 | if (reg_val == 0) |
| 237 | uV = 0; |
| 238 | else |
| 239 | uV = KRAIT_LDO_VOLTAGE_OFFSET + reg_val * KRAIT_LDO_STEP; |
| 240 | |
| 241 | return uV; |
| 242 | } |
| 243 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 244 | static int get_krait_ldo_uv(struct krait_power_vreg *kvreg) |
| 245 | { |
| 246 | uint32_t reg_val; |
| 247 | int uV; |
| 248 | |
| 249 | reg_val = readl_relaxed(kvreg->reg_base + APC_LDO_VREF_SET); |
| 250 | reg_val &= VREF_LDO_MASK; |
| 251 | reg_val >>= VREF_LDO_BIT_POS; |
| 252 | |
| 253 | if (reg_val == 0) |
| 254 | uV = 0; |
| 255 | else |
| 256 | uV = KRAIT_LDO_VOLTAGE_OFFSET + reg_val * KRAIT_LDO_STEP; |
| 257 | |
| 258 | return uV; |
| 259 | } |
| 260 | |
Abhijeet Dharmapurikar | 34f0d64 | 2012-10-25 12:35:23 -0700 | [diff] [blame] | 261 | static int set_krait_retention_uv(struct krait_power_vreg *kvreg, int uV) |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 262 | { |
| 263 | uint32_t reg_val; |
| 264 | |
Abhijeet Dharmapurikar | 34f0d64 | 2012-10-25 12:35:23 -0700 | [diff] [blame] | 265 | reg_val = DIV_ROUND_UP(uV - KRAIT_LDO_VOLTAGE_OFFSET, KRAIT_LDO_STEP); |
| 266 | krait_masked_write(kvreg, APC_LDO_VREF_SET, VREF_RET_MASK, |
| 267 | reg_val << VREF_RET_POS); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int set_krait_ldo_uv(struct krait_power_vreg *kvreg, int uV) |
| 273 | { |
| 274 | uint32_t reg_val; |
| 275 | |
| 276 | reg_val = DIV_ROUND_UP(uV - KRAIT_LDO_VOLTAGE_OFFSET, KRAIT_LDO_STEP); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 277 | krait_masked_write(kvreg, APC_LDO_VREF_SET, VREF_LDO_MASK, |
| 278 | reg_val << VREF_LDO_BIT_POS); |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 283 | static int __krait_power_mdd_enable(struct krait_power_vreg *kvreg, bool on) |
| 284 | { |
| 285 | if (on) { |
| 286 | writel_relaxed(0x00000002, kvreg->mdd_base + MDD_MODE); |
| 287 | /* complete the above write before the delay */ |
| 288 | mb(); |
| 289 | udelay(MDD_SETTLING_DELAY_US); |
| 290 | } else { |
| 291 | writel_relaxed(0x00000000, kvreg->mdd_base + MDD_MODE); |
| 292 | /* |
| 293 | * complete the above write before other accesses |
| 294 | * to krait regulator |
| 295 | */ |
| 296 | mb(); |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 301 | #define COEFF2_UV_THRESHOLD 850000 |
| 302 | static int get_coeff2(int krait_uV) |
| 303 | { |
| 304 | int coeff2 = 0; |
| 305 | int krait_mV = krait_uV / 1000; |
| 306 | |
| 307 | if (krait_uV <= COEFF2_UV_THRESHOLD) |
| 308 | coeff2 = (612229 * krait_mV) / 1000 - 211258; |
| 309 | else |
| 310 | coeff2 = (892564 * krait_mV) / 1000 - 449543; |
| 311 | |
| 312 | return coeff2; |
| 313 | } |
| 314 | |
| 315 | static int get_coeff1(int actual_uV, int requested_uV, int load) |
| 316 | { |
| 317 | int ratio = actual_uV * 1000 / requested_uV; |
| 318 | int coeff1 = 330 * load + (load * 673 * ratio / 1000); |
| 319 | |
| 320 | return coeff1; |
| 321 | } |
| 322 | |
| 323 | static int get_coeff_total(struct krait_power_vreg *from) |
| 324 | { |
| 325 | int coeff_total = 0; |
| 326 | struct krait_power_vreg *kvreg; |
| 327 | struct pmic_gang_vreg *pvreg = from->pvreg; |
| 328 | |
| 329 | list_for_each_entry(kvreg, &pvreg->krait_power_vregs, link) { |
| 330 | if (!kvreg->online) |
| 331 | continue; |
| 332 | |
| 333 | if (kvreg->mode == LDO_MODE) { |
| 334 | kvreg->coeff1 = |
| 335 | get_coeff1(kvreg->uV - kvreg->ldo_delta_uV, |
| 336 | kvreg->uV, kvreg->load); |
| 337 | kvreg->coeff2 = |
| 338 | get_coeff2(kvreg->uV - kvreg->ldo_delta_uV); |
| 339 | } else { |
| 340 | kvreg->coeff1 = |
| 341 | get_coeff1(pvreg->pmic_vmax_uV, |
| 342 | kvreg->uV, kvreg->load); |
| 343 | kvreg->coeff2 = get_coeff2(pvreg->pmic_vmax_uV); |
| 344 | } |
| 345 | coeff_total += kvreg->coeff1 + kvreg->coeff2; |
| 346 | } |
| 347 | |
| 348 | return coeff_total; |
| 349 | } |
| 350 | |
| 351 | static int set_pmic_gang_phases(struct pmic_gang_vreg *pvreg, int phase_count) |
| 352 | { |
| 353 | pr_debug("programming phase_count = %d\n", phase_count); |
| 354 | if (pvreg->use_phase_switching) |
| 355 | /* |
| 356 | * note the PMIC sets the phase count to one more than |
| 357 | * the value in the register - hence subtract 1 from it |
| 358 | */ |
| 359 | return msm_spm_apcs_set_phase(phase_count - 1); |
| 360 | else |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int num_online(struct pmic_gang_vreg *pvreg) |
| 365 | { |
| 366 | int online_total = 0; |
| 367 | struct krait_power_vreg *kvreg; |
| 368 | |
| 369 | list_for_each_entry(kvreg, &pvreg->krait_power_vregs, link) { |
| 370 | if (kvreg->online) |
| 371 | online_total++; |
| 372 | } |
| 373 | return online_total; |
| 374 | } |
| 375 | |
| 376 | #define PMIC_FTS_MODE_PFM 0x00 |
| 377 | #define PMIC_FTS_MODE_PWM 0x80 |
| 378 | #define ONE_PHASE_COEFF 1000000 |
| 379 | #define TWO_PHASE_COEFF 2000000 |
| 380 | |
| 381 | #define PHASE_SETTLING_TIME_US 10 |
| 382 | static unsigned int pmic_gang_set_phases(struct krait_power_vreg *from, |
| 383 | int coeff_total) |
| 384 | { |
| 385 | struct pmic_gang_vreg *pvreg = from->pvreg; |
| 386 | int phase_count; |
| 387 | int rc = 0; |
| 388 | int n_online = num_online(pvreg); |
| 389 | |
Willie Ruan | 421ad42 | 2013-04-17 12:26:14 -0700 | [diff] [blame^] | 390 | if (pvreg->manage_phases == false) |
| 391 | return 0; |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 392 | |
| 393 | /* First check if the coeff is low for PFM mode */ |
| 394 | if (coeff_total < pvreg->pfm_threshold && n_online == 1) { |
| 395 | if (!pvreg->pfm_mode) { |
| 396 | rc = msm_spm_enable_fts_lpm(PMIC_FTS_MODE_PFM); |
| 397 | if (rc) { |
| 398 | pr_err("%s PFM en failed coeff_t %d rc = %d\n", |
| 399 | from->name, coeff_total, rc); |
| 400 | return rc; |
| 401 | } else { |
| 402 | pvreg->pfm_mode = true; |
| 403 | } |
| 404 | } |
| 405 | return rc; |
| 406 | } |
| 407 | |
| 408 | /* coeff is high switch to PWM mode before changing phases */ |
| 409 | if (pvreg->pfm_mode) { |
| 410 | rc = msm_spm_enable_fts_lpm(PMIC_FTS_MODE_PWM); |
| 411 | if (rc) { |
| 412 | pr_err("%s PFM exit failed load %d rc = %d\n", |
| 413 | from->name, coeff_total, rc); |
| 414 | return rc; |
| 415 | } else { |
| 416 | pvreg->pfm_mode = false; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /* calculate phases */ |
| 421 | if (coeff_total < ONE_PHASE_COEFF) |
| 422 | phase_count = 1; |
| 423 | else if (coeff_total < TWO_PHASE_COEFF) |
| 424 | phase_count = 2; |
| 425 | else |
| 426 | phase_count = 4; |
| 427 | |
| 428 | /* don't increase the phase count higher than number of online cpus */ |
| 429 | if (phase_count > n_online) |
| 430 | phase_count = n_online; |
| 431 | |
| 432 | if (phase_count != pvreg->pmic_phase_count) { |
| 433 | rc = set_pmic_gang_phases(pvreg, phase_count); |
| 434 | if (rc < 0) { |
| 435 | pr_err("%s failed set phase %d rc = %d\n", |
| 436 | from->name, phase_count, rc); |
| 437 | return rc; |
| 438 | } |
| 439 | |
| 440 | /* complete the writes before the delay */ |
| 441 | mb(); |
| 442 | |
| 443 | /* |
| 444 | * delay until the phases are settled when |
| 445 | * the count is raised |
| 446 | */ |
| 447 | if (phase_count > pvreg->pmic_phase_count) |
| 448 | udelay(PHASE_SETTLING_TIME_US); |
| 449 | |
| 450 | pvreg->pmic_phase_count = phase_count; |
| 451 | } |
| 452 | |
| 453 | return rc; |
| 454 | } |
| 455 | |
| 456 | static unsigned int _get_optimum_mode(struct regulator_dev *rdev, |
| 457 | int input_uV, int output_uV, int load) |
| 458 | { |
| 459 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 460 | int coeff_total; |
| 461 | int rc; |
| 462 | |
| 463 | coeff_total = get_coeff_total(kvreg); |
| 464 | |
| 465 | rc = pmic_gang_set_phases(kvreg, coeff_total); |
| 466 | if (rc < 0) { |
| 467 | dev_err(&rdev->dev, "%s failed set mode %d rc = %d\n", |
| 468 | kvreg->name, coeff_total, rc); |
| 469 | } |
| 470 | |
| 471 | return kvreg->mode; |
| 472 | } |
| 473 | |
| 474 | static unsigned int krait_power_get_optimum_mode(struct regulator_dev *rdev, |
| 475 | int input_uV, int output_uV, int load_uA) |
| 476 | { |
| 477 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 478 | struct pmic_gang_vreg *pvreg = kvreg->pvreg; |
| 479 | int rc; |
| 480 | |
| 481 | mutex_lock(&pvreg->krait_power_vregs_lock); |
| 482 | kvreg->load = load_uA; |
| 483 | if (!kvreg->online) { |
| 484 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
| 485 | return kvreg->mode; |
| 486 | } |
| 487 | |
| 488 | rc = _get_optimum_mode(rdev, input_uV, output_uV, load_uA); |
| 489 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
| 490 | |
| 491 | return rc; |
| 492 | } |
| 493 | |
| 494 | static int krait_power_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 495 | { |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static unsigned int krait_power_get_mode(struct regulator_dev *rdev) |
| 500 | { |
| 501 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 502 | |
| 503 | return kvreg->mode; |
| 504 | } |
| 505 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 506 | static int switch_to_using_hs(struct krait_power_vreg *kvreg) |
| 507 | { |
| 508 | if (kvreg->mode == HS_MODE) |
| 509 | return 0; |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 510 | /* enable bhs */ |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 511 | krait_masked_write(kvreg, APC_PWR_GATE_CTL, |
| 512 | BHS_SEG_EN_MASK | BHS_EN_MASK, |
| 513 | BHS_SEG_EN_DEFAULT << BHS_SEG_EN_BIT_POS | BHS_EN_MASK); |
| 514 | |
| 515 | /* complete the above write before the delay */ |
| 516 | mb(); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | * wait for the bhs to settle - note that |
| 520 | * after the voltage has settled both BHS and LDO are supplying power |
| 521 | * to the krait. This avoids glitches during switching |
| 522 | */ |
| 523 | udelay(BHS_SETTLING_DELAY_US); |
| 524 | |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 525 | /* |
| 526 | * enable ldo bypass - the krait is powered still by LDO since |
| 527 | * LDO is enabled |
| 528 | */ |
| 529 | krait_masked_write(kvreg, APC_PWR_GATE_CTL, LDO_BYP_MASK, LDO_BYP_MASK); |
| 530 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 531 | /* disable ldo - only the BHS provides voltage to the cpu after this */ |
| 532 | krait_masked_write(kvreg, APC_PWR_GATE_CTL, |
| 533 | LDO_PWR_DWN_MASK, LDO_PWR_DWN_MASK); |
| 534 | |
| 535 | kvreg->mode = HS_MODE; |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 536 | pr_debug("%s using BHS\n", kvreg->name); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static int switch_to_using_ldo(struct krait_power_vreg *kvreg) |
| 541 | { |
Abhijeet Dharmapurikar | 2269db0 | 2012-12-05 15:49:20 -0800 | [diff] [blame] | 542 | if (kvreg->mode == LDO_MODE |
| 543 | && get_krait_ldo_uv(kvreg) == kvreg->uV - kvreg->ldo_delta_uV) |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 544 | return 0; |
| 545 | |
| 546 | /* |
| 547 | * if the krait is in ldo mode and a voltage change is requested on the |
| 548 | * ldo switch to using hs before changing ldo voltage |
| 549 | */ |
| 550 | if (kvreg->mode == LDO_MODE) |
| 551 | switch_to_using_hs(kvreg); |
| 552 | |
Abhijeet Dharmapurikar | 2269db0 | 2012-12-05 15:49:20 -0800 | [diff] [blame] | 553 | set_krait_ldo_uv(kvreg, kvreg->uV - kvreg->ldo_delta_uV); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 554 | |
| 555 | /* |
| 556 | * enable ldo - note that both LDO and BHS are are supplying voltage to |
| 557 | * the cpu after this. This avoids glitches during switching from BHS |
| 558 | * to LDO. |
| 559 | */ |
| 560 | krait_masked_write(kvreg, APC_PWR_GATE_CTL, LDO_PWR_DWN_MASK, 0); |
| 561 | |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 562 | /* complete the writes before the delay */ |
| 563 | mb(); |
| 564 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 565 | /* wait for the ldo to settle */ |
| 566 | udelay(LDO_SETTLING_DELAY_US); |
| 567 | |
| 568 | /* |
| 569 | * disable BHS and disable LDO bypass seperate from enabling |
| 570 | * the LDO above. |
| 571 | */ |
| 572 | krait_masked_write(kvreg, APC_PWR_GATE_CTL, |
| 573 | BHS_EN_MASK | LDO_BYP_MASK, 0); |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 574 | krait_masked_write(kvreg, APC_PWR_GATE_CTL, BHS_SEG_EN_MASK, 0); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 575 | |
| 576 | kvreg->mode = LDO_MODE; |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 577 | pr_debug("%s using LDO\n", kvreg->name); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 578 | return 0; |
| 579 | } |
| 580 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 581 | static int set_pmic_gang_voltage(struct pmic_gang_vreg *pvreg, int uV) |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 582 | { |
| 583 | int setpoint; |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 584 | int rc; |
| 585 | |
| 586 | if (pvreg->pmic_vmax_uV == uV) |
| 587 | return 0; |
| 588 | |
| 589 | pr_debug("%d\n", uV); |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 590 | |
| 591 | if (uV < PMIC_VOLTAGE_MIN) { |
| 592 | pr_err("requested %d < %d, restricting it to %d\n", |
| 593 | uV, PMIC_VOLTAGE_MIN, PMIC_VOLTAGE_MIN); |
| 594 | uV = PMIC_VOLTAGE_MIN; |
| 595 | } |
| 596 | if (uV > PMIC_VOLTAGE_MAX) { |
| 597 | pr_err("requested %d > %d, restricting it to %d\n", |
| 598 | uV, PMIC_VOLTAGE_MAX, PMIC_VOLTAGE_MAX); |
| 599 | uV = PMIC_VOLTAGE_MAX; |
| 600 | } |
| 601 | |
Abhijeet Dharmapurikar | 295c4dc | 2013-01-08 12:59:01 -0800 | [diff] [blame] | 602 | if (uV < pvreg->pmic_min_uV_for_retention) { |
| 603 | if (pvreg->retention_enabled) { |
| 604 | pr_debug("Disabling Retention pmic = %duV, pmic_min_uV_for_retention = %duV", |
| 605 | uV, pvreg->pmic_min_uV_for_retention); |
| 606 | msm_pm_enable_retention(false); |
| 607 | pvreg->retention_enabled = false; |
| 608 | } |
| 609 | } else { |
| 610 | if (!pvreg->retention_enabled) { |
| 611 | pr_debug("Enabling Retention pmic = %duV, pmic_min_uV_for_retention = %duV", |
| 612 | uV, pvreg->pmic_min_uV_for_retention); |
| 613 | msm_pm_enable_retention(true); |
| 614 | pvreg->retention_enabled = true; |
| 615 | } |
| 616 | } |
| 617 | |
Abhijeet Dharmapurikar | d4f38da | 2012-08-08 21:42:06 -0700 | [diff] [blame] | 618 | setpoint = DIV_ROUND_UP(uV, LV_RANGE_STEP); |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 619 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 620 | rc = msm_spm_apcs_set_vdd(setpoint); |
| 621 | if (rc < 0) |
| 622 | pr_err("could not set %duV setpt = 0x%x rc = %d\n", |
| 623 | uV, setpoint, rc); |
| 624 | else |
| 625 | pvreg->pmic_vmax_uV = uV; |
| 626 | |
| 627 | return rc; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 630 | static int configure_ldo_or_hs_all(struct krait_power_vreg *from, int vmax) |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 631 | { |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 632 | struct pmic_gang_vreg *pvreg = from->pvreg; |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 633 | struct krait_power_vreg *kvreg; |
| 634 | int rc = 0; |
| 635 | |
| 636 | list_for_each_entry(kvreg, &pvreg->krait_power_vregs, link) { |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 637 | if (!kvreg->online) |
| 638 | continue; |
Abhijeet Dharmapurikar | 2269db0 | 2012-12-05 15:49:20 -0800 | [diff] [blame] | 639 | if (kvreg->uV <= kvreg->ldo_threshold_uV |
| 640 | && kvreg->uV - kvreg->ldo_delta_uV + kvreg->headroom_uV |
| 641 | <= vmax) { |
| 642 | rc = switch_to_using_ldo(kvreg); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 643 | if (rc < 0) { |
Abhijeet Dharmapurikar | 2269db0 | 2012-12-05 15:49:20 -0800 | [diff] [blame] | 644 | pr_err("could not switch %s to ldo rc = %d\n", |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 645 | kvreg->name, rc); |
| 646 | return rc; |
| 647 | } |
| 648 | } else { |
Abhijeet Dharmapurikar | 2269db0 | 2012-12-05 15:49:20 -0800 | [diff] [blame] | 649 | rc = switch_to_using_hs(kvreg); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 650 | if (rc < 0) { |
Abhijeet Dharmapurikar | 2269db0 | 2012-12-05 15:49:20 -0800 | [diff] [blame] | 651 | pr_err("could not switch %s to hs rc = %d\n", |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 652 | kvreg->name, rc); |
| 653 | return rc; |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | return rc; |
| 659 | } |
| 660 | |
| 661 | #define SLEW_RATE 2994 |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 662 | static int krait_voltage_increase(struct krait_power_vreg *from, |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 663 | int vmax) |
| 664 | { |
| 665 | struct pmic_gang_vreg *pvreg = from->pvreg; |
| 666 | int rc = 0; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 667 | int settling_us; |
| 668 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 669 | /* |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 670 | * since krait voltage is increasing set the gang voltage |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 671 | * prior to changing ldo/hs states of the requesting krait |
| 672 | */ |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 673 | rc = set_pmic_gang_voltage(pvreg, vmax); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 674 | if (rc < 0) { |
| 675 | dev_err(&from->rdev->dev, "%s failed set voltage %d rc = %d\n", |
| 676 | pvreg->name, vmax, rc); |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 677 | return rc; |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 680 | |
| 681 | /* complete the above writes before the delay */ |
| 682 | mb(); |
| 683 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 684 | /* delay until the voltage is settled when it is raised */ |
| 685 | settling_us = DIV_ROUND_UP(vmax - pvreg->pmic_vmax_uV, SLEW_RATE); |
| 686 | udelay(settling_us); |
| 687 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 688 | rc = configure_ldo_or_hs_all(from, vmax); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 689 | if (rc < 0) { |
| 690 | dev_err(&from->rdev->dev, "%s failed ldo/hs conf %d rc = %d\n", |
| 691 | pvreg->name, vmax, rc); |
| 692 | } |
| 693 | |
| 694 | return rc; |
| 695 | } |
| 696 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 697 | static int krait_voltage_decrease(struct krait_power_vreg *from, |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 698 | int vmax) |
| 699 | { |
| 700 | struct pmic_gang_vreg *pvreg = from->pvreg; |
| 701 | int rc = 0; |
| 702 | |
| 703 | /* |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 704 | * since krait voltage is decreasing ldos might get out of their |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 705 | * operating range. Hence configure such kraits to be in hs mode prior |
| 706 | * to setting the pmic gang voltage |
| 707 | */ |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 708 | rc = configure_ldo_or_hs_all(from, vmax); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 709 | if (rc < 0) { |
| 710 | dev_err(&from->rdev->dev, "%s failed ldo/hs conf %d rc = %d\n", |
| 711 | pvreg->name, vmax, rc); |
| 712 | return rc; |
| 713 | } |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 714 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 715 | rc = set_pmic_gang_voltage(pvreg, vmax); |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 716 | if (rc < 0) { |
| 717 | dev_err(&from->rdev->dev, "%s failed set voltage %d rc = %d\n", |
| 718 | pvreg->name, vmax, rc); |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 721 | return rc; |
| 722 | } |
| 723 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 724 | static int krait_power_get_voltage(struct regulator_dev *rdev) |
| 725 | { |
| 726 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 727 | |
| 728 | return kvreg->uV; |
| 729 | } |
| 730 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 731 | static int get_vmax(struct pmic_gang_vreg *pvreg) |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 732 | { |
| 733 | int vmax = 0; |
| 734 | int v; |
| 735 | struct krait_power_vreg *kvreg; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 736 | |
| 737 | list_for_each_entry(kvreg, &pvreg->krait_power_vregs, link) { |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 738 | if (!kvreg->online) |
| 739 | continue; |
| 740 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 741 | v = kvreg->uV; |
| 742 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 743 | if (vmax < v) |
| 744 | vmax = v; |
| 745 | } |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 746 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 747 | return vmax; |
| 748 | } |
| 749 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 750 | #define ROUND_UP_VOLTAGE(v, res) (DIV_ROUND_UP(v, res) * res) |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 751 | static int _set_voltage(struct regulator_dev *rdev, |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 752 | int orig_krait_uV, int requested_uV) |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 753 | { |
| 754 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 755 | struct pmic_gang_vreg *pvreg = kvreg->pvreg; |
| 756 | int rc; |
| 757 | int vmax; |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 758 | int coeff_total; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 759 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 760 | pr_debug("%s: %d to %d\n", kvreg->name, orig_krait_uV, requested_uV); |
| 761 | /* |
| 762 | * Assign the voltage before updating the gang voltage as we iterate |
| 763 | * over all the core voltages and choose HS or LDO for each of them |
| 764 | */ |
| 765 | kvreg->uV = requested_uV; |
| 766 | |
| 767 | vmax = get_vmax(pvreg); |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 768 | |
| 769 | /* round up the pmic voltage as per its resolution */ |
| 770 | vmax = ROUND_UP_VOLTAGE(vmax, LV_RANGE_STEP); |
| 771 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 772 | if (requested_uV > orig_krait_uV) |
| 773 | rc = krait_voltage_increase(kvreg, vmax); |
| 774 | else |
| 775 | rc = krait_voltage_decrease(kvreg, vmax); |
| 776 | |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 777 | if (rc < 0) { |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 778 | dev_err(&rdev->dev, "%s failed to set %duV from %duV rc = %d\n", |
| 779 | kvreg->name, requested_uV, orig_krait_uV, rc); |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 782 | coeff_total = get_coeff_total(kvreg); |
| 783 | /* adjust the phases since coeff2 would have changed */ |
| 784 | rc = pmic_gang_set_phases(kvreg, coeff_total); |
| 785 | |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 786 | return rc; |
| 787 | } |
| 788 | |
| 789 | static int krait_power_set_voltage(struct regulator_dev *rdev, |
| 790 | int min_uV, int max_uV, unsigned *selector) |
| 791 | { |
| 792 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 793 | struct pmic_gang_vreg *pvreg = kvreg->pvreg; |
| 794 | int rc; |
| 795 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 796 | /* |
| 797 | * if the voltage requested is below LDO_THRESHOLD this cpu could |
| 798 | * switch to LDO mode. Hence round the voltage as per the LDO |
| 799 | * resolution |
| 800 | */ |
Abhijeet Dharmapurikar | 34f0d64 | 2012-10-25 12:35:23 -0700 | [diff] [blame] | 801 | if (min_uV < kvreg->ldo_threshold_uV) { |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 802 | if (min_uV < KRAIT_LDO_VOLTAGE_MIN) |
| 803 | min_uV = KRAIT_LDO_VOLTAGE_MIN; |
| 804 | min_uV = ROUND_UP_VOLTAGE(min_uV, KRAIT_LDO_STEP); |
| 805 | } |
| 806 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 807 | mutex_lock(&pvreg->krait_power_vregs_lock); |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 808 | if (!kvreg->online) { |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 809 | kvreg->uV = min_uV; |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 810 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
| 811 | return 0; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 812 | } |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 813 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 814 | rc = _set_voltage(rdev, kvreg->uV, min_uV); |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 815 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 816 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 817 | return rc; |
| 818 | } |
| 819 | |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 820 | static int krait_power_is_enabled(struct regulator_dev *rdev) |
| 821 | { |
| 822 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 823 | |
| 824 | return kvreg->online; |
| 825 | } |
| 826 | |
| 827 | static int krait_power_enable(struct regulator_dev *rdev) |
| 828 | { |
| 829 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 830 | struct pmic_gang_vreg *pvreg = kvreg->pvreg; |
| 831 | int rc; |
| 832 | |
| 833 | mutex_lock(&pvreg->krait_power_vregs_lock); |
Abhijeet Dharmapurikar | c8605a6 | 2013-03-11 21:24:25 -0700 | [diff] [blame] | 834 | __krait_power_mdd_enable(kvreg, true); |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 835 | kvreg->online = true; |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 836 | rc = _get_optimum_mode(rdev, kvreg->uV, kvreg->uV, kvreg->load); |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 837 | if (rc < 0) |
| 838 | goto en_err; |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 839 | /* |
| 840 | * since the core is being enabled, behave as if it is increasing |
| 841 | * the core voltage |
| 842 | */ |
| 843 | rc = _set_voltage(rdev, 0, kvreg->uV); |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 844 | en_err: |
| 845 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
| 846 | return rc; |
| 847 | } |
| 848 | |
| 849 | static int krait_power_disable(struct regulator_dev *rdev) |
| 850 | { |
| 851 | struct krait_power_vreg *kvreg = rdev_get_drvdata(rdev); |
| 852 | struct pmic_gang_vreg *pvreg = kvreg->pvreg; |
| 853 | int rc; |
| 854 | |
| 855 | mutex_lock(&pvreg->krait_power_vregs_lock); |
| 856 | kvreg->online = false; |
| 857 | |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 858 | rc = _get_optimum_mode(rdev, kvreg->uV, kvreg->uV, kvreg->load); |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 859 | if (rc < 0) |
| 860 | goto dis_err; |
| 861 | |
Abhijeet Dharmapurikar | fe1306b | 2012-11-15 18:09:31 -0800 | [diff] [blame] | 862 | rc = _set_voltage(rdev, kvreg->uV, kvreg->uV); |
Abhijeet Dharmapurikar | c8605a6 | 2013-03-11 21:24:25 -0700 | [diff] [blame] | 863 | __krait_power_mdd_enable(kvreg, false); |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 864 | dis_err: |
| 865 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
| 866 | return rc; |
| 867 | } |
| 868 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 869 | static struct regulator_ops krait_power_ops = { |
| 870 | .get_voltage = krait_power_get_voltage, |
| 871 | .set_voltage = krait_power_set_voltage, |
| 872 | .get_optimum_mode = krait_power_get_optimum_mode, |
| 873 | .set_mode = krait_power_set_mode, |
| 874 | .get_mode = krait_power_get_mode, |
Michael Bohan | b5d1cdb | 2012-11-08 18:09:52 -0800 | [diff] [blame] | 875 | .enable = krait_power_enable, |
| 876 | .disable = krait_power_disable, |
| 877 | .is_enabled = krait_power_is_enabled, |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 878 | }; |
| 879 | |
Abhijeet Dharmapurikar | 37c47a8 | 2013-01-08 18:44:21 -0800 | [diff] [blame] | 880 | static struct dentry *dent; |
| 881 | static int get_retention_dbg_uV(void *data, u64 *val) |
| 882 | { |
| 883 | struct pmic_gang_vreg *pvreg = data; |
| 884 | struct krait_power_vreg *kvreg; |
| 885 | |
| 886 | mutex_lock(&pvreg->krait_power_vregs_lock); |
| 887 | if (!list_empty(&pvreg->krait_power_vregs)) { |
| 888 | /* return the retention voltage on just the first cpu */ |
| 889 | kvreg = list_entry((&pvreg->krait_power_vregs)->next, |
| 890 | typeof(*kvreg), link); |
| 891 | *val = get_krait_retention_ldo_uv(kvreg); |
| 892 | } |
| 893 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | static int set_retention_dbg_uV(void *data, u64 val) |
| 898 | { |
| 899 | struct pmic_gang_vreg *pvreg = data; |
| 900 | struct krait_power_vreg *kvreg; |
| 901 | int retention_uV = val; |
| 902 | |
| 903 | if (!is_between(LDO_UV_MIN, LDO_UV_MAX, retention_uV)) |
| 904 | return -EINVAL; |
| 905 | |
| 906 | mutex_lock(&pvreg->krait_power_vregs_lock); |
| 907 | list_for_each_entry(kvreg, &pvreg->krait_power_vregs, link) { |
| 908 | kvreg->retention_uV = retention_uV; |
| 909 | set_krait_retention_uv(kvreg, retention_uV); |
| 910 | } |
| 911 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
| 912 | return 0; |
| 913 | } |
| 914 | DEFINE_SIMPLE_ATTRIBUTE(retention_fops, |
| 915 | get_retention_dbg_uV, set_retention_dbg_uV, "%llu\n"); |
| 916 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 917 | static void kvreg_hw_init(struct krait_power_vreg *kvreg) |
| 918 | { |
| 919 | /* |
| 920 | * bhs_cnt value sets the ramp-up time from power collapse, |
| 921 | * initialize the ramp up time |
| 922 | */ |
Abhijeet Dharmapurikar | 34f0d64 | 2012-10-25 12:35:23 -0700 | [diff] [blame] | 923 | set_krait_retention_uv(kvreg, kvreg->retention_uV); |
| 924 | set_krait_ldo_uv(kvreg, kvreg->ldo_default_uV); |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 925 | |
| 926 | /* setup the bandgap that configures the reference to the LDO */ |
| 927 | writel_relaxed(0x00000190, kvreg->mdd_base + MDD_CONFIG_CTL); |
Abhijeet Dharmapurikar | c8605a6 | 2013-03-11 21:24:25 -0700 | [diff] [blame] | 928 | /* Enable MDD */ |
| 929 | writel_relaxed(0x00000002, kvreg->mdd_base + MDD_MODE); |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 930 | mb(); |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Stepan Moskovchenko | 7663b78 | 2013-03-08 20:13:06 -0800 | [diff] [blame] | 933 | static void glb_init(void __iomem *apcs_gcc_base) |
Abhijeet Dharmapurikar | 3ff7cba | 2012-10-10 17:48:29 -0700 | [diff] [blame] | 934 | { |
| 935 | /* configure bi-modal switch */ |
Stepan Moskovchenko | 7663b78 | 2013-03-08 20:13:06 -0800 | [diff] [blame] | 936 | writel_relaxed(0x0008736E, apcs_gcc_base + PWR_GATE_CONFIG); |
Abhijeet Dharmapurikar | 3ff7cba | 2012-10-10 17:48:29 -0700 | [diff] [blame] | 937 | /* read kpss version */ |
Stepan Moskovchenko | 7663b78 | 2013-03-08 20:13:06 -0800 | [diff] [blame] | 938 | version = readl_relaxed(apcs_gcc_base + VERSION); |
Abhijeet Dharmapurikar | 3ff7cba | 2012-10-10 17:48:29 -0700 | [diff] [blame] | 939 | pr_debug("version= 0x%x\n", version); |
| 940 | } |
| 941 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 942 | static int __devinit krait_power_probe(struct platform_device *pdev) |
| 943 | { |
| 944 | struct krait_power_vreg *kvreg; |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 945 | struct resource *res, *res_mdd; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 946 | struct regulator_init_data *init_data = pdev->dev.platform_data; |
| 947 | int rc = 0; |
Abhijeet Dharmapurikar | 34f0d64 | 2012-10-25 12:35:23 -0700 | [diff] [blame] | 948 | int headroom_uV, retention_uV, ldo_default_uV, ldo_threshold_uV; |
Abhijeet Dharmapurikar | 2269db0 | 2012-12-05 15:49:20 -0800 | [diff] [blame] | 949 | int ldo_delta_uV; |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 950 | int cpu_num; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 951 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 952 | if (pdev->dev.of_node) { |
| 953 | /* Get init_data from device tree. */ |
| 954 | init_data = of_get_regulator_init_data(&pdev->dev, |
| 955 | pdev->dev.of_node); |
| 956 | init_data->constraints.valid_ops_mask |
| 957 | |= REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_DRMS |
| 958 | | REGULATOR_CHANGE_MODE; |
| 959 | init_data->constraints.valid_modes_mask |
| 960 | |= REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE |
| 961 | | REGULATOR_MODE_FAST; |
| 962 | init_data->constraints.input_uV = init_data->constraints.max_uV; |
Abhijeet Dharmapurikar | 34f0d64 | 2012-10-25 12:35:23 -0700 | [diff] [blame] | 963 | rc = of_property_read_u32(pdev->dev.of_node, |
| 964 | "qcom,headroom-voltage", |
| 965 | &headroom_uV); |
| 966 | if (rc < 0) { |
| 967 | pr_err("headroom-voltage missing rc=%d\n", rc); |
| 968 | return rc; |
| 969 | } |
| 970 | if (!is_between(LDO_HDROOM_MIN, LDO_HDROOM_MAX, headroom_uV)) { |
| 971 | pr_err("bad headroom-voltage = %d specified\n", |
| 972 | headroom_uV); |
| 973 | return -EINVAL; |
| 974 | } |
| 975 | |
| 976 | rc = of_property_read_u32(pdev->dev.of_node, |
| 977 | "qcom,retention-voltage", |
| 978 | &retention_uV); |
| 979 | if (rc < 0) { |
| 980 | pr_err("retention-voltage missing rc=%d\n", rc); |
| 981 | return rc; |
| 982 | } |
| 983 | if (!is_between(LDO_UV_MIN, LDO_UV_MAX, retention_uV)) { |
| 984 | pr_err("bad retention-voltage = %d specified\n", |
| 985 | retention_uV); |
| 986 | return -EINVAL; |
| 987 | } |
| 988 | |
| 989 | rc = of_property_read_u32(pdev->dev.of_node, |
| 990 | "qcom,ldo-default-voltage", |
| 991 | &ldo_default_uV); |
| 992 | if (rc < 0) { |
| 993 | pr_err("ldo-default-voltage missing rc=%d\n", rc); |
| 994 | return rc; |
| 995 | } |
| 996 | if (!is_between(LDO_UV_MIN, LDO_UV_MAX, ldo_default_uV)) { |
| 997 | pr_err("bad ldo-default-voltage = %d specified\n", |
| 998 | ldo_default_uV); |
| 999 | return -EINVAL; |
| 1000 | } |
| 1001 | |
| 1002 | rc = of_property_read_u32(pdev->dev.of_node, |
| 1003 | "qcom,ldo-threshold-voltage", |
| 1004 | &ldo_threshold_uV); |
| 1005 | if (rc < 0) { |
| 1006 | pr_err("ldo-threshold-voltage missing rc=%d\n", rc); |
| 1007 | return rc; |
| 1008 | } |
| 1009 | if (!is_between(LDO_TH_MIN, LDO_TH_MAX, ldo_threshold_uV)) { |
| 1010 | pr_err("bad ldo-threshold-voltage = %d specified\n", |
| 1011 | ldo_threshold_uV); |
| 1012 | return -EINVAL; |
| 1013 | } |
Abhijeet Dharmapurikar | 2269db0 | 2012-12-05 15:49:20 -0800 | [diff] [blame] | 1014 | |
| 1015 | rc = of_property_read_u32(pdev->dev.of_node, |
| 1016 | "qcom,ldo-delta-voltage", |
| 1017 | &ldo_delta_uV); |
| 1018 | if (rc < 0) { |
| 1019 | pr_err("ldo-delta-voltage missing rc=%d\n", rc); |
| 1020 | return rc; |
| 1021 | } |
| 1022 | if (!is_between(LDO_DELTA_MIN, LDO_DELTA_MAX, ldo_delta_uV)) { |
| 1023 | pr_err("bad ldo-delta-voltage = %d specified\n", |
| 1024 | ldo_delta_uV); |
| 1025 | return -EINVAL; |
| 1026 | } |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 1027 | rc = of_property_read_u32(pdev->dev.of_node, |
| 1028 | "qcom,cpu-num", |
| 1029 | &cpu_num); |
| 1030 | if (cpu_num > num_possible_cpus()) { |
| 1031 | pr_err("bad cpu-num= %d specified\n", cpu_num); |
| 1032 | return -EINVAL; |
| 1033 | } |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | if (!init_data) { |
| 1037 | dev_err(&pdev->dev, "init data required.\n"); |
| 1038 | return -EINVAL; |
| 1039 | } |
| 1040 | |
| 1041 | if (!init_data->constraints.name) { |
| 1042 | dev_err(&pdev->dev, |
| 1043 | "regulator name must be specified in constraints.\n"); |
| 1044 | return -EINVAL; |
| 1045 | } |
| 1046 | |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 1047 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "acs"); |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 1048 | if (!res) { |
| 1049 | dev_err(&pdev->dev, "missing physical register addresses\n"); |
| 1050 | return -EINVAL; |
| 1051 | } |
| 1052 | |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 1053 | res_mdd = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mdd"); |
| 1054 | if (!res_mdd) { |
| 1055 | dev_err(&pdev->dev, "missing mdd register addresses\n"); |
| 1056 | return -EINVAL; |
| 1057 | } |
| 1058 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 1059 | kvreg = devm_kzalloc(&pdev->dev, |
| 1060 | sizeof(struct krait_power_vreg), GFP_KERNEL); |
| 1061 | if (!kvreg) { |
| 1062 | dev_err(&pdev->dev, "kzalloc failed.\n"); |
| 1063 | return -ENOMEM; |
| 1064 | } |
| 1065 | |
| 1066 | kvreg->reg_base = devm_ioremap(&pdev->dev, |
| 1067 | res->start, resource_size(res)); |
| 1068 | |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 1069 | kvreg->mdd_base = devm_ioremap(&pdev->dev, |
| 1070 | res_mdd->start, resource_size(res)); |
| 1071 | |
Abhijeet Dharmapurikar | 34f0d64 | 2012-10-25 12:35:23 -0700 | [diff] [blame] | 1072 | kvreg->pvreg = the_gang; |
| 1073 | kvreg->name = init_data->constraints.name; |
| 1074 | kvreg->desc.name = kvreg->name; |
| 1075 | kvreg->desc.ops = &krait_power_ops; |
| 1076 | kvreg->desc.type = REGULATOR_VOLTAGE; |
| 1077 | kvreg->desc.owner = THIS_MODULE; |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 1078 | kvreg->uV = CORE_VOLTAGE_BOOTUP; |
Abhijeet Dharmapurikar | 34f0d64 | 2012-10-25 12:35:23 -0700 | [diff] [blame] | 1079 | kvreg->mode = HS_MODE; |
| 1080 | kvreg->desc.ops = &krait_power_ops; |
| 1081 | kvreg->headroom_uV = headroom_uV; |
| 1082 | kvreg->retention_uV = retention_uV; |
| 1083 | kvreg->ldo_default_uV = ldo_default_uV; |
| 1084 | kvreg->ldo_threshold_uV = ldo_threshold_uV; |
Abhijeet Dharmapurikar | 2269db0 | 2012-12-05 15:49:20 -0800 | [diff] [blame] | 1085 | kvreg->ldo_delta_uV = ldo_delta_uV; |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 1086 | kvreg->cpu_num = cpu_num; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 1087 | |
| 1088 | platform_set_drvdata(pdev, kvreg); |
| 1089 | |
| 1090 | mutex_lock(&the_gang->krait_power_vregs_lock); |
Abhijeet Dharmapurikar | 295c4dc | 2013-01-08 12:59:01 -0800 | [diff] [blame] | 1091 | the_gang->pmic_min_uV_for_retention |
| 1092 | = min(the_gang->pmic_min_uV_for_retention, |
| 1093 | kvreg->retention_uV + kvreg->headroom_uV); |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 1094 | list_add_tail(&kvreg->link, &the_gang->krait_power_vregs); |
| 1095 | mutex_unlock(&the_gang->krait_power_vregs_lock); |
| 1096 | |
| 1097 | kvreg->rdev = regulator_register(&kvreg->desc, &pdev->dev, init_data, |
| 1098 | kvreg, pdev->dev.of_node); |
| 1099 | if (IS_ERR(kvreg->rdev)) { |
| 1100 | rc = PTR_ERR(kvreg->rdev); |
| 1101 | pr_err("regulator_register failed, rc=%d.\n", rc); |
| 1102 | goto out; |
| 1103 | } |
| 1104 | |
Abhijeet Dharmapurikar | 152a708 | 2012-05-14 20:25:59 -0700 | [diff] [blame] | 1105 | kvreg_hw_init(kvreg); |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 1106 | per_cpu(krait_vregs, cpu_num) = kvreg; |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 1107 | dev_dbg(&pdev->dev, "id=%d, name=%s\n", pdev->id, kvreg->name); |
| 1108 | |
| 1109 | return 0; |
| 1110 | out: |
| 1111 | mutex_lock(&the_gang->krait_power_vregs_lock); |
| 1112 | list_del(&kvreg->link); |
| 1113 | mutex_unlock(&the_gang->krait_power_vregs_lock); |
| 1114 | |
| 1115 | platform_set_drvdata(pdev, NULL); |
| 1116 | return rc; |
| 1117 | } |
| 1118 | |
| 1119 | static int __devexit krait_power_remove(struct platform_device *pdev) |
| 1120 | { |
| 1121 | struct krait_power_vreg *kvreg = platform_get_drvdata(pdev); |
| 1122 | struct pmic_gang_vreg *pvreg = kvreg->pvreg; |
| 1123 | |
| 1124 | mutex_lock(&pvreg->krait_power_vregs_lock); |
| 1125 | list_del(&kvreg->link); |
| 1126 | mutex_unlock(&pvreg->krait_power_vregs_lock); |
| 1127 | |
| 1128 | regulator_unregister(kvreg->rdev); |
| 1129 | platform_set_drvdata(pdev, NULL); |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | static struct of_device_id krait_power_match_table[] = { |
| 1134 | { .compatible = "qcom,krait-regulator", }, |
| 1135 | {} |
| 1136 | }; |
| 1137 | |
| 1138 | static struct platform_driver krait_power_driver = { |
| 1139 | .probe = krait_power_probe, |
| 1140 | .remove = __devexit_p(krait_power_remove), |
| 1141 | .driver = { |
| 1142 | .name = KRAIT_REGULATOR_DRIVER_NAME, |
| 1143 | .of_match_table = krait_power_match_table, |
| 1144 | .owner = THIS_MODULE, |
| 1145 | }, |
| 1146 | }; |
| 1147 | |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 1148 | static struct of_device_id krait_pdn_match_table[] = { |
| 1149 | { .compatible = "qcom,krait-pdn", }, |
| 1150 | {} |
| 1151 | }; |
| 1152 | |
Abhijeet Dharmapurikar | c8605a6 | 2013-03-11 21:24:25 -0700 | [diff] [blame] | 1153 | static int boot_cpu_mdd_off(void) |
| 1154 | { |
| 1155 | struct krait_power_vreg *kvreg = per_cpu(krait_vregs, 0); |
| 1156 | |
| 1157 | __krait_power_mdd_enable(kvreg, false); |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | static void boot_cpu_mdd_on(void) |
| 1162 | { |
| 1163 | struct krait_power_vreg *kvreg = per_cpu(krait_vregs, 0); |
| 1164 | |
| 1165 | __krait_power_mdd_enable(kvreg, true); |
| 1166 | } |
| 1167 | |
| 1168 | static struct syscore_ops boot_cpu_mdd_ops = { |
| 1169 | .suspend = boot_cpu_mdd_off, |
| 1170 | .resume = boot_cpu_mdd_on, |
| 1171 | }; |
| 1172 | |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 1173 | static int __devinit krait_pdn_probe(struct platform_device *pdev) |
| 1174 | { |
| 1175 | int rc; |
| 1176 | bool use_phase_switching = false; |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 1177 | int pfm_threshold; |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 1178 | struct device *dev = &pdev->dev; |
| 1179 | struct device_node *node = dev->of_node; |
| 1180 | struct pmic_gang_vreg *pvreg; |
Stepan Moskovchenko | 7663b78 | 2013-03-08 20:13:06 -0800 | [diff] [blame] | 1181 | struct resource *res; |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 1182 | |
| 1183 | if (!dev->of_node) { |
| 1184 | dev_err(dev, "device tree information missing\n"); |
| 1185 | return -ENODEV; |
| 1186 | } |
| 1187 | |
| 1188 | use_phase_switching = of_property_read_bool(node, |
| 1189 | "qcom,use-phase-switching"); |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 1190 | |
| 1191 | rc = of_property_read_u32(node, "qcom,pfm-threshold", &pfm_threshold); |
| 1192 | if (rc < 0) { |
| 1193 | dev_err(dev, "pfm-threshold missing rc=%d, pfm disabled\n", rc); |
| 1194 | return -EINVAL; |
| 1195 | } |
| 1196 | |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 1197 | pvreg = devm_kzalloc(&pdev->dev, |
| 1198 | sizeof(struct pmic_gang_vreg), GFP_KERNEL); |
| 1199 | if (!pvreg) { |
| 1200 | pr_err("kzalloc failed.\n"); |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
Stepan Moskovchenko | 7663b78 | 2013-03-08 20:13:06 -0800 | [diff] [blame] | 1204 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "apcs_gcc"); |
| 1205 | if (!res) { |
| 1206 | dev_err(&pdev->dev, "missing apcs gcc base addresses\n"); |
| 1207 | return -EINVAL; |
| 1208 | } |
| 1209 | |
| 1210 | pvreg->apcs_gcc_base = devm_ioremap(&pdev->dev, res->start, |
| 1211 | resource_size(res)); |
| 1212 | |
| 1213 | if (pvreg->apcs_gcc_base == NULL) |
| 1214 | return -ENOMEM; |
| 1215 | |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 1216 | pvreg->name = "pmic_gang"; |
| 1217 | pvreg->pmic_vmax_uV = PMIC_VOLTAGE_MIN; |
| 1218 | pvreg->pmic_phase_count = -EINVAL; |
| 1219 | pvreg->retention_enabled = true; |
| 1220 | pvreg->pmic_min_uV_for_retention = INT_MAX; |
| 1221 | pvreg->use_phase_switching = use_phase_switching; |
Abhijeet Dharmapurikar | 62b1f70 | 2013-03-29 18:38:46 -0700 | [diff] [blame] | 1222 | pvreg->pfm_threshold = pfm_threshold; |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 1223 | |
| 1224 | mutex_init(&pvreg->krait_power_vregs_lock); |
| 1225 | INIT_LIST_HEAD(&pvreg->krait_power_vregs); |
| 1226 | the_gang = pvreg; |
| 1227 | |
| 1228 | pr_debug("name=%s inited\n", pvreg->name); |
| 1229 | |
| 1230 | /* global initializtion */ |
Stepan Moskovchenko | 7663b78 | 2013-03-08 20:13:06 -0800 | [diff] [blame] | 1231 | glb_init(pvreg->apcs_gcc_base); |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 1232 | |
| 1233 | rc = of_platform_populate(node, NULL, NULL, dev); |
| 1234 | if (rc) { |
| 1235 | dev_err(dev, "failed to add child nodes, rc=%d\n", rc); |
| 1236 | return rc; |
| 1237 | } |
| 1238 | |
| 1239 | dent = debugfs_create_dir(KRAIT_REGULATOR_DRIVER_NAME, NULL); |
| 1240 | debugfs_create_file("retention_uV", |
| 1241 | 0644, dent, the_gang, &retention_fops); |
Abhijeet Dharmapurikar | c8605a6 | 2013-03-11 21:24:25 -0700 | [diff] [blame] | 1242 | register_syscore_ops(&boot_cpu_mdd_ops); |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static int __devexit krait_pdn_remove(struct platform_device *pdev) |
| 1247 | { |
| 1248 | the_gang = NULL; |
| 1249 | debugfs_remove_recursive(dent); |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | static struct platform_driver krait_pdn_driver = { |
| 1254 | .probe = krait_pdn_probe, |
| 1255 | .remove = __devexit_p(krait_pdn_remove), |
| 1256 | .driver = { |
| 1257 | .name = KRAIT_PDN_DRIVER_NAME, |
| 1258 | .of_match_table = krait_pdn_match_table, |
| 1259 | .owner = THIS_MODULE, |
| 1260 | }, |
| 1261 | }; |
| 1262 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 1263 | int __init krait_power_init(void) |
| 1264 | { |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 1265 | int rc = platform_driver_register(&krait_power_driver); |
| 1266 | if (rc) { |
| 1267 | pr_err("failed to add %s driver rc = %d\n", |
| 1268 | KRAIT_REGULATOR_DRIVER_NAME, rc); |
| 1269 | return rc; |
| 1270 | } |
| 1271 | return platform_driver_register(&krait_pdn_driver); |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | static void __exit krait_power_exit(void) |
| 1275 | { |
| 1276 | platform_driver_unregister(&krait_power_driver); |
Abhijeet Dharmapurikar | 46a7ad3 | 2013-02-23 12:50:19 -0800 | [diff] [blame] | 1277 | platform_driver_unregister(&krait_pdn_driver); |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 1278 | } |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 1279 | module_exit(krait_power_exit); |
| 1280 | |
Abhijeet Dharmapurikar | e8571ef | 2012-08-14 20:44:58 -0700 | [diff] [blame] | 1281 | void secondary_cpu_hs_init(void *base_ptr) |
| 1282 | { |
Abhijeet Dharmapurikar | e8571ef | 2012-08-14 20:44:58 -0700 | [diff] [blame] | 1283 | /* Turn on the BHS, turn off LDO Bypass and power down LDO */ |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 1284 | writel_relaxed( |
| 1285 | BHS_CNT_DEFAULT << BHS_CNT_BIT_POS |
| 1286 | | LDO_PWR_DWN_MASK |
| 1287 | | CLK_SRC_DEFAULT << CLK_SRC_SEL_BIT_POS |
| 1288 | | BHS_SEG_EN_DEFAULT << BHS_SEG_EN_BIT_POS |
| 1289 | | BHS_EN_MASK, |
| 1290 | base_ptr + APC_PWR_GATE_CTL); |
| 1291 | |
| 1292 | /* complete the above write before the delay */ |
Abhijeet Dharmapurikar | e8571ef | 2012-08-14 20:44:58 -0700 | [diff] [blame] | 1293 | mb(); |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 1294 | |
| 1295 | /* |
| 1296 | * wait for the bhs to settle |
| 1297 | */ |
| 1298 | udelay(BHS_SETTLING_DELAY_US); |
Abhijeet Dharmapurikar | e8571ef | 2012-08-14 20:44:58 -0700 | [diff] [blame] | 1299 | |
| 1300 | /* Finally turn on the bypass so that BHS supplies power */ |
Abhijeet Dharmapurikar | 749ecd4 | 2012-11-08 17:29:04 -0800 | [diff] [blame] | 1301 | writel_relaxed( |
| 1302 | BHS_CNT_DEFAULT << BHS_CNT_BIT_POS |
| 1303 | | LDO_PWR_DWN_MASK |
| 1304 | | CLK_SRC_DEFAULT << CLK_SRC_SEL_BIT_POS |
| 1305 | | LDO_BYP_MASK |
| 1306 | | BHS_SEG_EN_DEFAULT << BHS_SEG_EN_BIT_POS |
| 1307 | | BHS_EN_MASK, |
| 1308 | base_ptr + APC_PWR_GATE_CTL); |
Abhijeet Dharmapurikar | e8571ef | 2012-08-14 20:44:58 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
Willie Ruan | 421ad42 | 2013-04-17 12:26:14 -0700 | [diff] [blame^] | 1311 | static int __devinit begin_pmic_phase_management(void) |
| 1312 | { |
| 1313 | if (the_gang != NULL) |
| 1314 | the_gang->manage_phases = true; |
| 1315 | return 0; |
| 1316 | } |
| 1317 | late_initcall(begin_pmic_phase_management); |
| 1318 | |
Abhijeet Dharmapurikar | 00269e5 | 2012-05-14 17:59:10 -0700 | [diff] [blame] | 1319 | MODULE_LICENSE("GPL v2"); |
| 1320 | MODULE_DESCRIPTION("KRAIT POWER regulator driver"); |
| 1321 | MODULE_VERSION("1.0"); |
| 1322 | MODULE_ALIAS("platform:"KRAIT_REGULATOR_DRIVER_NAME); |