ARM: tegra: clock: Set EMC and SCLK rates suspend floors
[linux-3.10.git] / arch / arm / mach-tegra / tegra3_actmon.c
1 /*
2  * Copyright (c) 2011, NVIDIA Corporation.
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 as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/spinlock.h>
20 #include <linux/err.h>
21 #include <linux/io.h>
22 #include <linux/clk.h>
23 #include <linux/clk/tegra.h>
24 #include <linux/interrupt.h>
25 #include <linux/suspend.h>
26 #include <linux/debugfs.h>
27 #include <linux/seq_file.h>
28 #include <linux/uaccess.h>
29 #include <linux/module.h>
30
31 #include <mach/irqs.h>
32
33 #include "clock.h"
34 #include "iomap.h"
35
36 #define ACTMON_GLB_STATUS                       0x00
37 #define ACTMON_GLB_PERIOD_CTRL                  0x04
38
39 #define ACTMON_DEV_CTRL                         0x00
40 #define ACTMON_DEV_CTRL_ENB                     (0x1 << 31)
41 #define ACTMON_DEV_CTRL_UP_WMARK_ENB            (0x1 << 30)
42 #define ACTMON_DEV_CTRL_DOWN_WMARK_ENB          (0x1 << 29)
43 #define ACTMON_DEV_CTRL_UP_WMARK_NUM_SHIFT      26
44 #define ACTMON_DEV_CTRL_UP_WMARK_NUM_MASK       (0x7 << 26)
45 #define ACTMON_DEV_CTRL_DOWN_WMARK_NUM_SHIFT    23
46 #define ACTMON_DEV_CTRL_DOWN_WMARK_NUM_MASK     (0x7 << 23)
47 #define ACTMON_DEV_CTRL_AVG_UP_WMARK_ENB        (0x1 << 21)
48 #define ACTMON_DEV_CTRL_AVG_DOWN_WMARK_ENB      (0x1 << 20)
49 #define ACTMON_DEV_CTRL_PERIODIC_ENB            (0x1 << 18)
50 #define ACTMON_DEV_CTRL_K_VAL_SHIFT             10
51 #define ACTMON_DEV_CTRL_K_VAL_MASK              (0x7 << 10)
52
53 #define ACTMON_DEV_UP_WMARK                     0x04
54 #define ACTMON_DEV_DOWN_WMARK                   0x08
55 #define ACTMON_DEV_INIT_AVG                     0x0c
56 #define ACTMON_DEV_AVG_UP_WMARK                 0x10
57 #define ACTMON_DEV_AVG_DOWN_WMARK                       0x14
58
59 #define ACTMON_DEV_COUNT_WEGHT                  0x18
60 #define ACTMON_DEV_COUNT                        0x1c
61 #define ACTMON_DEV_AVG_COUNT                    0x20
62
63 #define ACTMON_DEV_INTR_STATUS                  0x24
64 #define ACTMON_DEV_INTR_UP_WMARK                (0x1 << 31)
65 #define ACTMON_DEV_INTR_DOWN_WMARK              (0x1 << 30)
66 #define ACTMON_DEV_INTR_AVG_DOWN_WMARK          (0x1 << 25)
67 #define ACTMON_DEV_INTR_AVG_UP_WMARK            (0x1 << 24)
68
69 #define ACTMON_DEFAULT_AVG_WINDOW_LOG2          6
70 #define ACTMON_DEFAULT_AVG_BAND                 6       /* 1/10 of % */
71
72 enum actmon_type {
73         ACTMON_LOAD_SAMPLER,
74         ACTMON_FREQ_SAMPLER,
75 };
76
77 enum actmon_state {
78         ACTMON_UNINITIALIZED = -1,
79         ACTMON_OFF = 0,
80         ACTMON_ON  = 1,
81         ACTMON_SUSPENDED = 2,
82 };
83
84 #define ACTMON_DEFAULT_SAMPLING_PERIOD          12
85 static u8 actmon_sampling_period;
86
87 static unsigned long actmon_clk_freq;
88
89 /* Maximum frequency EMC is running at when sourced from PLLP. This is
90  * really a short-cut, but it is true for all Tegra3  platforms
91  */
92 #define EMC_PLLP_FREQ_MAX                       204000
93
94 /* Units:
95  * - frequency in kHz
96  * - coefficients, and thresholds in %
97  * - sampling period in ms
98  * - window in sample periods (value = setting + 1)
99  */
100 struct actmon_dev {
101         u32             reg;
102         u32             glb_status_irq_mask;
103         const char      *dev_id;
104         const char      *con_id;
105         struct clk      *clk;
106
107         unsigned long   max_freq;
108         unsigned long   target_freq;
109         unsigned long   cur_freq;
110         unsigned long   suspend_freq;
111
112         unsigned long   avg_actv_freq;
113         unsigned long   avg_band_freq;
114         unsigned int    avg_sustain_coef;
115         u32             avg_count;
116
117         unsigned long   boost_freq;
118         unsigned long   boost_freq_step;
119         unsigned int    boost_up_coef;
120         unsigned int    boost_down_coef;
121         unsigned int    boost_up_threshold;
122         unsigned int    boost_down_threshold;
123
124         u8              up_wmark_window;
125         u8              down_wmark_window;
126         u8              avg_window_log2;
127         u32             count_weight;
128
129         enum actmon_type        type;
130         enum actmon_state       state;
131         enum actmon_state       saved_state;
132
133         spinlock_t      lock;
134
135         struct notifier_block   rate_change_nb;
136 };
137
138 static void __iomem *actmon_base = IO_ADDRESS(TEGRA_ACTMON_BASE);
139
140 static inline u32 actmon_readl(u32 offset)
141 {
142         return __raw_readl(actmon_base + offset);
143 }
144 static inline void actmon_writel(u32 val, u32 offset)
145 {
146         __raw_writel(val, actmon_base + offset);
147 }
148 static inline void actmon_wmb(void)
149 {
150         wmb();
151         actmon_readl(ACTMON_GLB_STATUS);
152 }
153
154 #define offs(x)         (dev->reg + x)
155
156 static inline unsigned long do_percent(unsigned long val, unsigned int pct)
157 {
158         return val * pct / 100;
159 }
160
161 static inline void actmon_dev_up_wmark_set(struct actmon_dev *dev)
162 {
163         u32 val;
164         unsigned long freq = (dev->type == ACTMON_FREQ_SAMPLER) ?
165                 dev->cur_freq : actmon_clk_freq;
166
167         val = freq * actmon_sampling_period;
168         actmon_writel(do_percent(val, dev->boost_up_threshold),
169                       offs(ACTMON_DEV_UP_WMARK));
170 }
171
172 static inline void actmon_dev_down_wmark_set(struct actmon_dev *dev)
173 {
174         u32 val;
175         unsigned long freq = (dev->type == ACTMON_FREQ_SAMPLER) ?
176                 dev->cur_freq : actmon_clk_freq;
177
178         val = freq * actmon_sampling_period;
179         actmon_writel(do_percent(val, dev->boost_down_threshold),
180                       offs(ACTMON_DEV_DOWN_WMARK));
181 }
182
183 static inline void actmon_dev_wmark_set(struct actmon_dev *dev)
184 {
185         u32 val;
186         unsigned long freq = (dev->type == ACTMON_FREQ_SAMPLER) ?
187                 dev->cur_freq : actmon_clk_freq;
188
189         val = freq * actmon_sampling_period;
190         actmon_writel(do_percent(val, dev->boost_up_threshold),
191                       offs(ACTMON_DEV_UP_WMARK));
192         actmon_writel(do_percent(val, dev->boost_down_threshold),
193                       offs(ACTMON_DEV_DOWN_WMARK));
194 }
195
196 static inline void actmon_dev_avg_wmark_set(struct actmon_dev *dev)
197 {
198         u32 avg = dev->avg_count;
199         u32 band = dev->avg_band_freq * actmon_sampling_period;
200
201         actmon_writel(avg + band, offs(ACTMON_DEV_AVG_UP_WMARK));
202         avg = max(avg, band);
203         actmon_writel(avg - band, offs(ACTMON_DEV_AVG_DOWN_WMARK));
204 }
205
206 static unsigned long actmon_dev_avg_freq_get(struct actmon_dev *dev)
207 {
208         u64 val;
209
210         if (dev->type == ACTMON_FREQ_SAMPLER)
211                 return dev->avg_count / actmon_sampling_period;
212
213         val = (u64)dev->avg_count * dev->cur_freq;
214         do_div(val, actmon_clk_freq * actmon_sampling_period);
215         return (u32)val;
216 }
217
218 /* Activity monitor sampling operations */
219 irqreturn_t actmon_dev_isr(int irq, void *dev_id)
220 {
221         u32 val;
222         unsigned long flags;
223         struct actmon_dev *dev = (struct actmon_dev *)dev_id;
224
225         val = actmon_readl(ACTMON_GLB_STATUS) & dev->glb_status_irq_mask;
226         if (!val)
227                 return IRQ_NONE;
228
229         spin_lock_irqsave(&dev->lock, flags);
230
231         dev->avg_count = actmon_readl(offs(ACTMON_DEV_AVG_COUNT));
232         actmon_dev_avg_wmark_set(dev);
233
234         val = actmon_readl(offs(ACTMON_DEV_INTR_STATUS));
235         if (val & ACTMON_DEV_INTR_UP_WMARK) {
236                 val = actmon_readl(offs(ACTMON_DEV_CTRL)) |
237                         ACTMON_DEV_CTRL_UP_WMARK_ENB |
238                         ACTMON_DEV_CTRL_DOWN_WMARK_ENB;
239
240                 dev->boost_freq = dev->boost_freq_step +
241                         do_percent(dev->boost_freq, dev->boost_up_coef);
242                 if (dev->boost_freq >= dev->max_freq) {
243                         dev->boost_freq = dev->max_freq;
244                         val &= ~ACTMON_DEV_CTRL_UP_WMARK_ENB;
245                 }
246                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
247         } else if (val & ACTMON_DEV_INTR_DOWN_WMARK) {
248                 val = actmon_readl(offs(ACTMON_DEV_CTRL)) |
249                         ACTMON_DEV_CTRL_UP_WMARK_ENB |
250                         ACTMON_DEV_CTRL_DOWN_WMARK_ENB;
251
252                 dev->boost_freq =
253                         do_percent(dev->boost_freq, dev->boost_down_coef);
254                 if (dev->boost_freq < (dev->boost_freq_step >> 1)) {
255                         dev->boost_freq = 0;
256                         val &= ~ACTMON_DEV_CTRL_DOWN_WMARK_ENB;
257                 }
258                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
259         }
260
261         actmon_writel(0xffffffff, offs(ACTMON_DEV_INTR_STATUS)); /* clr all */
262         actmon_wmb();
263
264         spin_unlock_irqrestore(&dev->lock, flags);
265         return IRQ_WAKE_THREAD;
266 }
267
268 irqreturn_t actmon_dev_fn(int irq, void *dev_id)
269 {
270         unsigned long flags, freq;
271         struct actmon_dev *dev = (struct actmon_dev *)dev_id;
272
273         spin_lock_irqsave(&dev->lock, flags);
274
275         if (dev->state != ACTMON_ON) {
276                 spin_unlock_irqrestore(&dev->lock, flags);
277                 return IRQ_HANDLED;
278         }
279
280         freq = actmon_dev_avg_freq_get(dev);
281         dev->avg_actv_freq = freq;
282         freq = do_percent(freq, dev->avg_sustain_coef);
283         freq += dev->boost_freq;
284         dev->target_freq = freq;
285
286         spin_unlock_irqrestore(&dev->lock, flags);
287
288         pr_debug("%s.%s(kHz): avg: %lu, target: %lu current: %lu\n",
289                         dev->dev_id, dev->con_id, dev->avg_actv_freq,
290                         dev->target_freq, dev->cur_freq);
291         clk_set_rate(dev->clk, freq * 1000);
292
293         return IRQ_HANDLED;
294 }
295
296 static int actmon_rate_notify_cb(
297         struct notifier_block *nb, unsigned long rate, void *v)
298 {
299         unsigned long flags;
300         struct actmon_dev *dev = container_of(
301                 nb, struct actmon_dev, rate_change_nb);
302
303         spin_lock_irqsave(&dev->lock, flags);
304
305         dev->cur_freq = rate / 1000;
306         if (dev->type == ACTMON_FREQ_SAMPLER) {
307                 actmon_dev_wmark_set(dev);
308                 actmon_wmb();
309         }
310
311         spin_unlock_irqrestore(&dev->lock, flags);
312         return NOTIFY_OK;
313 };
314
315 /* Activity monitor configuration and control */
316 static void actmon_dev_configure(struct actmon_dev *dev, unsigned long freq)
317 {
318         u32 val;
319
320         dev->cur_freq = freq;
321         dev->target_freq = freq;
322         dev->avg_actv_freq = freq;
323
324         if (dev->type == ACTMON_FREQ_SAMPLER) {
325                 dev->avg_count = dev->cur_freq * actmon_sampling_period;
326                 dev->avg_band_freq = dev->max_freq *
327                         ACTMON_DEFAULT_AVG_BAND / 1000;
328         } else {
329                 dev->avg_count = actmon_clk_freq * actmon_sampling_period;
330                 dev->avg_band_freq = actmon_clk_freq *
331                         ACTMON_DEFAULT_AVG_BAND / 1000;
332         }
333         actmon_writel(dev->avg_count, offs(ACTMON_DEV_INIT_AVG));
334
335         BUG_ON(!dev->boost_up_threshold);
336         dev->avg_sustain_coef = 100 * 100 / dev->boost_up_threshold;
337         actmon_dev_avg_wmark_set(dev);
338         actmon_dev_wmark_set(dev);
339
340         actmon_writel(dev->count_weight, offs(ACTMON_DEV_COUNT_WEGHT));
341         actmon_writel(0xffffffff, offs(ACTMON_DEV_INTR_STATUS)); /* clr all */
342
343         val = ACTMON_DEV_CTRL_PERIODIC_ENB | ACTMON_DEV_CTRL_AVG_UP_WMARK_ENB |
344                 ACTMON_DEV_CTRL_AVG_DOWN_WMARK_ENB;
345         val |= ((dev->avg_window_log2 - 1) << ACTMON_DEV_CTRL_K_VAL_SHIFT) &
346                 ACTMON_DEV_CTRL_K_VAL_MASK;
347         val |= ((dev->down_wmark_window - 1) <<
348                 ACTMON_DEV_CTRL_DOWN_WMARK_NUM_SHIFT) &
349                 ACTMON_DEV_CTRL_DOWN_WMARK_NUM_MASK;
350         val |=  ((dev->up_wmark_window - 1) <<
351                 ACTMON_DEV_CTRL_UP_WMARK_NUM_SHIFT) &
352                 ACTMON_DEV_CTRL_UP_WMARK_NUM_MASK;
353         val |= ACTMON_DEV_CTRL_DOWN_WMARK_ENB | ACTMON_DEV_CTRL_UP_WMARK_ENB;
354         actmon_writel(val, offs(ACTMON_DEV_CTRL));
355         actmon_wmb();
356 }
357
358 static void actmon_dev_enable(struct actmon_dev *dev)
359 {
360         u32 val;
361         unsigned long flags;
362
363         spin_lock_irqsave(&dev->lock, flags);
364
365         if (dev->state == ACTMON_OFF) {
366                 dev->state = ACTMON_ON;
367
368                 val = actmon_readl(offs(ACTMON_DEV_CTRL));
369                 val |= ACTMON_DEV_CTRL_ENB;
370                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
371                 actmon_wmb();
372         }
373         spin_unlock_irqrestore(&dev->lock, flags);
374 }
375
376 static void actmon_dev_disable(struct actmon_dev *dev)
377 {
378         u32 val;
379         unsigned long flags;
380
381         spin_lock_irqsave(&dev->lock, flags);
382
383         if (dev->state == ACTMON_ON) {
384                 dev->state = ACTMON_OFF;
385
386                 val = actmon_readl(offs(ACTMON_DEV_CTRL));
387                 val &= ~ACTMON_DEV_CTRL_ENB;
388                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
389                 actmon_writel(0xffffffff, offs(ACTMON_DEV_INTR_STATUS));
390                 actmon_wmb();
391         }
392         spin_unlock_irqrestore(&dev->lock, flags);
393 }
394
395 static void actmon_dev_suspend(struct actmon_dev *dev)
396 {
397         u32 val;
398         unsigned long flags;
399
400         spin_lock_irqsave(&dev->lock, flags);
401
402         if ((dev->state == ACTMON_ON) || (dev->state == ACTMON_OFF)){
403                 dev->saved_state = dev->state;
404                 dev->state = ACTMON_SUSPENDED;
405
406                 val = actmon_readl(offs(ACTMON_DEV_CTRL));
407                 val &= ~ACTMON_DEV_CTRL_ENB;
408                 actmon_writel(val, offs(ACTMON_DEV_CTRL));
409                 actmon_writel(0xffffffff, offs(ACTMON_DEV_INTR_STATUS));
410                 actmon_wmb();
411         }
412         spin_unlock_irqrestore(&dev->lock, flags);
413
414         if (dev->suspend_freq)
415                 clk_set_rate(dev->clk, dev->suspend_freq * 1000);
416 }
417
418 static void actmon_dev_resume(struct actmon_dev *dev)
419 {
420         u32 val;
421         unsigned long flags;
422         unsigned long freq = clk_get_rate(dev->clk) / 1000;
423
424         spin_lock_irqsave(&dev->lock, flags);
425
426         if (dev->state == ACTMON_SUSPENDED) {
427                 actmon_dev_configure(dev, freq);
428                 dev->state = dev->saved_state;
429                 if (dev->state == ACTMON_ON) {
430                         val = actmon_readl(offs(ACTMON_DEV_CTRL));
431                         val |= ACTMON_DEV_CTRL_ENB;
432                         actmon_writel(val, offs(ACTMON_DEV_CTRL));
433                         actmon_wmb();
434                 }
435         }
436         spin_unlock_irqrestore(&dev->lock, flags);
437 }
438
439 static int __init actmon_dev_init(struct actmon_dev *dev)
440 {
441         int ret;
442         struct clk *p;
443         unsigned long freq;
444
445         spin_lock_init(&dev->lock);
446
447         dev->clk = clk_get_sys(dev->dev_id, dev->con_id);
448         if (IS_ERR(dev->clk)) {
449                 pr_err("Failed to find %s.%s clock\n",
450                        dev->dev_id, dev->con_id);
451                 return -ENODEV;
452         }
453         dev->max_freq = clk_round_rate(dev->clk, ULONG_MAX);
454         clk_set_rate(dev->clk, dev->max_freq);
455         dev->max_freq /= 1000;
456         freq = clk_get_rate(dev->clk) / 1000;
457         actmon_dev_configure(dev, freq);
458
459         /* actmon device controls shared bus user clock, but rate
460            change notification should come from bus clock itself */
461         p = clk_get_parent(dev->clk);
462         BUG_ON(!p);
463
464         if (dev->rate_change_nb.notifier_call) {
465                 ret = tegra_register_clk_rate_notifier(p, &dev->rate_change_nb);
466                 if (ret) {
467                         pr_err("Failed to register %s rate change notifier"
468                                " for %s\n", p->name, dev->dev_id);
469                         return ret;
470                 }
471         }
472
473         ret = request_threaded_irq(INT_ACTMON, actmon_dev_isr, actmon_dev_fn,
474                                    IRQF_SHARED, dev->dev_id, dev);
475         if (ret) {
476                 pr_err("Failed irq %d request for %s.%s\n",
477                        INT_ACTMON, dev->dev_id, dev->con_id);
478                 tegra_unregister_clk_rate_notifier(p, &dev->rate_change_nb);
479                 return ret;
480         }
481
482         dev->state = ACTMON_OFF;
483         actmon_dev_enable(dev);
484         clk_enable(dev->clk);
485         return 0;
486 }
487
488 /* EMC activity monitor: frequency sampling device:
489  * activity counter is incremented every 256 memory transactions, and
490  * each transaction takes 2 EMC clocks; count_weight = 512.
491  */
492 static struct actmon_dev actmon_dev_emc = {
493         .reg    = 0x1c0,
494         .glb_status_irq_mask = (0x1 << 26),
495         .dev_id = "tegra_actmon",
496         .con_id = "emc",
497
498         /* EMC suspend floor to guarantee suspend entry on PLLM */
499         .suspend_freq           = EMC_PLLP_FREQ_MAX + 2000,
500
501         .boost_freq_step        = 16000,
502         .boost_up_coef          = 200,
503         .boost_down_coef        = 50,
504         .boost_up_threshold     = 60,
505         .boost_down_threshold   = 40,
506
507         .up_wmark_window        = 1,
508         .down_wmark_window      = 3,
509         .avg_window_log2        = ACTMON_DEFAULT_AVG_WINDOW_LOG2,
510         .count_weight           = 0x200,
511
512         .type                   = ACTMON_FREQ_SAMPLER,
513         .state                  = ACTMON_UNINITIALIZED,
514
515         .rate_change_nb = {
516                 .notifier_call = actmon_rate_notify_cb,
517         },
518 };
519
520 /* AVP activity monitor: load sampling device:
521  * activity counter is incremented on every actmon clock pulse while
522  * AVP is not halted by flow controller; count_weight = 1.
523  */
524 static struct actmon_dev actmon_dev_avp = {
525         .reg    = 0x0c0,
526         .glb_status_irq_mask = (0x1 << 30),
527         .dev_id = "tegra_actmon",
528         .con_id = "avp",
529
530         /* AVP/SCLK suspend activity floor */
531         .suspend_freq           = 40000,
532
533         .boost_freq_step        = 8000,
534         .boost_up_coef          = 200,
535         .boost_down_coef        = 50,
536         .boost_up_threshold     = 75,
537         .boost_down_threshold   = 50,
538
539         .up_wmark_window        = 1,
540         .down_wmark_window      = 3,
541         .avg_window_log2        = ACTMON_DEFAULT_AVG_WINDOW_LOG2,
542         .count_weight           = 0x1,
543
544         .type                   = ACTMON_LOAD_SAMPLER,
545         .state                  = ACTMON_UNINITIALIZED,
546
547         .rate_change_nb = {
548                 .notifier_call = actmon_rate_notify_cb,
549         },
550 };
551
552 static struct actmon_dev *actmon_devices[] = {
553         &actmon_dev_emc,
554         &actmon_dev_avp,
555 };
556
557 /* Activity monitor suspend/resume */
558 static int actmon_pm_notify(struct notifier_block *nb,
559                             unsigned long event, void *data)
560 {
561         int i;
562
563         switch (event) {
564         case PM_SUSPEND_PREPARE:
565                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++)
566                         actmon_dev_suspend(actmon_devices[i]);
567                 break;
568         case PM_POST_SUSPEND:
569                 actmon_writel(actmon_sampling_period - 1,
570                               ACTMON_GLB_PERIOD_CTRL);
571                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++)
572                         actmon_dev_resume(actmon_devices[i]);
573                 break;
574         }
575
576         return NOTIFY_OK;
577 };
578
579 static struct notifier_block actmon_pm_nb = {
580         .notifier_call = actmon_pm_notify,
581 };
582
583 #ifdef CONFIG_DEBUG_FS
584
585 #define RW_MODE (S_IWUSR | S_IRUGO)
586 #define RO_MODE S_IRUGO
587
588 static struct dentry *clk_debugfs_root;
589
590 static int type_show(struct seq_file *s, void *data)
591 {
592         struct actmon_dev *dev = s->private;
593
594         seq_printf(s, "%s\n", (dev->type == ACTMON_LOAD_SAMPLER) ?
595                    "Load Activity Monitor" : "Frequency Activity Monitor");
596         return 0;
597 }
598 static int type_open(struct inode *inode, struct file *file)
599 {
600         return single_open(file, type_show, inode->i_private);
601 }
602 static const struct file_operations type_fops = {
603         .open           = type_open,
604         .read           = seq_read,
605         .llseek         = seq_lseek,
606         .release        = single_release,
607 };
608
609 static int actv_get(void *data, u64 *val)
610 {
611         unsigned long flags;
612         struct actmon_dev *dev = data;
613
614         spin_lock_irqsave(&dev->lock, flags);
615         *val = actmon_dev_avg_freq_get(dev);
616         spin_unlock_irqrestore(&dev->lock, flags);
617         return 0;
618 }
619 DEFINE_SIMPLE_ATTRIBUTE(actv_fops, actv_get, NULL, "%llu\n");
620
621 static int step_get(void *data, u64 *val)
622 {
623         struct actmon_dev *dev = data;
624         *val = dev->boost_freq_step * 100 / dev->max_freq;
625         return 0;
626 }
627 static int step_set(void *data, u64 val)
628 {
629         unsigned long flags;
630         struct actmon_dev *dev = data;
631
632         if (val > 100)
633                 val = 100;
634
635         spin_lock_irqsave(&dev->lock, flags);
636         dev->boost_freq_step = do_percent(dev->max_freq, (unsigned int)val);
637         spin_unlock_irqrestore(&dev->lock, flags);
638         return 0;
639 }
640 DEFINE_SIMPLE_ATTRIBUTE(step_fops, step_get, step_set, "%llu\n");
641
642 static int up_threshold_get(void *data, u64 *val)
643 {
644         struct actmon_dev *dev = data;
645         *val = dev->boost_up_threshold;
646         return 0;
647 }
648 static int up_threshold_set(void *data, u64 val)
649 {
650         unsigned long flags;
651         struct actmon_dev *dev = data;
652         unsigned int up_threshold = (unsigned int)val;
653
654         if (up_threshold > 100)
655                 up_threshold = 100;
656
657         spin_lock_irqsave(&dev->lock, flags);
658
659         if (up_threshold <= dev->boost_down_threshold)
660                 up_threshold = dev->boost_down_threshold;
661         if (up_threshold)
662                 dev->avg_sustain_coef = 100 * 100 / up_threshold;
663         dev->boost_up_threshold = up_threshold;
664
665         actmon_dev_up_wmark_set(dev);
666         actmon_wmb();
667
668         spin_unlock_irqrestore(&dev->lock, flags);
669         return 0;
670 }
671 DEFINE_SIMPLE_ATTRIBUTE(up_threshold_fops, up_threshold_get,
672                         up_threshold_set, "%llu\n");
673
674 static int down_threshold_get(void *data, u64 *val)
675 {
676         struct actmon_dev *dev = data;
677         *val = dev->boost_down_threshold;
678         return 0;
679 }
680 static int down_threshold_set(void *data, u64 val)
681 {
682         unsigned long flags;
683         struct actmon_dev *dev = data;
684         unsigned int down_threshold = (unsigned int)val;
685
686         spin_lock_irqsave(&dev->lock, flags);
687
688         if (down_threshold >= dev->boost_up_threshold)
689                 down_threshold = dev->boost_up_threshold;
690         dev->boost_down_threshold = down_threshold;
691
692         actmon_dev_down_wmark_set(dev);
693         actmon_wmb();
694
695         spin_unlock_irqrestore(&dev->lock, flags);
696         return 0;
697 }
698 DEFINE_SIMPLE_ATTRIBUTE(down_threshold_fops, down_threshold_get,
699                         down_threshold_set, "%llu\n");
700
701 static int state_get(void *data, u64 *val)
702 {
703         struct actmon_dev *dev = data;
704         *val = dev->state;
705         return 0;
706 }
707 static int state_set(void *data, u64 val)
708 {
709         struct actmon_dev *dev = data;
710
711         if (val)
712                 actmon_dev_enable(dev);
713         else
714                 actmon_dev_disable(dev);
715         return 0;
716 }
717 DEFINE_SIMPLE_ATTRIBUTE(state_fops, state_get, state_set, "%llu\n");
718
719 static int period_get(void *data, u64 *val)
720 {
721         *val = actmon_sampling_period;
722         return 0;
723 }
724 static int period_set(void *data, u64 val)
725 {
726         int i;
727         unsigned long flags;
728         u8 period = (u8)val;
729
730         if (period) {
731                 actmon_sampling_period = period;
732                 actmon_writel(period - 1, ACTMON_GLB_PERIOD_CTRL);
733
734                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
735                         struct actmon_dev *dev = actmon_devices[i];
736                         spin_lock_irqsave(&dev->lock, flags);
737                         actmon_dev_wmark_set(dev);
738                         spin_unlock_irqrestore(&dev->lock, flags);
739                 }
740                 actmon_wmb();
741                 return 0;
742         }
743         return -EINVAL;
744 }
745 DEFINE_SIMPLE_ATTRIBUTE(period_fops, period_get, period_set, "%llu\n");
746
747
748 static int actmon_debugfs_create_dev(struct actmon_dev *dev)
749 {
750         struct dentry *dir, *d;
751
752         if (dev->state == ACTMON_UNINITIALIZED)
753                 return 0;
754
755         dir = debugfs_create_dir(dev->con_id, clk_debugfs_root);
756         if (!dir)
757                 return -ENOMEM;
758
759         d = debugfs_create_file(
760                 "actv_type", RO_MODE, dir, dev, &type_fops);
761         if (!d)
762                 return -ENOMEM;
763
764         d = debugfs_create_file(
765                 "avg_activity", RO_MODE, dir, dev, &actv_fops);
766         if (!d)
767                 return -ENOMEM;
768
769         d = debugfs_create_file(
770                 "boost_step", RW_MODE, dir, dev, &step_fops);
771         if (!d)
772                 return -ENOMEM;
773
774         d = debugfs_create_u32(
775                 "boost_rate_dec", RW_MODE, dir, (u32 *)&dev->boost_down_coef);
776         if (!d)
777                 return -ENOMEM;
778
779         d = debugfs_create_u32(
780                 "boost_rate_inc", RW_MODE, dir, (u32 *)&dev->boost_up_coef);
781         if (!d)
782                 return -ENOMEM;
783
784         d = debugfs_create_file(
785                 "boost_threshold_dn", RW_MODE, dir, dev, &down_threshold_fops);
786         if (!d)
787                 return -ENOMEM;
788
789         d = debugfs_create_file(
790                 "boost_threshold_up", RW_MODE, dir, dev, &up_threshold_fops);
791         if (!d)
792                 return -ENOMEM;
793
794         d = debugfs_create_file(
795                 "state", RW_MODE, dir, dev, &state_fops);
796         if (!d)
797                 return -ENOMEM;
798
799         return 0;
800 }
801
802 static int __init actmon_debugfs_init(void)
803 {
804         int i;
805         int ret = -ENOMEM;
806         struct dentry *d;
807
808         d = debugfs_create_dir("tegra_actmon", NULL);
809         if (!d)
810                 return ret;
811         clk_debugfs_root = d;
812
813         d = debugfs_create_file("period", RW_MODE, d, NULL, &period_fops);
814         if (!d)
815                 goto err_out;
816
817         for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
818                 ret = actmon_debugfs_create_dev(actmon_devices[i]);
819                 if (ret)
820                         goto err_out;
821         }
822         return 0;
823
824 err_out:
825         debugfs_remove_recursive(clk_debugfs_root);
826         return ret;
827 }
828
829 #endif
830
831 static int __init tegra_actmon_init(void)
832 {
833         int i, ret;
834         struct clk *c = tegra_get_clock_by_name("actmon");
835
836         if (!c) {
837                 pr_err("%s: Failed to find actmon clock\n", __func__);
838                 return 0;
839         }
840         actmon_clk_freq = clk_get_rate(c) / 1000;
841         ret = clk_enable(c);
842         if (ret) {
843                 pr_err("%s: Failed to enable actmon clock\n", __func__);
844                 return 0;
845         }
846         actmon_sampling_period = ACTMON_DEFAULT_SAMPLING_PERIOD;
847         actmon_writel(actmon_sampling_period - 1, ACTMON_GLB_PERIOD_CTRL);
848
849         for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
850                 ret = actmon_dev_init(actmon_devices[i]);
851                 pr_info("%s.%s: %s initialization (%d)\n",
852                         actmon_devices[i]->dev_id, actmon_devices[i]->con_id,
853                         ret ? "Failed" : "Completed", ret);
854         }
855         register_pm_notifier(&actmon_pm_nb);
856
857 #ifdef CONFIG_DEBUG_FS
858         actmon_debugfs_init();
859 #endif
860         return 0;
861 }
862 late_initcall(tegra_actmon_init);