dea345f49fdfde91877eaa0080ec2f0dfe51e274
[pandora-kernel.git] / arch / arm / mach-omap2 / smartreflex.c
1 /*
2  * OMAP SmartReflex Voltage Control
3  *
4  * Author: Thara Gopinath       <thara@ti.com>
5  *
6  * Copyright (C) 2010 Texas Instruments, Inc.
7  * Thara Gopinath <thara@ti.com>
8  *
9  * Copyright (C) 2008 Nokia Corporation
10  * Kalle Jokiniemi
11  *
12  * Copyright (C) 2007 Texas Instruments, Inc.
13  * Lesly A M <x0080970@ti.com>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License version 2 as
17  * published by the Free Software Foundation.
18  */
19
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/clk.h>
23 #include <linux/io.h>
24 #include <linux/debugfs.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/pm_runtime.h>
28
29 #include <plat/common.h>
30
31 #include "pm.h"
32 #include "smartreflex.h"
33
34 #define SMARTREFLEX_NAME_LEN    16
35 #define NVALUE_NAME_LEN         40
36 #define SR_DISABLE_TIMEOUT      200
37
38 struct omap_sr {
39         int                             srid;
40         int                             ip_type;
41         int                             nvalue_count;
42         bool                            autocomp_active;
43         u32                             clk_length;
44         u32                             err_weight;
45         u32                             err_minlimit;
46         u32                             err_maxlimit;
47         u32                             accum_data;
48         u32                             senn_avgweight;
49         u32                             senp_avgweight;
50         u32                             senp_mod;
51         u32                             senn_mod;
52         unsigned int                    irq;
53         void __iomem                    *base;
54         struct platform_device          *pdev;
55         struct list_head                node;
56         struct omap_sr_nvalue_table     *nvalue_table;
57         struct voltagedomain            *voltdm;
58         struct dentry                   *dbg_dir;
59 };
60
61 /* sr_list contains all the instances of smartreflex module */
62 static LIST_HEAD(sr_list);
63
64 static struct omap_sr_class_data *sr_class;
65 static struct omap_sr_pmic_data *sr_pmic_data;
66 static struct dentry            *sr_dbg_dir;
67
68 static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
69 {
70         __raw_writel(value, (sr->base + offset));
71 }
72
73 static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
74                                         u32 value)
75 {
76         u32 reg_val;
77
78         /*
79          * Smartreflex error config register is special as it contains
80          * certain status bits which if written a 1 into means a clear
81          * of those bits. So in order to make sure no accidental write of
82          * 1 happens to those status bits, do a clear of them in the read
83          * value. This mean this API doesn't rewrite values in these bits
84          * if they are currently set, but does allow the caller to write
85          * those bits.
86          */
87         if (sr->ip_type == SR_TYPE_V1 && offset == ERRCONFIG_V1)
88                 mask |= ERRCONFIG_STATUS_V1_MASK;
89         else if (sr->ip_type == SR_TYPE_V2 && offset == ERRCONFIG_V2)
90                 mask |= ERRCONFIG_VPBOUNDINTST_V2;
91
92         reg_val = __raw_readl(sr->base + offset);
93         reg_val &= ~mask;
94
95         value &= mask;
96
97         reg_val |= value;
98
99         __raw_writel(reg_val, (sr->base + offset));
100 }
101
102 static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
103 {
104         return __raw_readl(sr->base + offset);
105 }
106
107 static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
108 {
109         struct omap_sr *sr_info;
110
111         if (!voltdm) {
112                 pr_err("%s: Null voltage domain passed!\n", __func__);
113                 return ERR_PTR(-EINVAL);
114         }
115
116         list_for_each_entry(sr_info, &sr_list, node) {
117                 if (voltdm == sr_info->voltdm)
118                         return sr_info;
119         }
120
121         return ERR_PTR(-ENODATA);
122 }
123
124 static irqreturn_t sr_interrupt(int irq, void *data)
125 {
126         struct omap_sr *sr_info = (struct omap_sr *)data;
127         u32 status = 0;
128
129         if (sr_info->ip_type == SR_TYPE_V1) {
130                 /* Read the status bits */
131                 status = sr_read_reg(sr_info, ERRCONFIG_V1);
132
133                 /* Clear them by writing back */
134                 sr_write_reg(sr_info, ERRCONFIG_V1, status);
135         } else if (sr_info->ip_type == SR_TYPE_V2) {
136                 /* Read the status bits */
137                 status = sr_read_reg(sr_info, IRQSTATUS);
138
139                 /* Clear them by writing back */
140                 sr_write_reg(sr_info, IRQSTATUS, status);
141         }
142
143         if (sr_class->notify)
144                 sr_class->notify(sr_info->voltdm, status);
145
146         return IRQ_HANDLED;
147 }
148
149 static void sr_set_clk_length(struct omap_sr *sr)
150 {
151         struct clk *sys_ck;
152         u32 sys_clk_speed;
153
154         if (cpu_is_omap34xx())
155                 sys_ck = clk_get(NULL, "sys_ck");
156         else
157                 sys_ck = clk_get(NULL, "sys_clkin_ck");
158
159         if (IS_ERR(sys_ck)) {
160                 dev_err(&sr->pdev->dev, "%s: unable to get sys clk\n",
161                         __func__);
162                 return;
163         }
164         sys_clk_speed = clk_get_rate(sys_ck);
165         clk_put(sys_ck);
166
167         switch (sys_clk_speed) {
168         case 12000000:
169                 sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
170                 break;
171         case 13000000:
172                 sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
173                 break;
174         case 19200000:
175                 sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
176                 break;
177         case 26000000:
178                 sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
179                 break;
180         case 38400000:
181                 sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
182                 break;
183         default:
184                 dev_err(&sr->pdev->dev, "%s: Invalid sysclk value: %d\n",
185                         __func__, sys_clk_speed);
186                 break;
187         }
188 }
189
190 static void sr_set_regfields(struct omap_sr *sr)
191 {
192         /*
193          * For time being these values are defined in smartreflex.h
194          * and populated during init. May be they can be moved to board
195          * file or pmic specific data structure. In that case these structure
196          * fields will have to be populated using the pdata or pmic structure.
197          */
198         if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
199                 sr->err_weight = OMAP3430_SR_ERRWEIGHT;
200                 sr->err_maxlimit = OMAP3430_SR_ERRMAXLIMIT;
201                 sr->accum_data = OMAP3430_SR_ACCUMDATA;
202                 if (!(strcmp(sr->voltdm->name, "mpu"))) {
203                         sr->senn_avgweight = OMAP3430_SR1_SENNAVGWEIGHT;
204                         sr->senp_avgweight = OMAP3430_SR1_SENPAVGWEIGHT;
205                 } else {
206                         sr->senn_avgweight = OMAP3430_SR2_SENNAVGWEIGHT;
207                         sr->senp_avgweight = OMAP3430_SR2_SENPAVGWEIGHT;
208                 }
209         }
210 }
211
212 static void sr_start_vddautocomp(struct omap_sr *sr)
213 {
214         if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
215                 dev_warn(&sr->pdev->dev,
216                         "%s: smartreflex class driver not registered\n",
217                         __func__);
218                 return;
219         }
220
221         if (!sr_class->enable(sr->voltdm))
222                 sr->autocomp_active = true;
223 }
224
225 static void sr_stop_vddautocomp(struct omap_sr *sr)
226 {
227         if (!sr_class || !(sr_class->disable)) {
228                 dev_warn(&sr->pdev->dev,
229                         "%s: smartreflex class driver not registered\n",
230                         __func__);
231                 return;
232         }
233
234         if (sr->autocomp_active) {
235                 sr_class->disable(sr->voltdm, 1);
236                 sr->autocomp_active = false;
237         }
238 }
239
240 /*
241  * This function handles the intializations which have to be done
242  * only when both sr device and class driver regiter has
243  * completed. This will be attempted to be called from both sr class
244  * driver register and sr device intializtion API's. Only one call
245  * will ultimately succeed.
246  *
247  * Currently this function registers interrupt handler for a particular SR
248  * if smartreflex class driver is already registered and has
249  * requested for interrupts and the SR interrupt line in present.
250  */
251 static int sr_late_init(struct omap_sr *sr_info)
252 {
253         char *name;
254         struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
255         struct resource *mem;
256         int ret = 0;
257
258         if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
259                 name = kasprintf(GFP_KERNEL, "sr_%s", sr_info->voltdm->name);
260                 if (name == NULL) {
261                         ret = -ENOMEM;
262                         goto error;
263                 }
264                 ret = request_irq(sr_info->irq, sr_interrupt,
265                                 0, name, (void *)sr_info);
266                 if (ret)
267                         goto error;
268                 disable_irq(sr_info->irq);
269         }
270
271         if (pdata && pdata->enable_on_init)
272                 sr_start_vddautocomp(sr_info);
273
274         return ret;
275
276 error:
277         iounmap(sr_info->base);
278         mem = platform_get_resource(sr_info->pdev, IORESOURCE_MEM, 0);
279         release_mem_region(mem->start, resource_size(mem));
280         list_del(&sr_info->node);
281         dev_err(&sr_info->pdev->dev, "%s: ERROR in registering"
282                 "interrupt handler. Smartreflex will"
283                 "not function as desired\n", __func__);
284         kfree(name);
285         kfree(sr_info);
286         return ret;
287 }
288
289 static void sr_v1_disable(struct omap_sr *sr)
290 {
291         int timeout = 0;
292         int errconf_val = ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
293                         ERRCONFIG_MCUBOUNDINTST;
294
295         /* Enable MCUDisableAcknowledge interrupt */
296         sr_modify_reg(sr, ERRCONFIG_V1,
297                         ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
298
299         /* SRCONFIG - disable SR */
300         sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
301
302         /* Disable all other SR interrupts and clear the status as needed */
303         if (sr_read_reg(sr, ERRCONFIG_V1) & ERRCONFIG_VPBOUNDINTST_V1)
304                 errconf_val |= ERRCONFIG_VPBOUNDINTST_V1;
305         sr_modify_reg(sr, ERRCONFIG_V1,
306                         (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
307                         ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
308                         errconf_val);
309
310         /*
311          * Wait for SR to be disabled.
312          * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
313          */
314         omap_test_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
315                         ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
316                         timeout);
317
318         if (timeout >= SR_DISABLE_TIMEOUT)
319                 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
320                         __func__);
321
322         /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
323         sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
324                         ERRCONFIG_MCUDISACKINTST);
325 }
326
327 static void sr_v2_disable(struct omap_sr *sr)
328 {
329         int timeout = 0;
330
331         /* Enable MCUDisableAcknowledge interrupt */
332         sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
333
334         /* SRCONFIG - disable SR */
335         sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
336
337         /*
338          * Disable all other SR interrupts and clear the status
339          * write to status register ONLY on need basis - only if status
340          * is set.
341          */
342         if (sr_read_reg(sr, ERRCONFIG_V2) & ERRCONFIG_VPBOUNDINTST_V2)
343                 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
344                         ERRCONFIG_VPBOUNDINTST_V2);
345         else
346                 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
347                                 0x0);
348         sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
349                         IRQENABLE_MCUVALIDINT |
350                         IRQENABLE_MCUBOUNDSINT));
351         sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
352                         IRQSTATUS_MCVALIDINT |
353                         IRQSTATUS_MCBOUNDSINT));
354
355         /*
356          * Wait for SR to be disabled.
357          * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
358          */
359         omap_test_timeout((sr_read_reg(sr, IRQSTATUS) &
360                         IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
361                         timeout);
362
363         if (timeout >= SR_DISABLE_TIMEOUT)
364                 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
365                         __func__);
366
367         /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
368         sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
369         sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
370 }
371
372 static u32 sr_retrieve_nvalue(struct omap_sr *sr, u32 efuse_offs)
373 {
374         int i;
375
376         if (!sr->nvalue_table) {
377                 dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
378                         __func__);
379                 return 0;
380         }
381
382         for (i = 0; i < sr->nvalue_count; i++) {
383                 if (sr->nvalue_table[i].efuse_offs == efuse_offs)
384                         return sr->nvalue_table[i].nvalue;
385         }
386
387         return 0;
388 }
389
390 /* Public Functions */
391
392 /**
393  * sr_configure_errgen() - Configures the smrtreflex to perform AVS using the
394  *                       error generator module.
395  * @voltdm:     VDD pointer to which the SR module to be configured belongs to.
396  *
397  * This API is to be called from the smartreflex class driver to
398  * configure the error generator module inside the smartreflex module.
399  * SR settings if using the ERROR module inside Smartreflex.
400  * SR CLASS 3 by default uses only the ERROR module where as
401  * SR CLASS 2 can choose between ERROR module and MINMAXAVG
402  * module. Returns 0 on success and error value in case of failure.
403  */
404 int sr_configure_errgen(struct voltagedomain *voltdm)
405 {
406         u32 sr_config, sr_errconfig, errconfig_offs, vpboundint_en;
407         u32 vpboundint_st, senp_en = 0, senn_en = 0;
408         u8 senp_shift, senn_shift;
409         struct omap_sr *sr = _sr_lookup(voltdm);
410
411         if (IS_ERR(sr)) {
412                 pr_warning("%s: omap_sr struct for sr_%s not found\n",
413                         __func__, voltdm->name);
414                 return -EINVAL;
415         }
416
417         if (!sr->clk_length)
418                 sr_set_clk_length(sr);
419
420         senp_en = sr->senp_mod;
421         senn_en = sr->senn_mod;
422
423         sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
424                 SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
425
426         if (sr->ip_type == SR_TYPE_V1) {
427                 sr_config |= SRCONFIG_DELAYCTRL;
428                 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
429                 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
430                 errconfig_offs = ERRCONFIG_V1;
431                 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
432                 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
433         } else if (sr->ip_type == SR_TYPE_V2) {
434                 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
435                 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
436                 errconfig_offs = ERRCONFIG_V2;
437                 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
438                 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
439         } else {
440                 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
441                         "module without specifying the ip\n", __func__);
442                 return -EINVAL;
443         }
444
445         sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
446         sr_write_reg(sr, SRCONFIG, sr_config);
447         sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
448                 (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
449                 (sr->err_minlimit <<  ERRCONFIG_ERRMINLIMIT_SHIFT);
450         sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
451                 SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
452                 sr_errconfig);
453
454         /* Enabling the interrupts if the ERROR module is used */
455         sr_modify_reg(sr, errconfig_offs, (vpboundint_en | vpboundint_st),
456                       vpboundint_en);
457
458         return 0;
459 }
460
461 /**
462  * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
463  * @voltdm:     VDD pointer to which the SR module to be configured belongs to.
464  *
465  * This API is to be called from the smartreflex class driver to
466  * disable the error generator module inside the smartreflex module.
467  *
468  * Returns 0 on success and error value in case of failure.
469  */
470 int sr_disable_errgen(struct voltagedomain *voltdm)
471 {
472         u32 errconfig_offs, vpboundint_en;
473         u32 vpboundint_st;
474         struct omap_sr *sr = _sr_lookup(voltdm);
475
476         if (IS_ERR(sr)) {
477                 pr_warning("%s: omap_sr struct for sr_%s not found\n",
478                         __func__, voltdm->name);
479                 return -EINVAL;
480         }
481
482         if (sr->ip_type == SR_TYPE_V1) {
483                 errconfig_offs = ERRCONFIG_V1;
484                 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
485                 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
486         } else if (sr->ip_type == SR_TYPE_V2) {
487                 errconfig_offs = ERRCONFIG_V2;
488                 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
489                 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
490         } else {
491                 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
492                         "module without specifying the ip\n", __func__);
493                 return -EINVAL;
494         }
495
496         /* Disable the interrupts of ERROR module */
497         sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
498
499         /* Disable the Sensor and errorgen */
500         sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
501
502         return 0;
503 }
504
505 /**
506  * sr_configure_minmax() - Configures the smrtreflex to perform AVS using the
507  *                       minmaxavg module.
508  * @voltdm:     VDD pointer to which the SR module to be configured belongs to.
509  *
510  * This API is to be called from the smartreflex class driver to
511  * configure the minmaxavg module inside the smartreflex module.
512  * SR settings if using the ERROR module inside Smartreflex.
513  * SR CLASS 3 by default uses only the ERROR module where as
514  * SR CLASS 2 can choose between ERROR module and MINMAXAVG
515  * module. Returns 0 on success and error value in case of failure.
516  */
517 int sr_configure_minmax(struct voltagedomain *voltdm)
518 {
519         u32 sr_config, sr_avgwt;
520         u32 senp_en = 0, senn_en = 0;
521         u8 senp_shift, senn_shift;
522         struct omap_sr *sr = _sr_lookup(voltdm);
523
524         if (IS_ERR(sr)) {
525                 pr_warning("%s: omap_sr struct for sr_%s not found\n",
526                         __func__, voltdm->name);
527                 return -EINVAL;
528         }
529
530         if (!sr->clk_length)
531                 sr_set_clk_length(sr);
532
533         senp_en = sr->senp_mod;
534         senn_en = sr->senn_mod;
535
536         sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
537                 SRCONFIG_SENENABLE |
538                 (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
539
540         if (sr->ip_type == SR_TYPE_V1) {
541                 sr_config |= SRCONFIG_DELAYCTRL;
542                 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
543                 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
544         } else if (sr->ip_type == SR_TYPE_V2) {
545                 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
546                 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
547         } else {
548                 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex"
549                         "module without specifying the ip\n", __func__);
550                 return -EINVAL;
551         }
552
553         sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
554         sr_write_reg(sr, SRCONFIG, sr_config);
555         sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
556                 (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
557         sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
558
559         /*
560          * Enabling the interrupts if MINMAXAVG module is used.
561          * TODO: check if all the interrupts are mandatory
562          */
563         if (sr->ip_type == SR_TYPE_V1) {
564                 sr_modify_reg(sr, ERRCONFIG_V1,
565                         (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
566                         ERRCONFIG_MCUBOUNDINTEN),
567                         (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
568                          ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
569                          ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
570         } else if (sr->ip_type == SR_TYPE_V2) {
571                 sr_write_reg(sr, IRQSTATUS,
572                         IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
573                         IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
574                 sr_write_reg(sr, IRQENABLE_SET,
575                         IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
576                         IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
577         }
578
579         return 0;
580 }
581
582 /**
583  * sr_enable() - Enables the smartreflex module.
584  * @voltdm:     VDD pointer to which the SR module to be configured belongs to.
585  * @volt:       The voltage at which the Voltage domain associated with
586  *              the smartreflex module is operating at.
587  *              This is required only to program the correct Ntarget value.
588  *
589  * This API is to be called from the smartreflex class driver to
590  * enable a smartreflex module. Returns 0 on success. Returns error
591  * value if the voltage passed is wrong or if ntarget value is wrong.
592  */
593 int sr_enable(struct voltagedomain *voltdm, unsigned long volt)
594 {
595         u32 nvalue_reciprocal;
596         struct omap_volt_data *volt_data;
597         struct omap_sr *sr = _sr_lookup(voltdm);
598         int ret;
599
600         if (IS_ERR(sr)) {
601                 pr_warning("%s: omap_sr struct for sr_%s not found\n",
602                         __func__, voltdm->name);
603                 return -EINVAL;
604         }
605
606         volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
607
608         if (IS_ERR(volt_data)) {
609                 dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table"
610                         "for nominal voltage %ld\n", __func__, volt);
611                 return -ENODATA;
612         }
613
614         nvalue_reciprocal = sr_retrieve_nvalue(sr, volt_data->sr_efuse_offs);
615
616         if (!nvalue_reciprocal) {
617                 dev_warn(&sr->pdev->dev, "%s: NVALUE = 0 at voltage %ld\n",
618                         __func__, volt);
619                 return -ENODATA;
620         }
621
622         /* errminlimit is opp dependent and hence linked to voltage */
623         sr->err_minlimit = volt_data->sr_errminlimit;
624
625         pm_runtime_get_sync(&sr->pdev->dev);
626
627         /* Check if SR is already enabled. If yes do nothing */
628         if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
629                 return 0;
630
631         /* Configure SR */
632         ret = sr_class->configure(voltdm);
633         if (ret)
634                 return ret;
635
636         sr_write_reg(sr, NVALUERECIPROCAL, nvalue_reciprocal);
637
638         /* SRCONFIG - enable SR */
639         sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
640         return 0;
641 }
642
643 /**
644  * sr_disable() - Disables the smartreflex module.
645  * @voltdm:     VDD pointer to which the SR module to be configured belongs to.
646  *
647  * This API is to be called from the smartreflex class driver to
648  * disable a smartreflex module.
649  */
650 void sr_disable(struct voltagedomain *voltdm)
651 {
652         struct omap_sr *sr = _sr_lookup(voltdm);
653
654         if (IS_ERR(sr)) {
655                 pr_warning("%s: omap_sr struct for sr_%s not found\n",
656                         __func__, voltdm->name);
657                 return;
658         }
659
660         /* Check if SR clocks are already disabled. If yes do nothing */
661         if (pm_runtime_suspended(&sr->pdev->dev))
662                 return;
663
664         /*
665          * Disable SR if only it is indeed enabled. Else just
666          * disable the clocks.
667          */
668         if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
669                 if (sr->ip_type == SR_TYPE_V1)
670                         sr_v1_disable(sr);
671                 else if (sr->ip_type == SR_TYPE_V2)
672                         sr_v2_disable(sr);
673         }
674
675         pm_runtime_put_sync_suspend(&sr->pdev->dev);
676 }
677
678 /**
679  * sr_register_class() - API to register a smartreflex class parameters.
680  * @class_data: The structure containing various sr class specific data.
681  *
682  * This API is to be called by the smartreflex class driver to register itself
683  * with the smartreflex driver during init. Returns 0 on success else the
684  * error value.
685  */
686 int sr_register_class(struct omap_sr_class_data *class_data)
687 {
688         struct omap_sr *sr_info;
689
690         if (!class_data) {
691                 pr_warning("%s:, Smartreflex class data passed is NULL\n",
692                         __func__);
693                 return -EINVAL;
694         }
695
696         if (sr_class) {
697                 pr_warning("%s: Smartreflex class driver already registered\n",
698                         __func__);
699                 return -EBUSY;
700         }
701
702         sr_class = class_data;
703
704         /*
705          * Call into late init to do intializations that require
706          * both sr driver and sr class driver to be initiallized.
707          */
708         list_for_each_entry(sr_info, &sr_list, node)
709                 sr_late_init(sr_info);
710
711         return 0;
712 }
713
714 /**
715  * omap_sr_enable() -  API to enable SR clocks and to call into the
716  *                      registered smartreflex class enable API.
717  * @voltdm:     VDD pointer to which the SR module to be configured belongs to.
718  *
719  * This API is to be called from the kernel in order to enable
720  * a particular smartreflex module. This API will do the initial
721  * configurations to turn on the smartreflex module and in turn call
722  * into the registered smartreflex class enable API.
723  */
724 void omap_sr_enable(struct voltagedomain *voltdm)
725 {
726         struct omap_sr *sr = _sr_lookup(voltdm);
727
728         if (IS_ERR(sr)) {
729                 pr_warning("%s: omap_sr struct for sr_%s not found\n",
730                         __func__, voltdm->name);
731                 return;
732         }
733
734         if (!sr->autocomp_active)
735                 return;
736
737         if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
738                 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
739                         "registered\n", __func__);
740                 return;
741         }
742
743         sr_class->enable(voltdm);
744 }
745
746 /**
747  * omap_sr_disable() - API to disable SR without resetting the voltage
748  *                      processor voltage
749  * @voltdm:     VDD pointer to which the SR module to be configured belongs to.
750  *
751  * This API is to be called from the kernel in order to disable
752  * a particular smartreflex module. This API will in turn call
753  * into the registered smartreflex class disable API. This API will tell
754  * the smartreflex class disable not to reset the VP voltage after
755  * disabling smartreflex.
756  */
757 void omap_sr_disable(struct voltagedomain *voltdm)
758 {
759         struct omap_sr *sr = _sr_lookup(voltdm);
760
761         if (IS_ERR(sr)) {
762                 pr_warning("%s: omap_sr struct for sr_%s not found\n",
763                         __func__, voltdm->name);
764                 return;
765         }
766
767         if (!sr->autocomp_active)
768                 return;
769
770         if (!sr_class || !(sr_class->disable)) {
771                 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
772                         "registered\n", __func__);
773                 return;
774         }
775
776         sr_class->disable(voltdm, 0);
777 }
778
779 /**
780  * omap_sr_disable_reset_volt() - API to disable SR and reset the
781  *                              voltage processor voltage
782  * @voltdm:     VDD pointer to which the SR module to be configured belongs to.
783  *
784  * This API is to be called from the kernel in order to disable
785  * a particular smartreflex module. This API will in turn call
786  * into the registered smartreflex class disable API. This API will tell
787  * the smartreflex class disable to reset the VP voltage after
788  * disabling smartreflex.
789  */
790 void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
791 {
792         struct omap_sr *sr = _sr_lookup(voltdm);
793
794         if (IS_ERR(sr)) {
795                 pr_warning("%s: omap_sr struct for sr_%s not found\n",
796                         __func__, voltdm->name);
797                 return;
798         }
799
800         if (!sr->autocomp_active)
801                 return;
802
803         if (!sr_class || !(sr_class->disable)) {
804                 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not"
805                         "registered\n", __func__);
806                 return;
807         }
808
809         sr_class->disable(voltdm, 1);
810 }
811
812 /**
813  * omap_sr_register_pmic() - API to register pmic specific info.
814  * @pmic_data:  The structure containing pmic specific data.
815  *
816  * This API is to be called from the PMIC specific code to register with
817  * smartreflex driver pmic specific info. Currently the only info required
818  * is the smartreflex init on the PMIC side.
819  */
820 void omap_sr_register_pmic(struct omap_sr_pmic_data *pmic_data)
821 {
822         if (!pmic_data) {
823                 pr_warning("%s: Trying to register NULL PMIC data structure"
824                         "with smartreflex\n", __func__);
825                 return;
826         }
827
828         sr_pmic_data = pmic_data;
829 }
830
831 /* PM Debug Fs enteries to enable disable smartreflex. */
832 static int omap_sr_autocomp_show(void *data, u64 *val)
833 {
834         struct omap_sr *sr_info = (struct omap_sr *) data;
835
836         if (!sr_info) {
837                 pr_warning("%s: omap_sr struct not found\n", __func__);
838                 return -EINVAL;
839         }
840
841         *val = sr_info->autocomp_active;
842
843         return 0;
844 }
845
846 static int omap_sr_autocomp_store(void *data, u64 val)
847 {
848         struct omap_sr *sr_info = (struct omap_sr *) data;
849
850         if (!sr_info) {
851                 pr_warning("%s: omap_sr struct not found\n", __func__);
852                 return -EINVAL;
853         }
854
855         /* Sanity check */
856         if (val && (val != 1)) {
857                 pr_warning("%s: Invalid argument %lld\n", __func__, val);
858                 return -EINVAL;
859         }
860
861         /* control enable/disable only if there is a delta in value */
862         if (sr_info->autocomp_active != val) {
863                 if (!val)
864                         sr_stop_vddautocomp(sr_info);
865                 else
866                         sr_start_vddautocomp(sr_info);
867         }
868
869         return 0;
870 }
871
872 DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
873                 omap_sr_autocomp_store, "%llu\n");
874
875 static int __init omap_sr_probe(struct platform_device *pdev)
876 {
877         struct omap_sr *sr_info = kzalloc(sizeof(struct omap_sr), GFP_KERNEL);
878         struct omap_sr_data *pdata = pdev->dev.platform_data;
879         struct resource *mem, *irq;
880         struct dentry *nvalue_dir;
881         struct omap_volt_data *volt_data;
882         int i, ret = 0;
883         char *name;
884
885         if (!sr_info) {
886                 dev_err(&pdev->dev, "%s: unable to allocate sr_info\n",
887                         __func__);
888                 return -ENOMEM;
889         }
890
891         platform_set_drvdata(pdev, sr_info);
892
893         if (!pdata) {
894                 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
895                 ret = -EINVAL;
896                 goto err_free_devinfo;
897         }
898
899         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
900         if (!mem) {
901                 dev_err(&pdev->dev, "%s: no mem resource\n", __func__);
902                 ret = -ENODEV;
903                 goto err_free_devinfo;
904         }
905
906         mem = request_mem_region(mem->start, resource_size(mem),
907                                         dev_name(&pdev->dev));
908         if (!mem) {
909                 dev_err(&pdev->dev, "%s: no mem region\n", __func__);
910                 ret = -EBUSY;
911                 goto err_free_devinfo;
912         }
913
914         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
915
916         pm_runtime_enable(&pdev->dev);
917         pm_runtime_irq_safe(&pdev->dev);
918
919         sr_info->pdev = pdev;
920         sr_info->srid = pdev->id;
921         sr_info->voltdm = pdata->voltdm;
922         sr_info->nvalue_table = pdata->nvalue_table;
923         sr_info->nvalue_count = pdata->nvalue_count;
924         sr_info->senn_mod = pdata->senn_mod;
925         sr_info->senp_mod = pdata->senp_mod;
926         sr_info->autocomp_active = false;
927         sr_info->ip_type = pdata->ip_type;
928         sr_info->base = ioremap(mem->start, resource_size(mem));
929         if (!sr_info->base) {
930                 dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
931                 ret = -ENOMEM;
932                 goto err_release_region;
933         }
934
935         if (irq)
936                 sr_info->irq = irq->start;
937
938         sr_set_clk_length(sr_info);
939         sr_set_regfields(sr_info);
940
941         list_add(&sr_info->node, &sr_list);
942
943         /*
944          * Call into late init to do intializations that require
945          * both sr driver and sr class driver to be initiallized.
946          */
947         if (sr_class) {
948                 ret = sr_late_init(sr_info);
949                 if (ret) {
950                         pr_warning("%s: Error in SR late init\n", __func__);
951                         goto err_iounmap;
952                 }
953         }
954
955         dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
956         if (!sr_dbg_dir) {
957                 sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
958                 if (!sr_dbg_dir) {
959                         ret = PTR_ERR(sr_dbg_dir);
960                         pr_err("%s:sr debugfs dir creation failed(%d)\n",
961                                 __func__, ret);
962                         goto err_iounmap;
963                 }
964         }
965
966         name = kasprintf(GFP_KERNEL, "sr_%s", sr_info->voltdm->name);
967         if (!name) {
968                 dev_err(&pdev->dev, "%s: Unable to alloc debugfs name\n",
969                         __func__);
970                 ret = -ENOMEM;
971                 goto err_iounmap;
972         }
973         sr_info->dbg_dir = debugfs_create_dir(name, sr_dbg_dir);
974         kfree(name);
975         if (IS_ERR(sr_info->dbg_dir)) {
976                 dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
977                         __func__);
978                 ret = PTR_ERR(sr_info->dbg_dir);
979                 goto err_iounmap;
980         }
981
982         (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR,
983                         sr_info->dbg_dir, (void *)sr_info, &pm_sr_fops);
984         (void) debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
985                         &sr_info->err_weight);
986         (void) debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
987                         &sr_info->err_maxlimit);
988         (void) debugfs_create_x32("errminlimit", S_IRUGO, sr_info->dbg_dir,
989                         &sr_info->err_minlimit);
990
991         nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
992         if (IS_ERR(nvalue_dir)) {
993                 dev_err(&pdev->dev, "%s: Unable to create debugfs directory"
994                         "for n-values\n", __func__);
995                 ret = PTR_ERR(nvalue_dir);
996                 goto err_debugfs;
997         }
998
999         omap_voltage_get_volttable(sr_info->voltdm, &volt_data);
1000         if (!volt_data) {
1001                 dev_warn(&pdev->dev, "%s: No Voltage table for the"
1002                         " corresponding vdd vdd_%s. Cannot create debugfs"
1003                         "entries for n-values\n",
1004                         __func__, sr_info->voltdm->name);
1005                 ret = -ENODATA;
1006                 goto err_debugfs;
1007         }
1008
1009         for (i = 0; i < sr_info->nvalue_count; i++) {
1010                 char name[NVALUE_NAME_LEN + 1];
1011
1012                 snprintf(name, sizeof(name), "volt_%d",
1013                          volt_data[i].volt_nominal);
1014                 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
1015                                 &(sr_info->nvalue_table[i].nvalue));
1016         }
1017
1018         return ret;
1019
1020 err_debugfs:
1021         debugfs_remove_recursive(sr_info->dbg_dir);
1022 err_iounmap:
1023         list_del(&sr_info->node);
1024         iounmap(sr_info->base);
1025 err_release_region:
1026         release_mem_region(mem->start, resource_size(mem));
1027 err_free_devinfo:
1028         kfree(sr_info);
1029
1030         return ret;
1031 }
1032
1033 static int __devexit omap_sr_remove(struct platform_device *pdev)
1034 {
1035         struct omap_sr_data *pdata = pdev->dev.platform_data;
1036         struct omap_sr *sr_info;
1037         struct resource *mem;
1038
1039         if (!pdata) {
1040                 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1041                 return -EINVAL;
1042         }
1043
1044         sr_info = _sr_lookup(pdata->voltdm);
1045         if (IS_ERR(sr_info)) {
1046                 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1047                         __func__);
1048                 return -EINVAL;
1049         }
1050
1051         if (sr_info->autocomp_active)
1052                 sr_stop_vddautocomp(sr_info);
1053         if (sr_info->dbg_dir)
1054                 debugfs_remove_recursive(sr_info->dbg_dir);
1055
1056         list_del(&sr_info->node);
1057         iounmap(sr_info->base);
1058         kfree(sr_info);
1059         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1060         release_mem_region(mem->start, resource_size(mem));
1061
1062         return 0;
1063 }
1064
1065 static void __devexit omap_sr_shutdown(struct platform_device *pdev)
1066 {
1067         struct omap_sr_data *pdata = pdev->dev.platform_data;
1068         struct omap_sr *sr_info;
1069
1070         if (!pdata) {
1071                 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1072                 return;
1073         }
1074
1075         sr_info = _sr_lookup(pdata->voltdm);
1076         if (IS_ERR(sr_info)) {
1077                 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1078                         __func__);
1079                 return;
1080         }
1081
1082         if (sr_info->autocomp_active)
1083                 sr_stop_vddautocomp(sr_info);
1084
1085         return;
1086 }
1087
1088 static struct platform_driver smartreflex_driver = {
1089         .remove         = __devexit_p(omap_sr_remove),
1090         .shutdown       = __devexit_p(omap_sr_shutdown),
1091         .driver         = {
1092                 .name   = "smartreflex",
1093         },
1094 };
1095
1096 static int __init sr_init(void)
1097 {
1098         int ret = 0;
1099
1100         /*
1101          * sr_init is a late init. If by then a pmic specific API is not
1102          * registered either there is no need for anything to be done on
1103          * the PMIC side or somebody has forgotten to register a PMIC
1104          * handler. Warn for the second condition.
1105          */
1106         if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
1107                 sr_pmic_data->sr_pmic_init();
1108         else
1109                 pr_warning("%s: No PMIC hook to init smartreflex\n", __func__);
1110
1111         ret = platform_driver_probe(&smartreflex_driver, omap_sr_probe);
1112         if (ret) {
1113                 pr_err("%s: platform driver register failed for SR\n",
1114                         __func__);
1115                 return ret;
1116         }
1117
1118         return 0;
1119 }
1120
1121 static void __exit sr_exit(void)
1122 {
1123         platform_driver_unregister(&smartreflex_driver);
1124 }
1125 late_initcall(sr_init);
1126 module_exit(sr_exit);
1127
1128 MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1129 MODULE_LICENSE("GPL");
1130 MODULE_ALIAS("platform:" DRIVER_NAME);
1131 MODULE_AUTHOR("Texas Instruments Inc");