blob: e51a1f554b578d88c55facde31d6e6685084e764 [file] [log] [blame]
Willie Ruan2635a852013-03-17 16:33:42 -07001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/module.h>
17#include <linux/err.h>
18#include <linux/string.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/io.h>
22#include <linux/bitops.h>
23#include <linux/slab.h>
24#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/platform_device.h>
Willie Ruanb17b7832013-04-08 21:30:27 -070027#include <linux/interrupt.h>
Willie Ruan2635a852013-03-17 16:33:42 -070028#include <linux/regulator/driver.h>
29#include <linux/regulator/of_regulator.h>
30#include <linux/regulator/cpr-regulator.h>
31
Willie Ruanb17b7832013-04-08 21:30:27 -070032/* Register Offsets for RB-CPR and Bit Definitions */
33
34/* RBCPR Gate Count and Target Registers */
35#define REG_RBCPR_GCNT_TARGET(n) (0x60 + 4 * n)
36
37#define RBCPR_GCNT_TARGET_GCNT_BITS 10
38#define RBCPR_GCNT_TARGET_GCNT_SHIFT 12
39#define RBCPR_GCNT_TARGET_GCNT_MASK ((1<<RBCPR_GCNT_TARGET_GCNT_BITS)-1)
40
41/* RBCPR Timer Control */
42#define REG_RBCPR_TIMER_INTERVAL 0x44
43#define REG_RBIF_TIMER_ADJUST 0x4C
44
45#define RBIF_TIMER_ADJ_CONS_UP_BITS 4
46#define RBIF_TIMER_ADJ_CONS_UP_MASK ((1<<RBIF_TIMER_ADJ_CONS_UP_BITS)-1)
47#define RBIF_TIMER_ADJ_CONS_DOWN_BITS 4
48#define RBIF_TIMER_ADJ_CONS_DOWN_MASK ((1<<RBIF_TIMER_ADJ_CONS_DOWN_BITS)-1)
49#define RBIF_TIMER_ADJ_CONS_DOWN_SHIFT 4
50
51/* RBCPR Config Register */
52#define REG_RBIF_LIMIT 0x48
53#define REG_RBCPR_STEP_QUOT 0x80
54#define REG_RBIF_SW_VLEVEL 0x94
55
56#define RBIF_LIMIT_CEILING_BITS 6
57#define RBIF_LIMIT_CEILING_MASK ((1<<RBIF_LIMIT_CEILING_BITS)-1)
58#define RBIF_LIMIT_CEILING_SHIFT 6
59#define RBIF_LIMIT_FLOOR_BITS 6
60#define RBIF_LIMIT_FLOOR_MASK ((1<<RBIF_LIMIT_FLOOR_BITS)-1)
61
62#define RBIF_LIMIT_CEILING_DEFAULT RBIF_LIMIT_CEILING_MASK
63#define RBIF_LIMIT_FLOOR_DEFAULT 0
64#define RBIF_SW_VLEVEL_DEFAULT 0x20
65
66#define RBCPR_STEP_QUOT_STEPQUOT_BITS 8
67#define RBCPR_STEP_QUOT_STEPQUOT_MASK ((1<<RBCPR_STEP_QUOT_STEPQUOT_BITS)-1)
68#define RBCPR_STEP_QUOT_IDLE_CLK_BITS 4
69#define RBCPR_STEP_QUOT_IDLE_CLK_MASK ((1<<RBCPR_STEP_QUOT_IDLE_CLK_BITS)-1)
70#define RBCPR_STEP_QUOT_IDLE_CLK_SHIFT 8
71
72/* RBCPR Control Register */
73#define REG_RBCPR_CTL 0x90
74
75#define RBCPR_CTL_LOOP_EN BIT(0)
76#define RBCPR_CTL_TIMER_EN BIT(3)
77#define RBCPR_CTL_SW_AUTO_CONT_ACK_EN BIT(5)
78#define RBCPR_CTL_SW_AUTO_CONT_NACK_DN_EN BIT(6)
79#define RBCPR_CTL_COUNT_MODE BIT(10)
80#define RBCPR_CTL_UP_THRESHOLD_BITS 4
81#define RBCPR_CTL_UP_THRESHOLD_MASK ((1<<RBCPR_CTL_UP_THRESHOLD_BITS)-1)
82#define RBCPR_CTL_UP_THRESHOLD_SHIFT 24
83#define RBCPR_CTL_DN_THRESHOLD_BITS 4
84#define RBCPR_CTL_DN_THRESHOLD_MASK ((1<<RBCPR_CTL_DN_THRESHOLD_BITS)-1)
85#define RBCPR_CTL_DN_THRESHOLD_SHIFT 28
86
87/* RBCPR Ack/Nack Response */
88#define REG_RBIF_CONT_ACK_CMD 0x98
89#define REG_RBIF_CONT_NACK_CMD 0x9C
90
91/* RBCPR Result status Register */
92#define REG_RBCPR_RESULT_0 0xA0
93
94#define RBCPR_RESULT0_ERROR_STEPS_SHIFT 2
95#define RBCPR_RESULT0_ERROR_STEPS_BITS 4
96#define RBCPR_RESULT0_ERROR_STEPS_MASK ((1<<RBCPR_RESULT0_ERROR_STEPS_BITS)-1)
97
98/* RBCPR Interrupt Control Register */
99#define REG_RBIF_IRQ_EN(n) (0x100 + 4 * n)
100#define REG_RBIF_IRQ_CLEAR 0x110
101#define REG_RBIF_IRQ_STATUS 0x114
102
103#define CPR_INT_DONE BIT(0)
104#define CPR_INT_MIN BIT(1)
105#define CPR_INT_DOWN BIT(2)
106#define CPR_INT_MID BIT(3)
107#define CPR_INT_UP BIT(4)
108#define CPR_INT_MAX BIT(5)
109#define CPR_INT_CLAMP BIT(6)
110#define CPR_INT_ALL (CPR_INT_DONE | CPR_INT_MIN | CPR_INT_DOWN | \
111 CPR_INT_MID | CPR_INT_UP | CPR_INT_MAX | CPR_INT_CLAMP)
112#define CPR_INT_DEFAULT (CPR_INT_UP | CPR_INT_DOWN)
113
114#define CPR_NUM_RING_OSC 8
115#define CPR_NUM_SAVE_REGS 10
116
117/* CPR eFuse parameters */
118#define CPR_FUSE_TARGET_QUOT_BITS 12
119#define CPR_FUSE_TARGET_QUOT_BITS_MASK ((1<<CPR_FUSE_TARGET_QUOT_BITS)-1)
120#define CPR_FUSE_RO_SEL_BITS 3
121#define CPR_FUSE_RO_SEL_BITS_MASK ((1<<CPR_FUSE_RO_SEL_BITS)-1)
122
123#define CPR_FUSE_TARGET_QUOT_TURBO_SHIFT 0
124#define CPR_FUSE_TARGET_QUOT_NOMINAL_SHIFT 12
125#define CPR_FUSE_TARGET_QUOT_SVS_SHIFT 24
126
127#define CPR_FUSE_DISABLE_CPR_SHIFT 36
128#define CPR_FUSE_LOCAL_APPROACH_SHIFT 37
129#define CPR_FUSE_REDUNDANT_SHIFT 57
130
131/* PVS eFuse parameters */
132#define PVS_FUSE_REDUNDANT_SHIFT 24
133#define PVS_FUSE_REDUNDANT_BITS 3
134#define PVS_FUSE_REDUNDANT_MASK ((1<<PVS_FUSE_REDUNDANT_BITS)-1)
135
136#define PVS_FUSE_BINS_SHIFT 6
137#define PVS_FUSE_BINS_REDUNDANT_SHIFT 27
138
139enum voltage_change_dir {
140 NO_CHANGE,
141 DOWN,
142 UP,
143};
144
Willie Ruan2635a852013-03-17 16:33:42 -0700145struct cpr_regulator {
146 struct regulator_desc rdesc;
147 struct regulator_dev *rdev;
Willie Ruanb17b7832013-04-08 21:30:27 -0700148 bool vreg_enabled;
Willie Ruan2635a852013-03-17 16:33:42 -0700149 int corner;
Willie Ruanb17b7832013-04-08 21:30:27 -0700150 int ceiling_max;
Willie Ruan2635a852013-03-17 16:33:42 -0700151
152 /* Process voltage parameters */
Willie Ruanb17b7832013-04-08 21:30:27 -0700153 phys_addr_t pvs_efuse;
Willie Ruan2635a852013-03-17 16:33:42 -0700154 u32 num_efuse_bits;
Willie Ruan2635a852013-03-17 16:33:42 -0700155 u32 pvs_bin_process[CPR_PVS_EFUSE_BINS_MAX];
Willie Ruanb17b7832013-04-08 21:30:27 -0700156 u32 pvs_corner_v[NUM_APC_PVS][CPR_CORNER_MAX];
Willie Ruan2635a852013-03-17 16:33:42 -0700157 /* Process voltage variables */
158 u32 pvs_bin;
Willie Ruanb17b7832013-04-08 21:30:27 -0700159 u32 process;
Willie Ruan2635a852013-03-17 16:33:42 -0700160
161 /* APC voltage regulator */
162 struct regulator *vdd_apc;
Willie Ruan6d502392013-03-27 15:40:28 -0700163
164 /* Dependency parameters */
165 struct regulator *vdd_mx;
166 int vdd_mx_vmax;
167 int vdd_mx_vmin_method;
168 int vdd_mx_vmin;
Willie Ruanb17b7832013-04-08 21:30:27 -0700169
170 /* CPR parameters */
171 phys_addr_t cpr_fuse_addr;
172 u64 cpr_fuse_bits;
173 u64 cpr_fuse_bits_2;
174 bool cpr_fuse_disable;
175 bool cpr_fuse_local;
176 bool cpr_fuse_redundancy;
177 int cpr_fuse_target_quot[CPR_CORNER_MAX];
178 int cpr_fuse_ro_sel[CPR_CORNER_MAX];
179 int gcnt;
180
181 unsigned int cpr_irq;
182 void __iomem *rbcpr_base;
183 struct mutex cpr_mutex;
184
185 int ceiling_volt[CPR_CORNER_MAX];
186 int floor_volt[CPR_CORNER_MAX];
187 int last_volt[CPR_CORNER_MAX];
188 int step_volt;
189
190 int save_ctl[CPR_CORNER_MAX];
191 int save_irq[CPR_CORNER_MAX];
192
193 u32 save_regs[CPR_NUM_SAVE_REGS];
194 u32 save_reg_val[CPR_NUM_SAVE_REGS];
195
196 /* Config parameters */
197 bool enable;
198 u32 ref_clk_khz;
199 u32 timer_delay_us;
200 u32 timer_cons_up;
201 u32 timer_cons_down;
202 u32 irq_line;
203 u32 step_quotient;
204 u32 up_threshold;
205 u32 down_threshold;
206 u32 idle_clocks;
207 u32 gcnt_time_us;
208 u32 vdd_apc_step_up_limit;
209 u32 vdd_apc_step_down_limit;
Willie Ruan2635a852013-03-17 16:33:42 -0700210};
211
Willie Ruanb17b7832013-04-08 21:30:27 -0700212static int cpr_debug_enable;
213static int cpr_enable;
214static struct cpr_regulator *the_cpr;
215
216module_param_named(debug_enable, cpr_debug_enable, int, S_IRUGO | S_IWUSR);
217#define cpr_debug(message, ...) \
218 do { \
219 if (cpr_debug_enable) \
220 pr_info(message, ##__VA_ARGS__); \
221 } while (0)
222
223static bool cpr_is_allowed(struct cpr_regulator *cpr_vreg)
224{
225 if (cpr_vreg->cpr_fuse_disable || !cpr_enable)
226 return false;
227 else
228 return true;
229}
230
231static void cpr_write(struct cpr_regulator *cpr_vreg, u32 offset, u32 value)
232{
233 writel_relaxed(value, cpr_vreg->rbcpr_base + offset);
234}
235
236static u32 cpr_read(struct cpr_regulator *cpr_vreg, u32 offset)
237{
238 return readl_relaxed(cpr_vreg->rbcpr_base + offset);
239}
240
241static void cpr_masked_write(struct cpr_regulator *cpr_vreg, u32 offset,
242 u32 mask, u32 value)
243{
244 u32 reg_val;
245
246 reg_val = readl_relaxed(cpr_vreg->rbcpr_base + offset);
247 reg_val &= ~mask;
248 reg_val |= value & mask;
249 writel_relaxed(reg_val, cpr_vreg->rbcpr_base + offset);
250}
251
252static void cpr_irq_clr(struct cpr_regulator *cpr_vreg)
253{
254 cpr_write(cpr_vreg, REG_RBIF_IRQ_CLEAR, CPR_INT_ALL);
255}
256
257static void cpr_irq_clr_nack(struct cpr_regulator *cpr_vreg)
258{
259 cpr_irq_clr(cpr_vreg);
260 cpr_write(cpr_vreg, REG_RBIF_CONT_NACK_CMD, 1);
261}
262
263static void cpr_irq_clr_ack(struct cpr_regulator *cpr_vreg)
264{
265 cpr_irq_clr(cpr_vreg);
266 cpr_write(cpr_vreg, REG_RBIF_CONT_ACK_CMD, 1);
267}
268
269static void cpr_irq_set(struct cpr_regulator *cpr_vreg, u32 int_bits)
270{
271 cpr_write(cpr_vreg, REG_RBIF_IRQ_EN(cpr_vreg->irq_line), int_bits);
272}
273
274static void cpr_ctl_modify(struct cpr_regulator *cpr_vreg, u32 mask, u32 value)
275{
276 cpr_masked_write(cpr_vreg, REG_RBCPR_CTL, mask, value);
277}
278
279static void cpr_ctl_enable(struct cpr_regulator *cpr_vreg)
280{
281 u32 val;
282
283 if (cpr_is_allowed(cpr_vreg))
284 val = RBCPR_CTL_LOOP_EN;
285 else
286 val = 0;
287 cpr_ctl_modify(cpr_vreg, RBCPR_CTL_LOOP_EN, val);
288}
289
290static void cpr_ctl_disable(struct cpr_regulator *cpr_vreg)
291{
292 cpr_ctl_modify(cpr_vreg, RBCPR_CTL_LOOP_EN, 0);
293}
294
295static void cpr_regs_save(struct cpr_regulator *cpr_vreg)
296{
297 int i, offset;
298
299 for (i = 0; i < CPR_NUM_SAVE_REGS; i++) {
300 offset = cpr_vreg->save_regs[i];
301 cpr_vreg->save_reg_val[i] = cpr_read(cpr_vreg, offset);
302 }
303}
304
305static void cpr_regs_restore(struct cpr_regulator *cpr_vreg)
306{
307 int i, offset;
308 u32 val;
309
310 for (i = 0; i < CPR_NUM_SAVE_REGS; i++) {
311 offset = cpr_vreg->save_regs[i];
312 val = cpr_vreg->save_reg_val[i];
313 cpr_write(cpr_vreg, offset, val);
314 }
315}
316
317static void cpr_corner_save(struct cpr_regulator *cpr_vreg, int corner)
318{
319 cpr_vreg->save_ctl[corner] = cpr_read(cpr_vreg, REG_RBCPR_CTL);
320 cpr_vreg->save_irq[corner] =
321 cpr_read(cpr_vreg, REG_RBIF_IRQ_EN(cpr_vreg->irq_line));
322}
323
324static void cpr_corner_restore(struct cpr_regulator *cpr_vreg, int corner)
325{
326 u32 gcnt, ctl, irq, ro_sel;
327
328 ro_sel = cpr_vreg->cpr_fuse_ro_sel[corner];
329 gcnt = cpr_vreg->gcnt | cpr_vreg->cpr_fuse_target_quot[corner];
330 cpr_write(cpr_vreg, REG_RBCPR_GCNT_TARGET(ro_sel), gcnt);
331 ctl = cpr_vreg->save_ctl[corner];
332 cpr_write(cpr_vreg, REG_RBCPR_CTL, ctl);
333 irq = cpr_vreg->save_irq[corner];
334 cpr_irq_set(cpr_vreg, irq);
335 cpr_debug("gcnt = 0x%08x, ctl = 0x%08x, irq = 0x%08x\n",
336 gcnt, ctl, irq);
337}
338
339static void cpr_corner_switch(struct cpr_regulator *cpr_vreg, int corner)
340{
341 if (cpr_vreg->corner == corner)
342 return;
343
344 cpr_corner_restore(cpr_vreg, corner);
345}
346
347/* Module parameter ops */
348static int cpr_enable_param_set(const char *val, const struct kernel_param *kp)
349{
350 int rc;
351 int old_cpr_enable;
352
353 if (!the_cpr) {
354 pr_err("the_cpr = NULL\n");
355 return -ENXIO;
356 }
357
358 mutex_lock(&the_cpr->cpr_mutex);
359
360 old_cpr_enable = cpr_enable;
361 rc = param_set_int(val, kp);
362 if (rc) {
363 pr_err("param_set_int: rc = %d\n", rc);
364 goto _exit;
365 }
366
367 cpr_debug("%d -> %d [corner=%d]\n",
368 old_cpr_enable, cpr_enable, the_cpr->corner);
369
370 if (the_cpr->cpr_fuse_disable) {
371 /* Already disabled */
372 pr_info("CPR disabled by fuse\n");
373 goto _exit;
374 }
375
376 if ((old_cpr_enable != cpr_enable) && the_cpr->corner) {
377 if (cpr_enable) {
378 cpr_ctl_disable(the_cpr);
379 cpr_irq_clr(the_cpr);
380 cpr_corner_restore(the_cpr, the_cpr->corner);
381 cpr_ctl_enable(the_cpr);
382 } else {
383 cpr_ctl_disable(the_cpr);
384 cpr_irq_set(the_cpr, 0);
385 }
386 }
387
388_exit:
389 mutex_unlock(&the_cpr->cpr_mutex);
390 return 0;
391}
392
393static struct kernel_param_ops cpr_enable_ops = {
394 .set = cpr_enable_param_set,
395 .get = param_get_int,
396};
397
398module_param_cb(cpr_enable, &cpr_enable_ops, &cpr_enable, S_IRUGO | S_IWUSR);
399
400static int cpr_apc_set(struct cpr_regulator *cpr_vreg, u32 new_volt)
401{
402 int max_volt, rc;
403
404 max_volt = cpr_vreg->ceiling_max;
405 rc = regulator_set_voltage(cpr_vreg->vdd_apc, new_volt, max_volt);
406 if (rc)
407 pr_err("set: vdd_apc = %d uV: rc=%d\n", new_volt, rc);
408 return rc;
409}
410
411static int cpr_mx_get(struct cpr_regulator *cpr_vreg, int corner, int apc_volt)
412{
413 int vdd_mx;
414
415 switch (cpr_vreg->vdd_mx_vmin_method) {
416 case VDD_MX_VMIN_APC:
417 vdd_mx = apc_volt;
418 break;
419 case VDD_MX_VMIN_APC_CORNER_CEILING:
420 vdd_mx = cpr_vreg->ceiling_volt[corner];
421 break;
422 case VDD_MX_VMIN_APC_SLOW_CORNER_CEILING:
423 vdd_mx = cpr_vreg->pvs_corner_v[APC_PVS_SLOW]
424 [CPR_CORNER_TURBO];
425 break;
426 case VDD_MX_VMIN_MX_VMAX:
427 vdd_mx = cpr_vreg->vdd_mx_vmax;
428 break;
429 default:
430 vdd_mx = 0;
431 break;
432 }
433
434 return vdd_mx;
435}
436
437static int cpr_mx_set(struct cpr_regulator *cpr_vreg, int corner,
438 int vdd_mx_vmin)
439{
440 int rc;
441
442 rc = regulator_set_voltage(cpr_vreg->vdd_mx, vdd_mx_vmin,
443 cpr_vreg->vdd_mx_vmax);
444 cpr_debug("[corner:%d] %d uV\n", corner, vdd_mx_vmin);
445 if (!rc)
446 cpr_vreg->vdd_mx_vmin = vdd_mx_vmin;
447 else
448 pr_err("set: vdd_mx [%d] = %d uV: rc=%d\n",
449 corner, vdd_mx_vmin, rc);
450 return rc;
451}
452
453static int cpr_scale_voltage(struct cpr_regulator *cpr_vreg, int corner,
454 int new_apc_volt, enum voltage_change_dir dir)
455{
456 int rc = 0, vdd_mx_vmin = 0;
457
458 /* No MX scaling if no vdd_mx */
459 if (cpr_vreg->vdd_mx == NULL)
460 dir = NO_CHANGE;
461
462 if (dir != NO_CHANGE) {
463 /* Determine the vdd_mx voltage */
464 vdd_mx_vmin = cpr_mx_get(cpr_vreg, corner, new_apc_volt);
465 }
466
467 if (vdd_mx_vmin && dir == UP) {
468 if (vdd_mx_vmin != cpr_vreg->vdd_mx_vmin)
469 rc = cpr_mx_set(cpr_vreg, corner, vdd_mx_vmin);
470 }
471
472 if (!rc)
473 rc = cpr_apc_set(cpr_vreg, new_apc_volt);
474
475 if (!rc && vdd_mx_vmin && dir == DOWN) {
476 if (vdd_mx_vmin != cpr_vreg->vdd_mx_vmin)
477 rc = cpr_mx_set(cpr_vreg, corner, vdd_mx_vmin);
478 }
479
480 return rc;
481}
482
483static void cpr_scale(struct cpr_regulator *cpr_vreg,
484 enum voltage_change_dir dir)
485{
486 u32 reg_val, error_steps, reg_mask;
487 int last_volt, new_volt, corner;
488
489 corner = cpr_vreg->corner;
490
491 reg_val = cpr_read(cpr_vreg, REG_RBCPR_RESULT_0);
492
493 error_steps = (reg_val >> RBCPR_RESULT0_ERROR_STEPS_SHIFT)
494 & RBCPR_RESULT0_ERROR_STEPS_MASK;
495 last_volt = cpr_vreg->last_volt[corner];
496
497 cpr_debug("last_volt[corner:%d] = %d uV\n", corner, last_volt);
498
499 if (dir == UP) {
500 cpr_debug("Up: cpr status = 0x%08x (error_steps=%d)\n",
501 reg_val, error_steps);
502
503 if (last_volt >= cpr_vreg->ceiling_volt[corner]) {
504 cpr_debug("[corn:%d] @ ceiling: %d >= %d: NACK\n",
505 corner, last_volt,
506 cpr_vreg->ceiling_volt[corner]);
507 cpr_irq_clr_nack(cpr_vreg);
508
509 /* Maximize the UP threshold */
510 reg_mask = RBCPR_CTL_UP_THRESHOLD_MASK <<
511 RBCPR_CTL_UP_THRESHOLD_SHIFT;
512 reg_val = reg_mask;
513 cpr_ctl_modify(cpr_vreg, reg_mask, reg_val);
514 return;
515 }
516
517 if (error_steps > cpr_vreg->vdd_apc_step_up_limit) {
518 cpr_debug("%d is over up-limit(%d): Clamp\n",
519 error_steps,
520 cpr_vreg->vdd_apc_step_up_limit);
521 error_steps = cpr_vreg->vdd_apc_step_up_limit;
522 }
523
524 /* Calculate new voltage */
525 new_volt = last_volt + (error_steps * cpr_vreg->step_volt);
526 if (new_volt > cpr_vreg->ceiling_volt[corner]) {
527 cpr_debug("new_volt(%d) >= ceiling_volt(%d): Clamp\n",
528 new_volt, cpr_vreg->ceiling_volt[corner]);
529 new_volt = cpr_vreg->ceiling_volt[corner];
530 }
531
532 if (cpr_scale_voltage(cpr_vreg, corner, new_volt, dir)) {
533 cpr_irq_clr_nack(cpr_vreg);
534 return;
535 }
536 cpr_vreg->last_volt[corner] = new_volt;
537
538 /* Restore default threshold for DOWN */
539 reg_mask = RBCPR_CTL_DN_THRESHOLD_MASK <<
540 RBCPR_CTL_DN_THRESHOLD_SHIFT;
541 reg_val = cpr_vreg->down_threshold <<
542 RBCPR_CTL_DN_THRESHOLD_SHIFT;
543 /* and disable auto nack down */
544 reg_mask |= RBCPR_CTL_SW_AUTO_CONT_NACK_DN_EN;
545
546 cpr_ctl_modify(cpr_vreg, reg_mask, reg_val);
547
548 /* Re-enable default interrupts */
549 cpr_irq_set(cpr_vreg, CPR_INT_DEFAULT);
550
551 /* Ack */
552 cpr_irq_clr_ack(cpr_vreg);
553
554 cpr_debug("UP: -> new_volt = %d uV\n", new_volt);
555 } else if (dir == DOWN) {
556 cpr_debug("Down: cpr status = 0x%08x (error_steps=%d)\n",
557 reg_val, error_steps);
558
559 if (last_volt <= cpr_vreg->floor_volt[corner]) {
560 cpr_debug("[corn:%d] @ floor: %d <= %d: NACK\n",
561 corner, last_volt,
562 cpr_vreg->floor_volt[corner]);
563 cpr_irq_clr_nack(cpr_vreg);
564
565 /* Maximize the DOWN threshold */
566 reg_mask = RBCPR_CTL_DN_THRESHOLD_MASK <<
567 RBCPR_CTL_DN_THRESHOLD_SHIFT;
568 reg_val = reg_mask;
569
570 /* Enable auto nack down */
571 reg_mask |= RBCPR_CTL_SW_AUTO_CONT_NACK_DN_EN;
572 reg_val |= RBCPR_CTL_SW_AUTO_CONT_NACK_DN_EN;
573
574 cpr_ctl_modify(cpr_vreg, reg_mask, reg_val);
575
576 /* Disable DOWN interrupt */
577 cpr_irq_set(cpr_vreg, CPR_INT_DEFAULT & ~CPR_INT_DOWN);
578
579 return;
580 }
581
582 if (error_steps > cpr_vreg->vdd_apc_step_down_limit) {
583 cpr_debug("%d is over down-limit(%d): Clamp\n",
584 error_steps,
585 cpr_vreg->vdd_apc_step_down_limit);
586 error_steps = cpr_vreg->vdd_apc_step_down_limit;
587 }
588
589 /* Calculte new voltage */
590 new_volt = last_volt - (error_steps * cpr_vreg->step_volt);
591 if (new_volt < cpr_vreg->floor_volt[corner]) {
592 cpr_debug("new_volt(%d) < floor_volt(%d): Clamp\n",
593 new_volt, cpr_vreg->floor_volt[corner]);
594 new_volt = cpr_vreg->floor_volt[corner];
595 }
596
597 if (cpr_scale_voltage(cpr_vreg, corner, new_volt, dir)) {
598 cpr_irq_clr_nack(cpr_vreg);
599 return;
600 }
601 cpr_vreg->last_volt[corner] = new_volt;
602
603 /* Restore default threshold for UP */
604 reg_mask = RBCPR_CTL_UP_THRESHOLD_MASK <<
605 RBCPR_CTL_UP_THRESHOLD_SHIFT;
606 reg_val = cpr_vreg->up_threshold <<
607 RBCPR_CTL_UP_THRESHOLD_SHIFT;
608 cpr_ctl_modify(cpr_vreg, reg_mask, reg_val);
609
610 /* Ack */
611 cpr_irq_clr_ack(cpr_vreg);
612
613 cpr_debug("DOWN: -> new_volt = %d uV\n", new_volt);
614 }
615}
616
617static irqreturn_t cpr_irq_handler(int irq, void *dev)
618{
619 struct cpr_regulator *cpr_vreg = dev;
620 u32 reg_val;
621
622 mutex_lock(&cpr_vreg->cpr_mutex);
623
624 reg_val = cpr_read(cpr_vreg, REG_RBIF_IRQ_STATUS);
625 cpr_debug("IRQ_STATUS = 0x%02X\n", reg_val);
626
627 if (!cpr_is_allowed(cpr_vreg)) {
628 reg_val = cpr_read(cpr_vreg, REG_RBCPR_CTL);
629 pr_err("Interrupt broken? RBCPR_CTL = 0x%02X\n", reg_val);
630 goto _exit;
631 }
632
633 /* Following sequence of handling is as per each IRQ's priority */
634 if (reg_val & CPR_INT_UP) {
635 cpr_scale(cpr_vreg, UP);
636 } else if (reg_val & CPR_INT_DOWN) {
637 cpr_scale(cpr_vreg, DOWN);
638 } else if (reg_val & CPR_INT_MIN) {
639 cpr_irq_clr_nack(cpr_vreg);
640 } else if (reg_val & CPR_INT_MAX) {
641 cpr_irq_clr_nack(cpr_vreg);
642 } else if (reg_val & CPR_INT_MID) {
643 /* RBCPR_CTL_SW_AUTO_CONT_ACK_EN is enabled */
644 cpr_debug("IRQ occured for Mid Flag\n");
645 } else {
646 pr_err("IRQ occured for unknown flag (0x%08x)\n", reg_val);
647 }
648
649 /* Save register values for the corner */
650 cpr_corner_save(cpr_vreg, cpr_vreg->corner);
651
652_exit:
653 mutex_unlock(&cpr_vreg->cpr_mutex);
654 return IRQ_HANDLED;
655}
656
Willie Ruan2635a852013-03-17 16:33:42 -0700657static int cpr_regulator_is_enabled(struct regulator_dev *rdev)
658{
659 struct cpr_regulator *cpr_vreg = rdev_get_drvdata(rdev);
660
Willie Ruanb17b7832013-04-08 21:30:27 -0700661 return cpr_vreg->vreg_enabled;
Willie Ruan2635a852013-03-17 16:33:42 -0700662}
663
664static int cpr_regulator_enable(struct regulator_dev *rdev)
665{
666 struct cpr_regulator *cpr_vreg = rdev_get_drvdata(rdev);
Willie Ruan6d502392013-03-27 15:40:28 -0700667 int rc = 0;
668
669 /* Enable dependency power before vdd_apc */
670 if (cpr_vreg->vdd_mx) {
671 rc = regulator_enable(cpr_vreg->vdd_mx);
672 if (rc) {
673 pr_err("regulator_enable: vdd_mx: rc=%d\n", rc);
674 return rc;
675 }
676 }
Willie Ruan2635a852013-03-17 16:33:42 -0700677
678 rc = regulator_enable(cpr_vreg->vdd_apc);
679 if (!rc)
Willie Ruanb17b7832013-04-08 21:30:27 -0700680 cpr_vreg->vreg_enabled = true;
Willie Ruan6d502392013-03-27 15:40:28 -0700681 else
682 pr_err("regulator_enable: vdd_apc: rc=%d\n", rc);
683
Willie Ruan2635a852013-03-17 16:33:42 -0700684 return rc;
685}
686
687static int cpr_regulator_disable(struct regulator_dev *rdev)
688{
689 struct cpr_regulator *cpr_vreg = rdev_get_drvdata(rdev);
690 int rc;
691
692 rc = regulator_disable(cpr_vreg->vdd_apc);
Willie Ruan6d502392013-03-27 15:40:28 -0700693 if (!rc) {
694 if (cpr_vreg->vdd_mx)
695 rc = regulator_disable(cpr_vreg->vdd_mx);
696
697 if (rc)
698 pr_err("regulator_disable: vdd_mx: rc=%d\n", rc);
699 else
Willie Ruanb17b7832013-04-08 21:30:27 -0700700 cpr_vreg->vreg_enabled = false;
Willie Ruan6d502392013-03-27 15:40:28 -0700701 } else {
702 pr_err("regulator_disable: vdd_apc: rc=%d\n", rc);
703 }
704
Willie Ruan2635a852013-03-17 16:33:42 -0700705 return rc;
706}
707
708static int cpr_regulator_set_voltage(struct regulator_dev *rdev,
Willie Ruanb17b7832013-04-08 21:30:27 -0700709 int corner, int corner_max, unsigned *selector)
Willie Ruan2635a852013-03-17 16:33:42 -0700710{
711 struct cpr_regulator *cpr_vreg = rdev_get_drvdata(rdev);
712 int rc;
Willie Ruanb17b7832013-04-08 21:30:27 -0700713 int new_volt;
714 enum voltage_change_dir change_dir = NO_CHANGE;
Willie Ruan2635a852013-03-17 16:33:42 -0700715
Willie Ruanb17b7832013-04-08 21:30:27 -0700716 mutex_lock(&cpr_vreg->cpr_mutex);
Willie Ruan6d502392013-03-27 15:40:28 -0700717
Willie Ruanb17b7832013-04-08 21:30:27 -0700718 if (cpr_is_allowed(cpr_vreg)) {
719 cpr_ctl_disable(cpr_vreg);
720 new_volt = cpr_vreg->last_volt[corner];
Willie Ruan6d502392013-03-27 15:40:28 -0700721 } else {
Willie Ruanb17b7832013-04-08 21:30:27 -0700722 new_volt = cpr_vreg->pvs_corner_v[cpr_vreg->process][corner];
Willie Ruan6d502392013-03-27 15:40:28 -0700723 }
724
Willie Ruanb17b7832013-04-08 21:30:27 -0700725 cpr_debug("[corner:%d] = %d uV\n", corner, new_volt);
Willie Ruan6d502392013-03-27 15:40:28 -0700726
Willie Ruanb17b7832013-04-08 21:30:27 -0700727 if (corner > cpr_vreg->corner)
728 change_dir = UP;
729 else if (corner < cpr_vreg->corner)
730 change_dir = DOWN;
731
732 rc = cpr_scale_voltage(cpr_vreg, corner, new_volt, change_dir);
733 if (rc)
734 goto _exit;
735
736 if (cpr_is_allowed(cpr_vreg)) {
737 cpr_irq_clr(cpr_vreg);
738 cpr_corner_switch(cpr_vreg, corner);
739 cpr_ctl_enable(cpr_vreg);
Willie Ruan6d502392013-03-27 15:40:28 -0700740 }
Willie Ruan2635a852013-03-17 16:33:42 -0700741
Willie Ruanb17b7832013-04-08 21:30:27 -0700742 cpr_vreg->corner = corner;
743
744_exit:
745 mutex_unlock(&cpr_vreg->cpr_mutex);
746
Willie Ruan2635a852013-03-17 16:33:42 -0700747 return rc;
748}
749
750static int cpr_regulator_get_voltage(struct regulator_dev *rdev)
751{
752 struct cpr_regulator *cpr_vreg = rdev_get_drvdata(rdev);
753
754 return cpr_vreg->corner;
755}
756
757static struct regulator_ops cpr_corner_ops = {
758 .enable = cpr_regulator_enable,
759 .disable = cpr_regulator_disable,
760 .is_enabled = cpr_regulator_is_enabled,
761 .set_voltage = cpr_regulator_set_voltage,
762 .get_voltage = cpr_regulator_get_voltage,
763};
764
Willie Ruanb17b7832013-04-08 21:30:27 -0700765#ifdef CONFIG_PM
766static int cpr_suspend(struct cpr_regulator *cpr_vreg)
767{
768 cpr_debug("suspend\n");
769
770 cpr_ctl_disable(cpr_vreg);
771 disable_irq(cpr_vreg->cpr_irq);
772
773 cpr_irq_clr(cpr_vreg);
774 cpr_regs_save(cpr_vreg);
775
776 return 0;
777}
778
779static int cpr_resume(struct cpr_regulator *cpr_vreg)
780
781{
782 cpr_debug("resume\n");
783
784 cpr_regs_restore(cpr_vreg);
785 cpr_irq_clr(cpr_vreg);
786
787 enable_irq(cpr_vreg->cpr_irq);
788 cpr_ctl_enable(cpr_vreg);
789
790 return 0;
791}
792
793static int cpr_regulator_suspend(struct platform_device *pdev,
794 pm_message_t state)
795{
796 struct cpr_regulator *cpr_vreg = platform_get_drvdata(pdev);
797
798 if (cpr_is_allowed(cpr_vreg))
799 return cpr_suspend(cpr_vreg);
800 else
801 return 0;
802}
803
804static int cpr_regulator_resume(struct platform_device *pdev)
805{
806 struct cpr_regulator *cpr_vreg = platform_get_drvdata(pdev);
807
808 if (cpr_is_allowed(cpr_vreg))
809 return cpr_resume(cpr_vreg);
810 else
811 return 0;
812}
813#else
814#define cpr_regulator_suspend NULL
815#define cpr_regulator_resume NULL
816#endif
817
818static void cpr_config(struct cpr_regulator *cpr_vreg)
819{
820 int i;
821 u32 val, gcnt;
822
823 /* Disable interrupt and CPR */
824 cpr_write(cpr_vreg, REG_RBIF_IRQ_EN(cpr_vreg->irq_line), 0);
825 cpr_write(cpr_vreg, REG_RBCPR_CTL, 0);
826
827 /* Program the default HW Ceiling, Floor and vlevel */
828 val = ((RBIF_LIMIT_CEILING_DEFAULT & RBIF_LIMIT_CEILING_MASK)
829 << RBIF_LIMIT_CEILING_SHIFT)
830 | (RBIF_LIMIT_FLOOR_DEFAULT & RBIF_LIMIT_FLOOR_MASK);
831 cpr_write(cpr_vreg, REG_RBIF_LIMIT, val);
832 cpr_write(cpr_vreg, REG_RBIF_SW_VLEVEL, RBIF_SW_VLEVEL_DEFAULT);
833
834 /* Clear the target quotient value and gate count of all ROs */
835 for (i = 0; i < CPR_NUM_RING_OSC; i++)
836 cpr_write(cpr_vreg, REG_RBCPR_GCNT_TARGET(i), 0);
837
838 /* Init and save gcnt */
839 gcnt = (cpr_vreg->ref_clk_khz * cpr_vreg->gcnt_time_us) / 1000;
840 gcnt = (gcnt & RBCPR_GCNT_TARGET_GCNT_MASK) <<
841 RBCPR_GCNT_TARGET_GCNT_SHIFT;
842 cpr_vreg->gcnt = gcnt;
843
844 /* Program the step quotient and idle clocks */
845 val = ((cpr_vreg->idle_clocks & RBCPR_STEP_QUOT_IDLE_CLK_MASK)
846 << RBCPR_STEP_QUOT_IDLE_CLK_SHIFT) |
847 (cpr_vreg->step_quotient & RBCPR_STEP_QUOT_STEPQUOT_MASK);
848 cpr_write(cpr_vreg, REG_RBCPR_STEP_QUOT, val);
849
850 /* Program the delay count for the timer */
851 val = (cpr_vreg->ref_clk_khz * cpr_vreg->timer_delay_us) / 1000;
852 cpr_write(cpr_vreg, REG_RBCPR_TIMER_INTERVAL, val);
853 pr_info("Timer count: 0x%0x (for %d us)\n", val,
854 cpr_vreg->timer_delay_us);
855
856 /* Program Consecutive Up & Down */
857 val = ((cpr_vreg->timer_cons_down & RBIF_TIMER_ADJ_CONS_DOWN_MASK)
858 << RBIF_TIMER_ADJ_CONS_DOWN_SHIFT) |
859 (cpr_vreg->timer_cons_up & RBIF_TIMER_ADJ_CONS_UP_MASK);
860 cpr_write(cpr_vreg, REG_RBIF_TIMER_ADJUST, val);
861
862 /* Program the control register */
863 cpr_vreg->up_threshold &= RBCPR_CTL_UP_THRESHOLD_MASK;
864 cpr_vreg->down_threshold &= RBCPR_CTL_DN_THRESHOLD_MASK;
865 val = (cpr_vreg->up_threshold << RBCPR_CTL_UP_THRESHOLD_SHIFT)
866 | (cpr_vreg->down_threshold << RBCPR_CTL_DN_THRESHOLD_SHIFT);
867 val |= RBCPR_CTL_TIMER_EN | RBCPR_CTL_COUNT_MODE;
868 val |= RBCPR_CTL_SW_AUTO_CONT_ACK_EN;
869 cpr_write(cpr_vreg, REG_RBCPR_CTL, val);
870
871 /* Registers to save & restore for suspend */
872 cpr_vreg->save_regs[0] = REG_RBCPR_TIMER_INTERVAL;
873 cpr_vreg->save_regs[1] = REG_RBCPR_STEP_QUOT;
874 cpr_vreg->save_regs[2] = REG_RBIF_TIMER_ADJUST;
875 cpr_vreg->save_regs[3] = REG_RBIF_LIMIT;
876 cpr_vreg->save_regs[4] = REG_RBIF_SW_VLEVEL;
877 cpr_vreg->save_regs[5] = REG_RBIF_IRQ_EN(cpr_vreg->irq_line);
878 cpr_vreg->save_regs[6] = REG_RBCPR_CTL;
879 cpr_vreg->save_regs[7] = REG_RBCPR_GCNT_TARGET
880 (cpr_vreg->cpr_fuse_ro_sel[CPR_CORNER_SVS]);
881 cpr_vreg->save_regs[8] = REG_RBCPR_GCNT_TARGET
882 (cpr_vreg->cpr_fuse_ro_sel[CPR_CORNER_NORMAL]);
883 cpr_vreg->save_regs[9] = REG_RBCPR_GCNT_TARGET
884 (cpr_vreg->cpr_fuse_ro_sel[CPR_CORNER_TURBO]);
885
886 cpr_irq_set(cpr_vreg, CPR_INT_DEFAULT);
887
888 cpr_corner_save(cpr_vreg, CPR_CORNER_SVS);
889 cpr_corner_save(cpr_vreg, CPR_CORNER_NORMAL);
890 cpr_corner_save(cpr_vreg, CPR_CORNER_TURBO);
891}
892
893static int __init cpr_pvs_init(struct cpr_regulator *cpr_vreg)
Willie Ruan2635a852013-03-17 16:33:42 -0700894{
895 void __iomem *efuse_base;
Willie Ruanb17b7832013-04-08 21:30:27 -0700896 u32 efuse_bits, redundant, shift, mask;
897 int i, process;
Willie Ruan2635a852013-03-17 16:33:42 -0700898
Willie Ruanb17b7832013-04-08 21:30:27 -0700899 efuse_base = ioremap(cpr_vreg->pvs_efuse, 4);
Willie Ruan2635a852013-03-17 16:33:42 -0700900 if (!efuse_base) {
Willie Ruanb17b7832013-04-08 21:30:27 -0700901 pr_err("Unable to map pvs_efuse 0x%08x\n",
902 cpr_vreg->pvs_efuse);
Willie Ruan2635a852013-03-17 16:33:42 -0700903 return -EINVAL;
904 }
905
906 efuse_bits = readl_relaxed(efuse_base);
907
908 /* Construct PVS process # from the efuse bits */
Willie Ruanb17b7832013-04-08 21:30:27 -0700909 redundant = (efuse_bits >> PVS_FUSE_REDUNDANT_SHIFT)
910 & PVS_FUSE_REDUNDANT_MASK;
911 if (redundant == 2)
912 shift = PVS_FUSE_BINS_REDUNDANT_SHIFT;
913 else
914 shift = PVS_FUSE_BINS_SHIFT;
915 mask = (1 << cpr_vreg->num_efuse_bits) - 1;
916 cpr_vreg->pvs_bin = (efuse_bits >> shift) & mask;
Willie Ruan2635a852013-03-17 16:33:42 -0700917
Willie Ruanb17b7832013-04-08 21:30:27 -0700918 /* Set ceiling max and use it for APC_PVS_NO */
919 cpr_vreg->ceiling_max =
920 cpr_vreg->pvs_corner_v[APC_PVS_SLOW][CPR_CORNER_TURBO];
Willie Ruan2635a852013-03-17 16:33:42 -0700921
922 iounmap(efuse_base);
923
Willie Ruanb17b7832013-04-08 21:30:27 -0700924 process = cpr_vreg->pvs_bin_process[cpr_vreg->pvs_bin];
925 pr_info("[0x%08X] = 0x%08X, n_bits=%d, bin=%d (%d) [redundant=%d]\n",
926 cpr_vreg->pvs_efuse, efuse_bits, cpr_vreg->num_efuse_bits,
927 cpr_vreg->pvs_bin, process, redundant);
928 for (i = APC_PVS_SLOW; i < NUM_APC_PVS; i++) {
929 pr_info("[%d] [%d %d %d] uV\n", i,
930 cpr_vreg->pvs_corner_v[i][CPR_CORNER_SVS],
931 cpr_vreg->pvs_corner_v[i][CPR_CORNER_NORMAL],
932 cpr_vreg->pvs_corner_v[i][CPR_CORNER_TURBO]);
933 }
934
935 if (process == APC_PVS_NO || process >= NUM_APC_PVS) {
936 pr_err("Bin=%d (%d) is out of spec. Assume SLOW.\n",
937 cpr_vreg->pvs_bin, process);
938 process = APC_PVS_SLOW;
939 }
940
941 cpr_vreg->process = process;
Willie Ruan2635a852013-03-17 16:33:42 -0700942
943 return 0;
944}
945
Willie Ruanb17b7832013-04-08 21:30:27 -0700946#define CPR_PROP_READ_U32(of_node, cpr_property, cpr_config, rc) \
947do { \
948 if (!rc) { \
949 rc = of_property_read_u32(of_node, \
950 "qcom," cpr_property, \
951 cpr_config); \
952 if (rc) { \
953 pr_err("Missing " #cpr_property \
954 ": rc = %d\n", rc); \
955 } \
956 } \
957} while (0)
958
959static int __init cpr_apc_init(struct platform_device *pdev,
960 struct cpr_regulator *cpr_vreg)
Willie Ruan2635a852013-03-17 16:33:42 -0700961{
Willie Ruan6d502392013-03-27 15:40:28 -0700962 struct device_node *of_node = pdev->dev.of_node;
963 int rc;
964
Willie Ruan2635a852013-03-17 16:33:42 -0700965 cpr_vreg->vdd_apc = devm_regulator_get(&pdev->dev, "vdd-apc");
966 if (IS_ERR_OR_NULL(cpr_vreg->vdd_apc)) {
Willie Ruan6d502392013-03-27 15:40:28 -0700967 rc = PTR_RET(cpr_vreg->vdd_apc);
968 if (rc != -EPROBE_DEFER)
969 pr_err("devm_regulator_get: rc=%d\n", rc);
970 return rc;
Willie Ruan2635a852013-03-17 16:33:42 -0700971 }
972
Willie Ruan6d502392013-03-27 15:40:28 -0700973 /* Check dependencies */
974 if (of_property_read_bool(of_node, "vdd-mx-supply")) {
975 cpr_vreg->vdd_mx = devm_regulator_get(&pdev->dev, "vdd-mx");
976 if (IS_ERR_OR_NULL(cpr_vreg->vdd_mx)) {
977 rc = PTR_RET(cpr_vreg->vdd_mx);
978 if (rc != -EPROBE_DEFER)
979 pr_err("devm_regulator_get: vdd-mx: rc=%d\n",
980 rc);
981 return rc;
982 }
983 }
984
985 /* Parse dependency parameters */
986 if (cpr_vreg->vdd_mx) {
987 rc = of_property_read_u32(of_node, "qcom,vdd-mx-vmax",
988 &cpr_vreg->vdd_mx_vmax);
989 if (rc < 0) {
990 pr_err("vdd-mx-vmax missing: rc=%d\n", rc);
991 return rc;
992 }
993
994 rc = of_property_read_u32(of_node, "qcom,vdd-mx-vmin-method",
995 &cpr_vreg->vdd_mx_vmin_method);
996 if (rc < 0) {
997 pr_err("vdd-mx-vmin-method missing: rc=%d\n", rc);
998 return rc;
999 }
1000 if (cpr_vreg->vdd_mx_vmin_method > VDD_MX_VMIN_MX_VMAX) {
1001 pr_err("Invalid vdd-mx-vmin-method(%d)\n",
1002 cpr_vreg->vdd_mx_vmin_method);
1003 return -EINVAL;
1004 }
1005 }
1006
1007 return 0;
Willie Ruan2635a852013-03-17 16:33:42 -07001008}
1009
Willie Ruanb17b7832013-04-08 21:30:27 -07001010static void cpr_apc_exit(struct cpr_regulator *cpr_vreg)
Willie Ruan2635a852013-03-17 16:33:42 -07001011{
Willie Ruanb17b7832013-04-08 21:30:27 -07001012 if (cpr_vreg->vreg_enabled) {
Willie Ruan2635a852013-03-17 16:33:42 -07001013 regulator_disable(cpr_vreg->vdd_apc);
Willie Ruan6d502392013-03-27 15:40:28 -07001014
1015 if (cpr_vreg->vdd_mx)
1016 regulator_disable(cpr_vreg->vdd_mx);
1017 }
Willie Ruan2635a852013-03-17 16:33:42 -07001018}
1019
Willie Ruanb17b7832013-04-08 21:30:27 -07001020static int __init cpr_init_cpr_efuse(struct cpr_regulator *cpr_vreg)
1021{
1022 void __iomem *efuse_base;
1023 u32 ro_sel, val;
1024 u64 fuse_bits;
1025 int ro_sel_shift[CPR_CORNER_MAX];
1026
1027 efuse_base = ioremap(cpr_vreg->cpr_fuse_addr, 16);
1028 if (!efuse_base) {
1029 pr_err("Unable to map cpr_fuse_addr 0x%08x\n",
1030 cpr_vreg->cpr_fuse_addr);
1031 return -EINVAL;
1032 }
1033
1034 cpr_vreg->cpr_fuse_bits = readll_relaxed(efuse_base);
1035 cpr_vreg->cpr_fuse_bits_2 = readll_relaxed(efuse_base + 8);
1036
1037 iounmap(efuse_base);
1038
1039 /* Read the control bits of eFuse */
1040 cpr_vreg->cpr_fuse_disable = (cpr_vreg->cpr_fuse_bits >>
1041 CPR_FUSE_DISABLE_CPR_SHIFT) & 0x01;
1042 cpr_vreg->cpr_fuse_local = (cpr_vreg->cpr_fuse_bits >>
1043 CPR_FUSE_LOCAL_APPROACH_SHIFT) & 0x01;
1044 cpr_vreg->cpr_fuse_redundancy = (cpr_vreg->cpr_fuse_bits >>
1045 CPR_FUSE_REDUNDANT_SHIFT) & 0x01;
1046
1047 pr_info("[0x%08X] = 0x%llx\n", cpr_vreg->cpr_fuse_addr,
1048 cpr_vreg->cpr_fuse_bits);
1049 pr_info("disable = %d, local = %d, redundancy = %d\n",
1050 cpr_vreg->cpr_fuse_disable,
1051 cpr_vreg->cpr_fuse_local,
1052 cpr_vreg->cpr_fuse_redundancy);
1053 pr_info("[0x%08X] = 0x%llx\n", cpr_vreg->cpr_fuse_addr + 8,
1054 cpr_vreg->cpr_fuse_bits_2);
1055
1056 if (cpr_vreg->cpr_fuse_redundancy == 0) {
1057 fuse_bits = cpr_vreg->cpr_fuse_bits;
1058 ro_sel_shift[CPR_CORNER_SVS] = 54;
1059 ro_sel_shift[CPR_CORNER_NORMAL] = 38;
1060 ro_sel_shift[CPR_CORNER_TURBO] = 41;
1061 } else {
1062 fuse_bits = cpr_vreg->cpr_fuse_bits_2;
1063 ro_sel_shift[CPR_CORNER_SVS] = 46;
1064 ro_sel_shift[CPR_CORNER_NORMAL] = 36;
1065 ro_sel_shift[CPR_CORNER_TURBO] = 39;
1066 }
1067
1068 /* SVS */
1069 ro_sel = (fuse_bits >> ro_sel_shift[CPR_CORNER_SVS])
1070 & CPR_FUSE_RO_SEL_BITS_MASK;
1071 val = (fuse_bits >> CPR_FUSE_TARGET_QUOT_SVS_SHIFT)
1072 & CPR_FUSE_TARGET_QUOT_BITS_MASK;
1073 cpr_vreg->cpr_fuse_target_quot[CPR_CORNER_SVS] = val;
1074 cpr_vreg->cpr_fuse_ro_sel[CPR_CORNER_SVS] = ro_sel;
1075 pr_info("SVS: ro_sel = %d, target quot = 0x%04x\n", ro_sel, val);
1076
1077 /* Nominal */
1078 ro_sel = (fuse_bits >> ro_sel_shift[CPR_CORNER_NORMAL])
1079 & CPR_FUSE_RO_SEL_BITS_MASK;
1080 val = (fuse_bits >> CPR_FUSE_TARGET_QUOT_NOMINAL_SHIFT)
1081 & CPR_FUSE_TARGET_QUOT_BITS_MASK;
1082 cpr_vreg->cpr_fuse_target_quot[CPR_CORNER_NORMAL] = val;
1083 cpr_vreg->cpr_fuse_ro_sel[CPR_CORNER_NORMAL] = ro_sel;
1084 pr_info("Nominal: ro_sel = %d, target quot = 0x%04x\n", ro_sel, val);
1085
1086 /* Turbo */
1087 ro_sel = (fuse_bits >> ro_sel_shift[CPR_CORNER_TURBO])
1088 & CPR_FUSE_RO_SEL_BITS_MASK;
1089 val = (fuse_bits >> CPR_FUSE_TARGET_QUOT_TURBO_SHIFT)
1090 & CPR_FUSE_TARGET_QUOT_BITS_MASK;
1091 cpr_vreg->cpr_fuse_target_quot[CPR_CORNER_TURBO] = val;
1092 cpr_vreg->cpr_fuse_ro_sel[CPR_CORNER_TURBO] = ro_sel;
1093 pr_info("Turbo: ro_sel = %d, target quot = 0x%04x\n", ro_sel, val);
1094
1095 if (!cpr_vreg->cpr_fuse_bits) {
1096 cpr_vreg->cpr_fuse_disable = 1;
1097 pr_err("cpr_fuse_bits = 0: set cpr_fuse_disable = 1\n");
1098 }
1099
1100 return 0;
1101}
1102
1103static int __init cpr_init_cpr_voltages(struct cpr_regulator *cpr_vreg)
1104{
1105 int i;
1106
1107 /* Construct CPR voltage limits */
1108 for (i = CPR_CORNER_SVS; i < CPR_CORNER_MAX; i++) {
1109 cpr_vreg->floor_volt[i] =
1110 cpr_vreg->pvs_corner_v[APC_PVS_FAST][i];
1111 cpr_vreg->ceiling_volt[i] =
1112 cpr_vreg->pvs_corner_v[APC_PVS_SLOW][i];
1113 cpr_vreg->last_volt[i] =
1114 cpr_vreg->pvs_corner_v[cpr_vreg->process][i];
1115 }
1116
1117 return 0;
1118}
1119
1120static int __init cpr_init_cpr_parameters(struct platform_device *pdev,
1121 struct cpr_regulator *cpr_vreg)
1122{
1123 struct device_node *of_node = pdev->dev.of_node;
1124 int rc = 0;
1125
1126 CPR_PROP_READ_U32(of_node, "cpr-ref-clk",
1127 &cpr_vreg->ref_clk_khz, rc);
1128 if (rc)
1129 return rc;
1130 CPR_PROP_READ_U32(of_node, "cpr-timer-delay",
1131 &cpr_vreg->timer_delay_us, rc);
1132 if (rc)
1133 return rc;
1134 CPR_PROP_READ_U32(of_node, "cpr-timer-cons-up",
1135 &cpr_vreg->timer_cons_up, rc);
1136 if (rc)
1137 return rc;
1138 CPR_PROP_READ_U32(of_node, "cpr-timer-cons-down",
1139 &cpr_vreg->timer_cons_down, rc);
1140 if (rc)
1141 return rc;
1142 CPR_PROP_READ_U32(of_node, "cpr-irq-line",
1143 &cpr_vreg->irq_line, rc);
1144 if (rc)
1145 return rc;
1146 CPR_PROP_READ_U32(of_node, "cpr-step-quotient",
1147 &cpr_vreg->step_quotient, rc);
1148 if (rc)
1149 return rc;
1150 CPR_PROP_READ_U32(of_node, "cpr-up-threshold",
1151 &cpr_vreg->up_threshold, rc);
1152 if (rc)
1153 return rc;
1154 CPR_PROP_READ_U32(of_node, "cpr-down-threshold",
1155 &cpr_vreg->down_threshold, rc);
1156 if (rc)
1157 return rc;
1158 CPR_PROP_READ_U32(of_node, "cpr-idle-clocks",
1159 &cpr_vreg->idle_clocks, rc);
1160 if (rc)
1161 return rc;
1162 CPR_PROP_READ_U32(of_node, "cpr-gcnt-time",
1163 &cpr_vreg->gcnt_time_us, rc);
1164 if (rc)
1165 return rc;
1166 CPR_PROP_READ_U32(of_node, "vdd-apc-step-up-limit",
1167 &cpr_vreg->vdd_apc_step_up_limit, rc);
1168 if (rc)
1169 return rc;
1170 CPR_PROP_READ_U32(of_node, "vdd-apc-step-down-limit",
1171 &cpr_vreg->vdd_apc_step_down_limit, rc);
1172 if (rc)
1173 return rc;
1174 CPR_PROP_READ_U32(of_node, "cpr-apc-volt-step",
1175 &cpr_vreg->step_volt, rc);
1176 if (rc)
1177 return rc;
1178
1179 /* Init module parameter with the DT value */
1180 cpr_vreg->enable = of_property_read_bool(of_node, "qcom,cpr-enable");
1181 cpr_enable = (int) cpr_vreg->enable;
1182 pr_info("CPR is %s by default.\n",
1183 cpr_vreg->enable ? "enabled" : "disabled");
1184
1185 return rc;
1186}
1187
1188static int __init cpr_init_cpr(struct platform_device *pdev,
1189 struct cpr_regulator *cpr_vreg)
1190{
1191 struct resource *res;
1192 int rc = 0;
1193
1194 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1195 "cpr_efuse");
1196 if (!res || !res->start) {
1197 pr_err("cpr_efuse missing: res=%p\n", res);
1198 return -EINVAL;
1199 }
1200 cpr_vreg->cpr_fuse_addr = res->start;
1201
1202 rc = cpr_init_cpr_efuse(cpr_vreg);
1203 if (rc)
1204 return rc;
1205
1206 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rbcpr");
1207 if (!res || !res->start) {
1208 pr_err("missing rbcpr address: res=%p\n", res);
1209 return -EINVAL;
1210 }
1211 cpr_vreg->rbcpr_base = devm_ioremap(&pdev->dev, res->start,
1212 resource_size(res));
1213
1214 /* Init all voltage set points of APC regulator for CPR */
1215 cpr_init_cpr_voltages(cpr_vreg);
1216
1217 /* Init CPR configuration parameters */
1218 rc = cpr_init_cpr_parameters(pdev, cpr_vreg);
1219 if (rc)
1220 return rc;
1221
1222 /* Get and Init interrupt */
1223 cpr_vreg->cpr_irq = platform_get_irq(pdev, 0);
1224 if (!cpr_vreg->cpr_irq) {
1225 pr_err("missing CPR IRQ\n");
1226 return -EINVAL;
1227 }
1228
1229 /* Configure CPR HW but keep it disabled */
1230 cpr_config(cpr_vreg);
1231
1232 rc = request_threaded_irq(cpr_vreg->cpr_irq, NULL, cpr_irq_handler,
1233 IRQF_TRIGGER_RISING, "cpr", cpr_vreg);
1234 if (rc) {
1235 pr_err("CPR: request irq failed for IRQ %d\n",
1236 cpr_vreg->cpr_irq);
1237 return rc;
1238 }
1239
1240 return 0;
1241}
1242
1243static int __init cpr_pvs_parse_dt(struct platform_device *pdev,
1244 struct cpr_regulator *cpr_vreg)
Willie Ruan2635a852013-03-17 16:33:42 -07001245{
1246 struct device_node *of_node = pdev->dev.of_node;
1247 struct resource *res;
1248 int rc;
1249 size_t pvs_bins;
1250
1251 /* Parse process voltage parameters */
Willie Ruanb17b7832013-04-08 21:30:27 -07001252 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pvs_efuse");
Willie Ruan2635a852013-03-17 16:33:42 -07001253 if (!res || !res->start) {
Willie Ruanb17b7832013-04-08 21:30:27 -07001254 pr_err("pvs_efuse missing: res=%p\n", res);
Willie Ruan2635a852013-03-17 16:33:42 -07001255 return -EINVAL;
1256 }
Willie Ruanb17b7832013-04-08 21:30:27 -07001257 cpr_vreg->pvs_efuse = res->start;
Willie Ruan2635a852013-03-17 16:33:42 -07001258
1259 rc = of_property_read_u32(of_node, "qcom,num-efuse-bits",
1260 &cpr_vreg->num_efuse_bits);
1261 if (rc < 0) {
1262 pr_err("num-efuse-bits missing: rc=%d\n", rc);
1263 return rc;
1264 }
1265
1266 if (cpr_vreg->num_efuse_bits == 0 ||
1267 cpr_vreg->num_efuse_bits > CPR_PVS_EFUSE_BITS_MAX) {
1268 pr_err("invalid num-efuse-bits : %d\n",
1269 cpr_vreg->num_efuse_bits);
1270 return -EINVAL;
1271 }
1272
Willie Ruan2635a852013-03-17 16:33:42 -07001273 pvs_bins = 1 << cpr_vreg->num_efuse_bits;
1274 rc = of_property_read_u32_array(of_node, "qcom,pvs-bin-process",
1275 cpr_vreg->pvs_bin_process,
1276 pvs_bins);
1277 if (rc < 0) {
1278 pr_err("pvs-bin-process missing: rc=%d\n", rc);
1279 return rc;
1280 }
1281
1282 rc = of_property_read_u32_array(of_node,
1283 "qcom,pvs-corner-ceiling-slow",
Willie Ruanb17b7832013-04-08 21:30:27 -07001284 &cpr_vreg->pvs_corner_v[APC_PVS_SLOW][CPR_CORNER_SVS],
Willie Ruan2635a852013-03-17 16:33:42 -07001285 CPR_CORNER_MAX - CPR_CORNER_SVS);
1286 if (rc < 0) {
1287 pr_err("pvs-corner-ceiling-slow missing: rc=%d\n", rc);
1288 return rc;
1289 }
1290
1291 rc = of_property_read_u32_array(of_node,
1292 "qcom,pvs-corner-ceiling-nom",
Willie Ruanb17b7832013-04-08 21:30:27 -07001293 &cpr_vreg->pvs_corner_v[APC_PVS_NOM][CPR_CORNER_SVS],
Willie Ruan2635a852013-03-17 16:33:42 -07001294 CPR_CORNER_MAX - CPR_CORNER_SVS);
1295 if (rc < 0) {
1296 pr_err("pvs-corner-ceiling-norm missing: rc=%d\n", rc);
1297 return rc;
1298 }
1299
1300 rc = of_property_read_u32_array(of_node,
1301 "qcom,pvs-corner-ceiling-fast",
Willie Ruanb17b7832013-04-08 21:30:27 -07001302 &cpr_vreg->pvs_corner_v[APC_PVS_FAST][CPR_CORNER_SVS],
Willie Ruan2635a852013-03-17 16:33:42 -07001303 CPR_CORNER_MAX - CPR_CORNER_SVS);
1304 if (rc < 0) {
1305 pr_err("pvs-corner-ceiling-fast missing: rc=%d\n", rc);
1306 return rc;
1307 }
1308
1309 return 0;
1310}
1311
1312static int __devinit cpr_regulator_probe(struct platform_device *pdev)
1313{
1314 struct cpr_regulator *cpr_vreg;
1315 struct regulator_desc *rdesc;
1316 struct regulator_init_data *init_data = pdev->dev.platform_data;
1317 int rc;
1318
1319 if (!pdev->dev.of_node) {
1320 pr_err("Device tree node is missing\n");
1321 return -EINVAL;
1322 }
1323
1324 init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node);
1325 if (!init_data) {
1326 pr_err("regulator init data is missing\n");
1327 return -EINVAL;
1328 } else {
1329 init_data->constraints.input_uV
1330 = init_data->constraints.max_uV;
1331 init_data->constraints.valid_ops_mask
1332 |= REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_STATUS;
1333 }
1334
1335 cpr_vreg = devm_kzalloc(&pdev->dev, sizeof(struct cpr_regulator),
1336 GFP_KERNEL);
1337 if (!cpr_vreg) {
1338 pr_err("Can't allocate cpr_regulator memory\n");
1339 return -ENOMEM;
1340 }
1341
Willie Ruanb17b7832013-04-08 21:30:27 -07001342 rc = cpr_pvs_parse_dt(pdev, cpr_vreg);
Willie Ruan2635a852013-03-17 16:33:42 -07001343 if (rc) {
1344 pr_err("Wrong DT parameter specified: rc=%d\n", rc);
1345 return rc;
1346 }
1347
Willie Ruanb17b7832013-04-08 21:30:27 -07001348 rc = cpr_pvs_init(cpr_vreg);
Willie Ruan2635a852013-03-17 16:33:42 -07001349 if (rc) {
1350 pr_err("Initialize PVS wrong: rc=%d\n", rc);
1351 return rc;
1352 }
1353
Willie Ruanb17b7832013-04-08 21:30:27 -07001354 rc = cpr_apc_init(pdev, cpr_vreg);
Willie Ruan2635a852013-03-17 16:33:42 -07001355 if (rc) {
1356 if (rc != -EPROBE_DEFER)
1357 pr_err("Initialize APC wrong: rc=%d\n", rc);
1358 return rc;
1359 }
1360
Willie Ruanb17b7832013-04-08 21:30:27 -07001361 rc = cpr_init_cpr(pdev, cpr_vreg);
1362 if (rc) {
1363 pr_err("Initialize CPR failed: rc=%d\n", rc);
1364 return rc;
1365 }
1366
1367 mutex_init(&cpr_vreg->cpr_mutex);
1368
Willie Ruan2635a852013-03-17 16:33:42 -07001369 rdesc = &cpr_vreg->rdesc;
1370 rdesc->owner = THIS_MODULE;
1371 rdesc->type = REGULATOR_VOLTAGE;
1372 rdesc->ops = &cpr_corner_ops;
1373 rdesc->name = init_data->constraints.name;
1374
1375 cpr_vreg->rdev = regulator_register(rdesc, &pdev->dev, init_data,
1376 cpr_vreg, pdev->dev.of_node);
1377 if (IS_ERR(cpr_vreg->rdev)) {
1378 rc = PTR_ERR(cpr_vreg->rdev);
1379 pr_err("regulator_register failed: rc=%d\n", rc);
1380
Willie Ruanb17b7832013-04-08 21:30:27 -07001381 cpr_apc_exit(cpr_vreg);
Willie Ruan2635a852013-03-17 16:33:42 -07001382 return rc;
1383 }
1384
1385 platform_set_drvdata(pdev, cpr_vreg);
Willie Ruanb17b7832013-04-08 21:30:27 -07001386 the_cpr = cpr_vreg;
Willie Ruan2635a852013-03-17 16:33:42 -07001387
1388 return 0;
1389}
1390
1391static int __devexit cpr_regulator_remove(struct platform_device *pdev)
1392{
1393 struct cpr_regulator *cpr_vreg;
1394
1395 cpr_vreg = platform_get_drvdata(pdev);
1396 if (cpr_vreg) {
Willie Ruanb17b7832013-04-08 21:30:27 -07001397 /* Disable CPR */
1398 if (cpr_is_allowed(cpr_vreg)) {
1399 cpr_ctl_disable(cpr_vreg);
1400 cpr_irq_set(cpr_vreg, 0);
1401 }
1402
1403 cpr_apc_exit(cpr_vreg);
Willie Ruan2635a852013-03-17 16:33:42 -07001404 regulator_unregister(cpr_vreg->rdev);
1405 }
1406
1407 return 0;
1408}
1409
1410static struct of_device_id cpr_regulator_match_table[] = {
1411 { .compatible = CPR_REGULATOR_DRIVER_NAME, },
1412 {}
1413};
1414
1415static struct platform_driver cpr_regulator_driver = {
1416 .driver = {
1417 .name = CPR_REGULATOR_DRIVER_NAME,
1418 .of_match_table = cpr_regulator_match_table,
1419 .owner = THIS_MODULE,
1420 },
1421 .probe = cpr_regulator_probe,
1422 .remove = __devexit_p(cpr_regulator_remove),
Willie Ruanb17b7832013-04-08 21:30:27 -07001423 .suspend = cpr_regulator_suspend,
1424 .resume = cpr_regulator_resume,
Willie Ruan2635a852013-03-17 16:33:42 -07001425};
1426
1427/**
1428 * cpr_regulator_init() - register cpr-regulator driver
1429 *
1430 * This initialization function should be called in systems in which driver
1431 * registration ordering must be controlled precisely.
1432 */
1433int __init cpr_regulator_init(void)
1434{
1435 static bool initialized;
1436
1437 if (initialized)
1438 return 0;
1439 else
1440 initialized = true;
1441
1442 return platform_driver_register(&cpr_regulator_driver);
1443}
1444EXPORT_SYMBOL(cpr_regulator_init);
1445
1446static void __exit cpr_regulator_exit(void)
1447{
1448 platform_driver_unregister(&cpr_regulator_driver);
1449}
1450
1451MODULE_DESCRIPTION("CPR regulator driver");
1452MODULE_LICENSE("GPL v2");
1453
1454arch_initcall(cpr_regulator_init);
1455module_exit(cpr_regulator_exit);