ARM: tegra: actmon: adjust boost up and down value
[linux-3.10.git] / arch / arm / mach-tegra / tegra3_actmon.c
1 /*
2  * Copyright (c) 2012, NVIDIA CORPORATION.  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 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         tegra_clk_prepare_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 on Tegra3.
491  * On Tegra11 there is only 1 clock per transaction, hence weight = 256.
492  */
493 static struct actmon_dev actmon_dev_emc = {
494         .reg    = 0x1c0,
495         .glb_status_irq_mask = (0x1 << 26),
496         .dev_id = "tegra_actmon",
497         .con_id = "emc",
498
499         /* EMC suspend floor to guarantee suspend entry on PLLM */
500         .suspend_freq           = EMC_PLLP_FREQ_MAX + 2000,
501
502         .boost_freq_step        = 16000,
503         .boost_up_coef          = 200,
504         .boost_down_coef        = 50,
505 #if defined(CONFIG_ARCH_TEGRA_14x_SOC)
506         .boost_up_threshold     = 70,
507         .boost_down_threshold   = 50,
508 #else
509         .boost_up_threshold     = 60,
510         .boost_down_threshold   = 40,
511 #endif
512
513         .up_wmark_window        = 1,
514         .down_wmark_window      = 3,
515         .avg_window_log2        = ACTMON_DEFAULT_AVG_WINDOW_LOG2,
516 #if defined(CONFIG_ARCH_TEGRA_3x_SOC) || defined(CONFIG_ARCH_TEGRA_14x_SOC)
517         .count_weight           = 0x200,
518 #else
519         .count_weight           = 0x100,
520 #endif
521
522         .type                   = ACTMON_FREQ_SAMPLER,
523         .state                  = ACTMON_UNINITIALIZED,
524
525         .rate_change_nb = {
526                 .notifier_call = actmon_rate_notify_cb,
527         },
528 };
529
530 /* AVP activity monitor: load sampling device:
531  * activity counter is incremented on every actmon clock pulse while
532  * AVP is not halted by flow controller; count_weight = 1.
533  */
534 static struct actmon_dev actmon_dev_avp = {
535         .reg    = 0x0c0,
536         .glb_status_irq_mask = (0x1 << 30),
537         .dev_id = "tegra_actmon",
538         .con_id = "avp",
539
540         /* AVP/SCLK suspend activity floor */
541         .suspend_freq           = 40000,
542
543         .boost_freq_step        = 8000,
544         .boost_up_coef          = 200,
545         .boost_down_coef        = 50,
546         .boost_up_threshold     = 85,
547         .boost_down_threshold   = 50,
548
549         .up_wmark_window        = 1,
550         .down_wmark_window      = 3,
551         .avg_window_log2        = ACTMON_DEFAULT_AVG_WINDOW_LOG2,
552         .count_weight           = 0x1,
553
554         .type                   = ACTMON_LOAD_SAMPLER,
555         .state                  = ACTMON_UNINITIALIZED,
556
557         .rate_change_nb = {
558                 .notifier_call = actmon_rate_notify_cb,
559         },
560 };
561
562 static struct actmon_dev *actmon_devices[] = {
563         &actmon_dev_emc,
564         &actmon_dev_avp,
565 };
566
567 /* Activity monitor suspend/resume */
568 static int actmon_pm_notify(struct notifier_block *nb,
569                             unsigned long event, void *data)
570 {
571         int i;
572
573         switch (event) {
574         case PM_SUSPEND_PREPARE:
575                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++)
576                         actmon_dev_suspend(actmon_devices[i]);
577                 break;
578         case PM_POST_SUSPEND:
579                 actmon_writel(actmon_sampling_period - 1,
580                               ACTMON_GLB_PERIOD_CTRL);
581                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++)
582                         actmon_dev_resume(actmon_devices[i]);
583                 break;
584         }
585
586         return NOTIFY_OK;
587 };
588
589 static struct notifier_block actmon_pm_nb = {
590         .notifier_call = actmon_pm_notify,
591 };
592
593 #ifdef CONFIG_DEBUG_FS
594
595 #define RW_MODE (S_IWUSR | S_IRUGO)
596 #define RO_MODE S_IRUGO
597
598 static struct dentry *clk_debugfs_root;
599
600 static int type_show(struct seq_file *s, void *data)
601 {
602         struct actmon_dev *dev = s->private;
603
604         seq_printf(s, "%s\n", (dev->type == ACTMON_LOAD_SAMPLER) ?
605                    "Load Activity Monitor" : "Frequency Activity Monitor");
606         return 0;
607 }
608 static int type_open(struct inode *inode, struct file *file)
609 {
610         return single_open(file, type_show, inode->i_private);
611 }
612 static const struct file_operations type_fops = {
613         .open           = type_open,
614         .read           = seq_read,
615         .llseek         = seq_lseek,
616         .release        = single_release,
617 };
618
619 static int actv_get(void *data, u64 *val)
620 {
621         unsigned long flags;
622         struct actmon_dev *dev = data;
623
624         spin_lock_irqsave(&dev->lock, flags);
625         *val = actmon_dev_avg_freq_get(dev);
626         spin_unlock_irqrestore(&dev->lock, flags);
627         return 0;
628 }
629 DEFINE_SIMPLE_ATTRIBUTE(actv_fops, actv_get, NULL, "%llu\n");
630
631 static int step_get(void *data, u64 *val)
632 {
633         struct actmon_dev *dev = data;
634         *val = dev->boost_freq_step * 100 / dev->max_freq;
635         return 0;
636 }
637 static int step_set(void *data, u64 val)
638 {
639         unsigned long flags;
640         struct actmon_dev *dev = data;
641
642         if (val > 100)
643                 val = 100;
644
645         spin_lock_irqsave(&dev->lock, flags);
646         dev->boost_freq_step = do_percent(dev->max_freq, (unsigned int)val);
647         spin_unlock_irqrestore(&dev->lock, flags);
648         return 0;
649 }
650 DEFINE_SIMPLE_ATTRIBUTE(step_fops, step_get, step_set, "%llu\n");
651
652 static int up_threshold_get(void *data, u64 *val)
653 {
654         struct actmon_dev *dev = data;
655         *val = dev->boost_up_threshold;
656         return 0;
657 }
658 static int up_threshold_set(void *data, u64 val)
659 {
660         unsigned long flags;
661         struct actmon_dev *dev = data;
662         unsigned int up_threshold = (unsigned int)val;
663
664         if (up_threshold > 100)
665                 up_threshold = 100;
666
667         spin_lock_irqsave(&dev->lock, flags);
668
669         if (up_threshold <= dev->boost_down_threshold)
670                 up_threshold = dev->boost_down_threshold;
671         if (up_threshold)
672                 dev->avg_sustain_coef = 100 * 100 / up_threshold;
673         dev->boost_up_threshold = up_threshold;
674
675         actmon_dev_up_wmark_set(dev);
676         actmon_wmb();
677
678         spin_unlock_irqrestore(&dev->lock, flags);
679         return 0;
680 }
681 DEFINE_SIMPLE_ATTRIBUTE(up_threshold_fops, up_threshold_get,
682                         up_threshold_set, "%llu\n");
683
684 static int down_threshold_get(void *data, u64 *val)
685 {
686         struct actmon_dev *dev = data;
687         *val = dev->boost_down_threshold;
688         return 0;
689 }
690 static int down_threshold_set(void *data, u64 val)
691 {
692         unsigned long flags;
693         struct actmon_dev *dev = data;
694         unsigned int down_threshold = (unsigned int)val;
695
696         spin_lock_irqsave(&dev->lock, flags);
697
698         if (down_threshold >= dev->boost_up_threshold)
699                 down_threshold = dev->boost_up_threshold;
700         dev->boost_down_threshold = down_threshold;
701
702         actmon_dev_down_wmark_set(dev);
703         actmon_wmb();
704
705         spin_unlock_irqrestore(&dev->lock, flags);
706         return 0;
707 }
708 DEFINE_SIMPLE_ATTRIBUTE(down_threshold_fops, down_threshold_get,
709                         down_threshold_set, "%llu\n");
710
711 static int state_get(void *data, u64 *val)
712 {
713         struct actmon_dev *dev = data;
714         *val = dev->state;
715         return 0;
716 }
717 static int state_set(void *data, u64 val)
718 {
719         struct actmon_dev *dev = data;
720
721         if (val)
722                 actmon_dev_enable(dev);
723         else
724                 actmon_dev_disable(dev);
725         return 0;
726 }
727 DEFINE_SIMPLE_ATTRIBUTE(state_fops, state_get, state_set, "%llu\n");
728
729 static int period_get(void *data, u64 *val)
730 {
731         *val = actmon_sampling_period;
732         return 0;
733 }
734 static int period_set(void *data, u64 val)
735 {
736         int i;
737         unsigned long flags;
738         u8 period = (u8)val;
739
740         if (period) {
741                 actmon_sampling_period = period;
742                 actmon_writel(period - 1, ACTMON_GLB_PERIOD_CTRL);
743
744                 for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
745                         struct actmon_dev *dev = actmon_devices[i];
746                         spin_lock_irqsave(&dev->lock, flags);
747                         actmon_dev_wmark_set(dev);
748                         spin_unlock_irqrestore(&dev->lock, flags);
749                 }
750                 actmon_wmb();
751                 return 0;
752         }
753         return -EINVAL;
754 }
755 DEFINE_SIMPLE_ATTRIBUTE(period_fops, period_get, period_set, "%llu\n");
756
757
758 static int actmon_debugfs_create_dev(struct actmon_dev *dev)
759 {
760         struct dentry *dir, *d;
761
762         if (dev->state == ACTMON_UNINITIALIZED)
763                 return 0;
764
765         dir = debugfs_create_dir(dev->con_id, clk_debugfs_root);
766         if (!dir)
767                 return -ENOMEM;
768
769         d = debugfs_create_file(
770                 "actv_type", RO_MODE, dir, dev, &type_fops);
771         if (!d)
772                 return -ENOMEM;
773
774         d = debugfs_create_file(
775                 "avg_activity", RO_MODE, dir, dev, &actv_fops);
776         if (!d)
777                 return -ENOMEM;
778
779         d = debugfs_create_file(
780                 "boost_step", RW_MODE, dir, dev, &step_fops);
781         if (!d)
782                 return -ENOMEM;
783
784         d = debugfs_create_u32(
785                 "boost_rate_dec", RW_MODE, dir, (u32 *)&dev->boost_down_coef);
786         if (!d)
787                 return -ENOMEM;
788
789         d = debugfs_create_u32(
790                 "boost_rate_inc", RW_MODE, dir, (u32 *)&dev->boost_up_coef);
791         if (!d)
792                 return -ENOMEM;
793
794         d = debugfs_create_file(
795                 "boost_threshold_dn", RW_MODE, dir, dev, &down_threshold_fops);
796         if (!d)
797                 return -ENOMEM;
798
799         d = debugfs_create_file(
800                 "boost_threshold_up", RW_MODE, dir, dev, &up_threshold_fops);
801         if (!d)
802                 return -ENOMEM;
803
804         d = debugfs_create_file(
805                 "state", RW_MODE, dir, dev, &state_fops);
806         if (!d)
807                 return -ENOMEM;
808
809         return 0;
810 }
811
812 static int __init actmon_debugfs_init(void)
813 {
814         int i;
815         int ret = -ENOMEM;
816         struct dentry *d;
817
818         d = debugfs_create_dir("tegra_actmon", NULL);
819         if (!d)
820                 return ret;
821         clk_debugfs_root = d;
822
823         d = debugfs_create_file("period", RW_MODE, d, NULL, &period_fops);
824         if (!d)
825                 goto err_out;
826
827         for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
828                 ret = actmon_debugfs_create_dev(actmon_devices[i]);
829                 if (ret)
830                         goto err_out;
831         }
832         return 0;
833
834 err_out:
835         debugfs_remove_recursive(clk_debugfs_root);
836         return ret;
837 }
838
839 #endif
840
841 static int __init tegra_actmon_init(void)
842 {
843         int i, ret;
844         struct clk *c = tegra_get_clock_by_name("actmon");
845
846         if (!c) {
847                 pr_err("%s: Failed to find actmon clock\n", __func__);
848                 return 0;
849         }
850         actmon_clk_freq = clk_get_rate(c) / 1000;
851         ret = tegra_clk_prepare_enable(c);
852         if (ret) {
853                 pr_err("%s: Failed to enable actmon clock\n", __func__);
854                 return 0;
855         }
856         actmon_sampling_period = ACTMON_DEFAULT_SAMPLING_PERIOD;
857         actmon_writel(actmon_sampling_period - 1, ACTMON_GLB_PERIOD_CTRL);
858
859         for (i = 0; i < ARRAY_SIZE(actmon_devices); i++) {
860                 ret = actmon_dev_init(actmon_devices[i]);
861                 pr_info("%s.%s: %s initialization (%d)\n",
862                         actmon_devices[i]->dev_id, actmon_devices[i]->con_id,
863                         ret ? "Failed" : "Completed", ret);
864         }
865         register_pm_notifier(&actmon_pm_nb);
866
867 #ifdef CONFIG_DEBUG_FS
868         actmon_debugfs_init();
869 #endif
870         return 0;
871 }
872 late_initcall(tegra_actmon_init);