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