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