]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/arm-ds5-gator.git/blob - driver/gator_events_mmapped.c
Merge branch 'master' into android
[android-sdk/arm-ds5-gator.git] / driver / gator_events_mmapped.c
1 /*
2  * Example events provider
3  *
4  * Copyright (C) ARM Limited 2010-2014. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Similar entries to those below must be present in the events.xml file.
11  * To add them to the events.xml, create an events-mmap.xml with the 
12  * following contents and rebuild gatord:
13  *
14  * <counter_set name="mmapped_cnt" count="3"/>
15  * <category name="mmapped" counter_set="mmapped_cnt" per_cpu="no">
16  *   <event event="0x0" title="Simulated1" name="Sine" display="maximum" average_selection="yes" description="Sort-of-sine"/>
17  *   <event event="0x1" title="Simulated2" name="Triangle" display="maximum" average_selection="yes" description="Triangular wave"/>
18  *   <event event="0x2" title="Simulated3" name="PWM" display="maximum" average_selection="yes" description="PWM Signal"/>
19  * </category>
20  *
21  * When adding custom events, be sure do the following
22  * - add any needed .c files to the gator driver Makefile
23  * - call gator_events_install in the events init function
24  * - add the init function to GATOR_EVENTS_LIST in gator_main.c
25  * - add a new events-*.xml file to the gator daemon and rebuild
26  */
28 #include <linux/init.h>
29 #include <linux/io.h>
30 #include <linux/ratelimit.h>
32 #include "gator.h"
34 #define MMAPPED_COUNTERS_NUM 3
36 static int mmapped_global_enabled;
38 static struct {
39         unsigned long enabled;
40         unsigned long event;
41         unsigned long key;
42 } mmapped_counters[MMAPPED_COUNTERS_NUM];
44 static int mmapped_buffer[MMAPPED_COUNTERS_NUM * 2];
46 static s64 prev_time;
48 /* Adds mmapped_cntX directories and enabled, event, and key files to /dev/gator/events */
49 static int gator_events_mmapped_create_files(struct super_block *sb,
50                                             struct dentry *root)
51 {
52         int i;
54         for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
55                 char buf[16];
56                 struct dentry *dir;
58                 snprintf(buf, sizeof(buf), "mmapped_cnt%d", i);
59                 dir = gatorfs_mkdir(sb, root, buf);
60                 if (WARN_ON(!dir))
61                         return -1;
62                 gatorfs_create_ulong(sb, dir, "enabled",
63                                      &mmapped_counters[i].enabled);
64                 gatorfs_create_ulong(sb, dir, "event",
65                                      &mmapped_counters[i].event);
66                 gatorfs_create_ro_ulong(sb, dir, "key",
67                                         &mmapped_counters[i].key);
68         }
70         return 0;
71 }
73 static int gator_events_mmapped_start(void)
74 {
75         int i;
76         struct timespec ts;
78         getnstimeofday(&ts);
79         prev_time = timespec_to_ns(&ts);
81         mmapped_global_enabled = 0;
82         for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
83                 if (mmapped_counters[i].enabled) {
84                         mmapped_global_enabled = 1;
85                         break;
86                 }
87         }
89         return 0;
90 }
92 static void gator_events_mmapped_stop(void)
93 {
94 }
96 /* This function "simulates" counters, generating values of fancy
97  * functions like sine or triangle... */
98 static int mmapped_simulate(int counter, int delta_in_us)
99 {
100         int result = 0;
102         switch (counter) {
103         case 0:         /* sort-of-sine */
104                 {
105                         static int t = 0;
106                         int x;
108                         t += delta_in_us;
109                         if (t > 2048000)
110                                 t = 0;
112                         if (t % 1024000 < 512000)
113                                 x = 512000 - (t % 512000);
114                         else
115                                 x = t % 512000;
117                         result = 32 * x / 512000;
118                         result = result * result;
120                         if (t < 1024000)
121                                 result = 1922 - result;
122                 }
123                 break;
124         case 1:         /* triangle */
125                 {
126                         static int v, d = 1;
128                         v = v + d * delta_in_us;
129                         if (v < 0) {
130                                 v = 0;
131                                 d = 1;
132                         } else if (v > 1000000) {
133                                 v = 1000000;
134                                 d = -1;
135                         }
137                         result = v;
138                 }
139                 break;
140         case 2:         /* PWM signal */
141                 {
142                         static int dc, x, t = 0;
144                         t += delta_in_us;
145                         if (t > 1000000)
146                                 t = 0;
147                         if (x / 1000000 != (x + delta_in_us) / 1000000)
148                                 dc = (dc + 100000) % 1000000;
149                         x += delta_in_us;
151                         result = t < dc ? 0 : 10;
152                 }
153                 break;
154         }
156         return result;
159 static int gator_events_mmapped_read(int **buffer)
161         int i;
162         int len = 0;
163         int delta_in_us;
164         struct timespec ts;
165         s64 time;
167         /* System wide counters - read from one core only */
168         if (!on_primary_core() || !mmapped_global_enabled)
169                 return 0;
171         getnstimeofday(&ts);
172         time = timespec_to_ns(&ts);
173         delta_in_us = (int)(time - prev_time) / 1000;
174         prev_time = time;
176         for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
177                 if (mmapped_counters[i].enabled) {
178                         mmapped_buffer[len++] = mmapped_counters[i].key;
179                         mmapped_buffer[len++] =
180                             mmapped_simulate(mmapped_counters[i].event,
181                                             delta_in_us);
182                 }
183         }
185         if (buffer)
186                 *buffer = mmapped_buffer;
188         return len;
191 static struct gator_interface gator_events_mmapped_interface = {
192         .create_files = gator_events_mmapped_create_files,
193         .start = gator_events_mmapped_start,
194         .stop = gator_events_mmapped_stop,
195         .read = gator_events_mmapped_read,
196 };
198 /* Must not be static! */
199 int __init gator_events_mmapped_init(void)
201         int i;
203         for (i = 0; i < MMAPPED_COUNTERS_NUM; i++) {
204                 mmapped_counters[i].enabled = 0;
205                 mmapped_counters[i].key = gator_events_get_key();
206         }
208         return gator_events_install(&gator_events_mmapped_interface);