blob: c714a857d481d47f9183b1b22354aa00990643f3 [file] [log] [blame]
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -08001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -07002 *
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 Dharmapurikarfe1306b2012-11-15 18:09:31 -080013#define pr_fmt(fmt) "PDN %s: " fmt, __func__
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -070014
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 Dharmapurikar37c47a82013-01-08 18:44:21 -080030#include <linux/debugfs.h>
Abhijeet Dharmapurikarc8605a62013-03-11 21:24:25 -070031#include <linux/syscore_ops.h>
Abhijeet Dharmapurikar3ff7cba2012-10-10 17:48:29 -070032#include <mach/msm_iomap.h>
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -070033
34#include "spm.h"
Abhijeet Dharmapurikar295c4dc2013-01-08 12:59:01 -080035#include "pm.h"
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -070036
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -070037/*
38 * supply
39 * from
40 * pmic
41 * gang
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -080042 * |
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 Dharmapurikar152a7082012-05-14 20:25:59 -070053 * |
54 * ________|________
55 * | |
56 * | KRAIT |
57 * |_________________|
58 */
59
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -070060#define PMIC_VOLTAGE_MIN 350000
61#define PMIC_VOLTAGE_MAX 1355000
62#define LV_RANGE_STEP 5000
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -070063
Abhijeet Dharmapurikar62b1f702013-03-29 18:38:46 -070064#define CORE_VOLTAGE_BOOTUP 900000
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -070065
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -070066#define KRAIT_LDO_VOLTAGE_MIN 465000
Abhijeet Dharmapurikare8571ef2012-08-14 20:44:58 -070067#define KRAIT_LDO_VOLTAGE_OFFSET 465000
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -070068#define KRAIT_LDO_STEP 5000
69
70#define BHS_SETTLING_DELAY_US 1
71#define LDO_SETTLING_DELAY_US 1
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -080072#define MDD_SETTLING_DELAY_US 5
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -070073
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 Dharmapurikar3ff7cba2012-10-10 17:48:29 -070085#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 Dharmapurikar152a7082012-05-14 20:25:59 -070090
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -080091/* MDD register group */
92#define MDD_CONFIG_CTL 0x00000000
93#define MDD_MODE 0x00000010
94
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -070095/* 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 Dharmapurikar37c47a82013-01-08 18:44:21 -0800124#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 Dharmapurikar00269e52012-05-14 17:59:10 -0700136/**
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 Dharmapurikar62b1f702013-03-29 18:38:46 -0700147 * @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 Dharmapurikar00269e52012-05-14 17:59:10 -0700151 */
152struct 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 Dharmapurikar6a42ef82012-10-18 19:37:36 -0700158 bool pfm_mode;
Abhijeet Dharmapurikar295c4dc2013-01-08 12:59:01 -0800159 int pmic_min_uV_for_retention;
160 bool retention_enabled;
Abhijeet Dharmapurikar46a7ad32013-02-23 12:50:19 -0800161 bool use_phase_switching;
Stepan Moskovchenko7663b782013-03-08 20:13:06 -0800162 void __iomem *apcs_gcc_base;
Abhijeet Dharmapurikar62b1f702013-03-29 18:38:46 -0700163 bool manage_phases;
164 int pfm_threshold;
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700165};
166
167static struct pmic_gang_vreg *the_gang;
168
169enum krait_supply_mode {
170 HS_MODE = REGULATOR_MODE_NORMAL,
171 LDO_MODE = REGULATOR_MODE_IDLE,
172};
173
174struct 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 Dharmapurikar62b1f702013-03-29 18:38:46 -0700181 int load;
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700182 enum krait_supply_mode mode;
183 void __iomem *reg_base;
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -0800184 void __iomem *mdd_base;
Abhijeet Dharmapurikar34f0d642012-10-25 12:35:23 -0700185 int ldo_default_uV;
186 int retention_uV;
187 int headroom_uV;
188 int ldo_threshold_uV;
Abhijeet Dharmapurikar2269db02012-12-05 15:49:20 -0800189 int ldo_delta_uV;
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -0800190 int cpu_num;
Abhijeet Dharmapurikar62b1f702013-03-29 18:38:46 -0700191 int coeff1;
192 int coeff2;
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800193 bool online;
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700194};
195
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -0800196DEFINE_PER_CPU(struct krait_power_vreg *, krait_vregs);
197
Abhijeet Dharmapurikar3ff7cba2012-10-10 17:48:29 -0700198static u32 version;
199
Abhijeet Dharmapurikar37c47a82013-01-08 18:44:21 -0800200static 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 Dharmapurikar152a7082012-05-14 20:25:59 -0700209static 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 Dharmapurikar37c47a82013-01-08 18:44:21 -0800227static 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 Dharmapurikar152a7082012-05-14 20:25:59 -0700244static 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 Dharmapurikar34f0d642012-10-25 12:35:23 -0700261static int set_krait_retention_uv(struct krait_power_vreg *kvreg, int uV)
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700262{
263 uint32_t reg_val;
264
Abhijeet Dharmapurikar34f0d642012-10-25 12:35:23 -0700265 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
272static 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 Dharmapurikar152a7082012-05-14 20:25:59 -0700277 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 Dharmapurikare8f83852013-02-06 18:57:17 -0800283static 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 Dharmapurikar62b1f702013-03-29 18:38:46 -0700301#define COEFF2_UV_THRESHOLD 850000
302static 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
315static 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
323static 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
351static 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
364static 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
382static 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 Ruan421ad422013-04-17 12:26:14 -0700390 if (pvreg->manage_phases == false)
391 return 0;
Abhijeet Dharmapurikar62b1f702013-03-29 18:38:46 -0700392
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
456static 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
474static 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
494static int krait_power_set_mode(struct regulator_dev *rdev, unsigned int mode)
495{
496 return 0;
497}
498
499static 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 Dharmapurikar152a7082012-05-14 20:25:59 -0700506static int switch_to_using_hs(struct krait_power_vreg *kvreg)
507{
508 if (kvreg->mode == HS_MODE)
509 return 0;
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700510 /* enable bhs */
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -0800511 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 Dharmapurikar152a7082012-05-14 20:25:59 -0700517
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 Dharmapurikar749ecd42012-11-08 17:29:04 -0800525 /*
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 Dharmapurikar152a7082012-05-14 20:25:59 -0700531 /* 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 Dharmapurikarfe1306b2012-11-15 18:09:31 -0800536 pr_debug("%s using BHS\n", kvreg->name);
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700537 return 0;
538}
539
540static int switch_to_using_ldo(struct krait_power_vreg *kvreg)
541{
Abhijeet Dharmapurikar2269db02012-12-05 15:49:20 -0800542 if (kvreg->mode == LDO_MODE
543 && get_krait_ldo_uv(kvreg) == kvreg->uV - kvreg->ldo_delta_uV)
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700544 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 Dharmapurikar2269db02012-12-05 15:49:20 -0800553 set_krait_ldo_uv(kvreg, kvreg->uV - kvreg->ldo_delta_uV);
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700554
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 Dharmapurikar749ecd42012-11-08 17:29:04 -0800562 /* complete the writes before the delay */
563 mb();
564
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700565 /* 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 Dharmapurikar749ecd42012-11-08 17:29:04 -0800574 krait_masked_write(kvreg, APC_PWR_GATE_CTL, BHS_SEG_EN_MASK, 0);
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700575
576 kvreg->mode = LDO_MODE;
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800577 pr_debug("%s using LDO\n", kvreg->name);
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700578 return 0;
579}
580
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800581static int set_pmic_gang_voltage(struct pmic_gang_vreg *pvreg, int uV)
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700582{
583 int setpoint;
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800584 int rc;
585
586 if (pvreg->pmic_vmax_uV == uV)
587 return 0;
588
589 pr_debug("%d\n", uV);
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700590
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 Dharmapurikar295c4dc2013-01-08 12:59:01 -0800602 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 Dharmapurikard4f38da2012-08-08 21:42:06 -0700618 setpoint = DIV_ROUND_UP(uV, LV_RANGE_STEP);
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800619
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800620 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 Dharmapurikar00269e52012-05-14 17:59:10 -0700628}
629
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800630static int configure_ldo_or_hs_all(struct krait_power_vreg *from, int vmax)
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700631{
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700632 struct pmic_gang_vreg *pvreg = from->pvreg;
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700633 struct krait_power_vreg *kvreg;
634 int rc = 0;
635
636 list_for_each_entry(kvreg, &pvreg->krait_power_vregs, link) {
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800637 if (!kvreg->online)
638 continue;
Abhijeet Dharmapurikar2269db02012-12-05 15:49:20 -0800639 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 Dharmapurikar152a7082012-05-14 20:25:59 -0700643 if (rc < 0) {
Abhijeet Dharmapurikar2269db02012-12-05 15:49:20 -0800644 pr_err("could not switch %s to ldo rc = %d\n",
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700645 kvreg->name, rc);
646 return rc;
647 }
648 } else {
Abhijeet Dharmapurikar2269db02012-12-05 15:49:20 -0800649 rc = switch_to_using_hs(kvreg);
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700650 if (rc < 0) {
Abhijeet Dharmapurikar2269db02012-12-05 15:49:20 -0800651 pr_err("could not switch %s to hs rc = %d\n",
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700652 kvreg->name, rc);
653 return rc;
654 }
655 }
656 }
657
658 return rc;
659}
660
661#define SLEW_RATE 2994
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800662static int krait_voltage_increase(struct krait_power_vreg *from,
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700663 int vmax)
664{
665 struct pmic_gang_vreg *pvreg = from->pvreg;
666 int rc = 0;
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700667 int settling_us;
668
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700669 /*
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800670 * since krait voltage is increasing set the gang voltage
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700671 * prior to changing ldo/hs states of the requesting krait
672 */
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800673 rc = set_pmic_gang_voltage(pvreg, vmax);
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700674 if (rc < 0) {
675 dev_err(&from->rdev->dev, "%s failed set voltage %d rc = %d\n",
676 pvreg->name, vmax, rc);
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800677 return rc;
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700678 }
679
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -0800680
681 /* complete the above writes before the delay */
682 mb();
683
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700684 /* 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 Dharmapurikarfe1306b2012-11-15 18:09:31 -0800688 rc = configure_ldo_or_hs_all(from, vmax);
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700689 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 Dharmapurikarfe1306b2012-11-15 18:09:31 -0800697static int krait_voltage_decrease(struct krait_power_vreg *from,
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700698 int vmax)
699{
700 struct pmic_gang_vreg *pvreg = from->pvreg;
701 int rc = 0;
702
703 /*
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800704 * since krait voltage is decreasing ldos might get out of their
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700705 * operating range. Hence configure such kraits to be in hs mode prior
706 * to setting the pmic gang voltage
707 */
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800708 rc = configure_ldo_or_hs_all(from, vmax);
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700709 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 Dharmapurikar00269e52012-05-14 17:59:10 -0700714
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800715 rc = set_pmic_gang_voltage(pvreg, vmax);
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700716 if (rc < 0) {
717 dev_err(&from->rdev->dev, "%s failed set voltage %d rc = %d\n",
718 pvreg->name, vmax, rc);
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700719 }
720
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700721 return rc;
722}
723
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700724static 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 Dharmapurikarfe1306b2012-11-15 18:09:31 -0800731static int get_vmax(struct pmic_gang_vreg *pvreg)
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700732{
733 int vmax = 0;
734 int v;
735 struct krait_power_vreg *kvreg;
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700736
737 list_for_each_entry(kvreg, &pvreg->krait_power_vregs, link) {
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800738 if (!kvreg->online)
739 continue;
740
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700741 v = kvreg->uV;
742
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700743 if (vmax < v)
744 vmax = v;
745 }
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800746
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700747 return vmax;
748}
749
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700750#define ROUND_UP_VOLTAGE(v, res) (DIV_ROUND_UP(v, res) * res)
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800751static int _set_voltage(struct regulator_dev *rdev,
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800752 int orig_krait_uV, int requested_uV)
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700753{
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 Dharmapurikar62b1f702013-03-29 18:38:46 -0700758 int coeff_total;
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700759
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800760 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 Bohanb5d1cdb2012-11-08 18:09:52 -0800768
769 /* round up the pmic voltage as per its resolution */
770 vmax = ROUND_UP_VOLTAGE(vmax, LV_RANGE_STEP);
771
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800772 if (requested_uV > orig_krait_uV)
773 rc = krait_voltage_increase(kvreg, vmax);
774 else
775 rc = krait_voltage_decrease(kvreg, vmax);
776
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800777 if (rc < 0) {
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800778 dev_err(&rdev->dev, "%s failed to set %duV from %duV rc = %d\n",
779 kvreg->name, requested_uV, orig_krait_uV, rc);
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800780 }
781
Abhijeet Dharmapurikar62b1f702013-03-29 18:38:46 -0700782 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 Bohanb5d1cdb2012-11-08 18:09:52 -0800786 return rc;
787}
788
789static 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 Dharmapurikar152a7082012-05-14 20:25:59 -0700796 /*
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 Dharmapurikar34f0d642012-10-25 12:35:23 -0700801 if (min_uV < kvreg->ldo_threshold_uV) {
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700802 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 Dharmapurikar00269e52012-05-14 17:59:10 -0700807 mutex_lock(&pvreg->krait_power_vregs_lock);
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800808 if (!kvreg->online) {
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800809 kvreg->uV = min_uV;
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800810 mutex_unlock(&pvreg->krait_power_vregs_lock);
811 return 0;
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700812 }
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700813
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800814 rc = _set_voltage(rdev, kvreg->uV, min_uV);
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700815 mutex_unlock(&pvreg->krait_power_vregs_lock);
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800816
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700817 return rc;
818}
819
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800820static 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
827static 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 Dharmapurikarc8605a62013-03-11 21:24:25 -0700834 __krait_power_mdd_enable(kvreg, true);
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800835 kvreg->online = true;
Abhijeet Dharmapurikar62b1f702013-03-29 18:38:46 -0700836 rc = _get_optimum_mode(rdev, kvreg->uV, kvreg->uV, kvreg->load);
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800837 if (rc < 0)
838 goto en_err;
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800839 /*
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 Bohanb5d1cdb2012-11-08 18:09:52 -0800844en_err:
845 mutex_unlock(&pvreg->krait_power_vregs_lock);
846 return rc;
847}
848
849static 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 Dharmapurikar62b1f702013-03-29 18:38:46 -0700858 rc = _get_optimum_mode(rdev, kvreg->uV, kvreg->uV, kvreg->load);
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800859 if (rc < 0)
860 goto dis_err;
861
Abhijeet Dharmapurikarfe1306b2012-11-15 18:09:31 -0800862 rc = _set_voltage(rdev, kvreg->uV, kvreg->uV);
Abhijeet Dharmapurikarc8605a62013-03-11 21:24:25 -0700863 __krait_power_mdd_enable(kvreg, false);
Michael Bohanb5d1cdb2012-11-08 18:09:52 -0800864dis_err:
865 mutex_unlock(&pvreg->krait_power_vregs_lock);
866 return rc;
867}
868
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700869static 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 Bohanb5d1cdb2012-11-08 18:09:52 -0800875 .enable = krait_power_enable,
876 .disable = krait_power_disable,
877 .is_enabled = krait_power_is_enabled,
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700878};
879
Abhijeet Dharmapurikar37c47a82013-01-08 18:44:21 -0800880static struct dentry *dent;
881static 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
897static 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}
914DEFINE_SIMPLE_ATTRIBUTE(retention_fops,
915 get_retention_dbg_uV, set_retention_dbg_uV, "%llu\n");
916
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700917static 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 Dharmapurikar34f0d642012-10-25 12:35:23 -0700923 set_krait_retention_uv(kvreg, kvreg->retention_uV);
924 set_krait_ldo_uv(kvreg, kvreg->ldo_default_uV);
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -0800925
926 /* setup the bandgap that configures the reference to the LDO */
927 writel_relaxed(0x00000190, kvreg->mdd_base + MDD_CONFIG_CTL);
Abhijeet Dharmapurikarc8605a62013-03-11 21:24:25 -0700928 /* Enable MDD */
929 writel_relaxed(0x00000002, kvreg->mdd_base + MDD_MODE);
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -0800930 mb();
Abhijeet Dharmapurikar152a7082012-05-14 20:25:59 -0700931}
932
Stepan Moskovchenko7663b782013-03-08 20:13:06 -0800933static void glb_init(void __iomem *apcs_gcc_base)
Abhijeet Dharmapurikar3ff7cba2012-10-10 17:48:29 -0700934{
935 /* configure bi-modal switch */
Stepan Moskovchenko7663b782013-03-08 20:13:06 -0800936 writel_relaxed(0x0008736E, apcs_gcc_base + PWR_GATE_CONFIG);
Abhijeet Dharmapurikar3ff7cba2012-10-10 17:48:29 -0700937 /* read kpss version */
Stepan Moskovchenko7663b782013-03-08 20:13:06 -0800938 version = readl_relaxed(apcs_gcc_base + VERSION);
Abhijeet Dharmapurikar3ff7cba2012-10-10 17:48:29 -0700939 pr_debug("version= 0x%x\n", version);
940}
941
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700942static int __devinit krait_power_probe(struct platform_device *pdev)
943{
944 struct krait_power_vreg *kvreg;
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -0800945 struct resource *res, *res_mdd;
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700946 struct regulator_init_data *init_data = pdev->dev.platform_data;
947 int rc = 0;
Abhijeet Dharmapurikar34f0d642012-10-25 12:35:23 -0700948 int headroom_uV, retention_uV, ldo_default_uV, ldo_threshold_uV;
Abhijeet Dharmapurikar2269db02012-12-05 15:49:20 -0800949 int ldo_delta_uV;
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -0800950 int cpu_num;
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700951
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -0700952 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 Dharmapurikar34f0d642012-10-25 12:35:23 -0700963 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 Dharmapurikar2269db02012-12-05 15:49:20 -08001014
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 Dharmapurikare8f83852013-02-06 18:57:17 -08001027 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 Dharmapurikar00269e52012-05-14 17:59:10 -07001034 }
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 Dharmapurikar749ecd42012-11-08 17:29:04 -08001047 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "acs");
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -07001048 if (!res) {
1049 dev_err(&pdev->dev, "missing physical register addresses\n");
1050 return -EINVAL;
1051 }
1052
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -08001053 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 Dharmapurikar00269e52012-05-14 17:59:10 -07001059 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 Dharmapurikar749ecd42012-11-08 17:29:04 -08001069 kvreg->mdd_base = devm_ioremap(&pdev->dev,
1070 res_mdd->start, resource_size(res));
1071
Abhijeet Dharmapurikar34f0d642012-10-25 12:35:23 -07001072 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 Dharmapurikar62b1f702013-03-29 18:38:46 -07001078 kvreg->uV = CORE_VOLTAGE_BOOTUP;
Abhijeet Dharmapurikar34f0d642012-10-25 12:35:23 -07001079 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 Dharmapurikar2269db02012-12-05 15:49:20 -08001085 kvreg->ldo_delta_uV = ldo_delta_uV;
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -08001086 kvreg->cpu_num = cpu_num;
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -07001087
1088 platform_set_drvdata(pdev, kvreg);
1089
1090 mutex_lock(&the_gang->krait_power_vregs_lock);
Abhijeet Dharmapurikar295c4dc2013-01-08 12:59:01 -08001091 the_gang->pmic_min_uV_for_retention
1092 = min(the_gang->pmic_min_uV_for_retention,
1093 kvreg->retention_uV + kvreg->headroom_uV);
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -07001094 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 Dharmapurikar152a7082012-05-14 20:25:59 -07001105 kvreg_hw_init(kvreg);
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -08001106 per_cpu(krait_vregs, cpu_num) = kvreg;
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -07001107 dev_dbg(&pdev->dev, "id=%d, name=%s\n", pdev->id, kvreg->name);
1108
1109 return 0;
1110out:
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
1119static 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
1133static struct of_device_id krait_power_match_table[] = {
1134 { .compatible = "qcom,krait-regulator", },
1135 {}
1136};
1137
1138static 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 Dharmapurikar46a7ad32013-02-23 12:50:19 -08001148static struct of_device_id krait_pdn_match_table[] = {
1149 { .compatible = "qcom,krait-pdn", },
1150 {}
1151};
1152
Abhijeet Dharmapurikarc8605a62013-03-11 21:24:25 -07001153static 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
1161static 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
1168static struct syscore_ops boot_cpu_mdd_ops = {
1169 .suspend = boot_cpu_mdd_off,
1170 .resume = boot_cpu_mdd_on,
1171};
1172
Abhijeet Dharmapurikar46a7ad32013-02-23 12:50:19 -08001173static int __devinit krait_pdn_probe(struct platform_device *pdev)
1174{
1175 int rc;
1176 bool use_phase_switching = false;
Abhijeet Dharmapurikar62b1f702013-03-29 18:38:46 -07001177 int pfm_threshold;
Abhijeet Dharmapurikar46a7ad32013-02-23 12:50:19 -08001178 struct device *dev = &pdev->dev;
1179 struct device_node *node = dev->of_node;
1180 struct pmic_gang_vreg *pvreg;
Stepan Moskovchenko7663b782013-03-08 20:13:06 -08001181 struct resource *res;
Abhijeet Dharmapurikar46a7ad32013-02-23 12:50:19 -08001182
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 Dharmapurikar62b1f702013-03-29 18:38:46 -07001190
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 Dharmapurikar46a7ad32013-02-23 12:50:19 -08001197 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 Moskovchenko7663b782013-03-08 20:13:06 -08001204 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 Dharmapurikar46a7ad32013-02-23 12:50:19 -08001216 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 Dharmapurikar62b1f702013-03-29 18:38:46 -07001222 pvreg->pfm_threshold = pfm_threshold;
Abhijeet Dharmapurikar46a7ad32013-02-23 12:50:19 -08001223
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 Moskovchenko7663b782013-03-08 20:13:06 -08001231 glb_init(pvreg->apcs_gcc_base);
Abhijeet Dharmapurikar46a7ad32013-02-23 12:50:19 -08001232
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 Dharmapurikarc8605a62013-03-11 21:24:25 -07001242 register_syscore_ops(&boot_cpu_mdd_ops);
Abhijeet Dharmapurikar46a7ad32013-02-23 12:50:19 -08001243 return 0;
1244}
1245
1246static int __devexit krait_pdn_remove(struct platform_device *pdev)
1247{
1248 the_gang = NULL;
1249 debugfs_remove_recursive(dent);
1250 return 0;
1251}
1252
1253static 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 Dharmapurikar00269e52012-05-14 17:59:10 -07001263int __init krait_power_init(void)
1264{
Abhijeet Dharmapurikar46a7ad32013-02-23 12:50:19 -08001265 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 Dharmapurikar00269e52012-05-14 17:59:10 -07001272}
1273
1274static void __exit krait_power_exit(void)
1275{
1276 platform_driver_unregister(&krait_power_driver);
Abhijeet Dharmapurikar46a7ad32013-02-23 12:50:19 -08001277 platform_driver_unregister(&krait_pdn_driver);
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -07001278}
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -07001279module_exit(krait_power_exit);
1280
Abhijeet Dharmapurikare8571ef2012-08-14 20:44:58 -07001281void secondary_cpu_hs_init(void *base_ptr)
1282{
Abhijeet Dharmapurikare8571ef2012-08-14 20:44:58 -07001283 /* Turn on the BHS, turn off LDO Bypass and power down LDO */
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -08001284 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 Dharmapurikare8571ef2012-08-14 20:44:58 -07001293 mb();
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -08001294
1295 /*
1296 * wait for the bhs to settle
1297 */
1298 udelay(BHS_SETTLING_DELAY_US);
Abhijeet Dharmapurikare8571ef2012-08-14 20:44:58 -07001299
1300 /* Finally turn on the bypass so that BHS supplies power */
Abhijeet Dharmapurikar749ecd42012-11-08 17:29:04 -08001301 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 Dharmapurikare8571ef2012-08-14 20:44:58 -07001309}
1310
Willie Ruan421ad422013-04-17 12:26:14 -07001311static int __devinit begin_pmic_phase_management(void)
1312{
1313 if (the_gang != NULL)
1314 the_gang->manage_phases = true;
1315 return 0;
1316}
1317late_initcall(begin_pmic_phase_management);
1318
Abhijeet Dharmapurikar00269e52012-05-14 17:59:10 -07001319MODULE_LICENSE("GPL v2");
1320MODULE_DESCRIPTION("KRAIT POWER regulator driver");
1321MODULE_VERSION("1.0");
1322MODULE_ALIAS("platform:"KRAIT_REGULATOR_DRIVER_NAME);