1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/ioctl.h>
4 #include <errno.h>
5 #include <getopt.h>
6 #include <stdbool.h>
7 #include <limits.h>
8 #include <string.h>
9 #include <inttypes.h>
10 #include <linux/mfd/adnc/iaxxx-module.h>
11 
12 #define LOG_TAG "ia_get_pwr_stats"
13 
14 #include <log/log.h>
15 
16 #define GETOPT_HELP_CHAR (CHAR_MIN - 2)
17 
18 #define DEV_NODE "/dev/iaxxx-module-celldrv"
19 
20 struct ia_pwr_mgr {
21     FILE *dev_node;
22 };
23 
24 static struct option const long_options[] =
25 {
26     {"cumulative count", no_argument, NULL, 'c'},
27     {"help", no_argument, NULL, GETOPT_HELP_CHAR},
28     {NULL, 0, NULL, 0}
29 };
30 
usage()31 void usage() {
32     fputs("\
33     USAGE -\n\
34     -------\n\
35     1) get_pwr_stats -c\n\
36     \n\
37     get pwr stats with all cumulative counters for each\n\
38     mpll and apll clock freq values\n\
39     ", stdout);
40 
41     fputs("\n\
42     OPTIONS - \n\
43     ---------\n\
44     -c     get pwr stats for each mpll and apll clk values.\n\
45     ", stdout);
46 
47     exit(EXIT_FAILURE);
48 }
49 
main(int argc,char * argv[])50 int main(int argc, char *argv[]) {
51     struct ia_pwr_mgr * pwr = NULL;
52     struct iaxxx_pwr_stats pwr_stats_count;
53     char use_case;
54     int getchr;
55     int i = 0;
56     int err = 0;
57 
58     if (argc <= 1) {
59         usage();
60     }
61 
62     while ((getchr = getopt_long (argc, argv, "c", long_options, NULL)) != -1) {
63         switch (getchr) {
64         case 'c':
65             use_case = 'c';
66          break;
67         case GETOPT_HELP_CHAR:
68         default:
69             usage();
70         break;
71         }
72     }
73 
74     pwr = (struct ia_pwr_mgr*) malloc(sizeof(struct ia_pwr_mgr));
75     if (NULL == pwr) {
76         ALOGE("ERROR Failed to allocated memory for ia_pwr_mgr");
77         fprintf(stderr, "ERROR Failed to allocated memory for ia_pwr_mgr");
78         return -ENOMEM;
79     }
80 
81     if((pwr->dev_node = fopen(DEV_NODE, "r")) == NULL) {
82         ALOGE("%s: ERROR file %s open for write error: %s\n",
83                 __func__, DEV_NODE, strerror(errno));
84         err = -EBADFD;
85         fprintf(stderr, "%s: ERROR file %s open for write error: %s\n", __func__, DEV_NODE, strerror(errno));
86         goto out;
87     }
88 
89     if ('c' == use_case) {
90         err = ioctl(fileno(pwr->dev_node), IAXXX_POWER_STATS_COUNT, &pwr_stats_count);
91         if (0 != err) {
92             ALOGE("ERROR: POWER STATS COUNT failed %d(%s)", errno, strerror(errno));
93             fprintf(stderr, "ERROR: POWER STATS COUNT failed %d(%s)", errno, strerror(errno));
94             goto out;
95         }
96 
97         for (i = 0; i < NUM_MPLL_CLK_FREQ; i++){
98             ALOGI("mpll_cumulative_cnts (%d) counter value: %"PRIu64"\n", i, pwr_stats_count.mpll_cumulative_cnts[i]);
99             fprintf(stdout, "mpll_cumulative_cnts (%d) counter value: %"PRIu64"\n", i, pwr_stats_count.mpll_cumulative_cnts[i]);
100         }
101 
102         for (i = 0; i < NUM_APLL_CLK_FREQ; i++){
103             ALOGI("apll_cumulative_cnts (%d) counter value: %"PRIu64"\n", i, pwr_stats_count.apll_cumulative_cnts[i]);
104             fprintf(stdout, "apll_cumulative_cnts (%d) counter value: %"PRIu64"\n", i, pwr_stats_count.apll_cumulative_cnts[i]);
105         }
106 
107         for (i = 0; i < NUM_MPLL_CLK_FREQ; i++){
108             ALOGI("mpllCumulativeDur (%d) Duration  value: %" PRIu64 "\n", i, pwr_stats_count.mpllCumulativeDur[i]);
109             fprintf(stdout, "mpllCumulativeDur (%d) Duration  value %" PRIu64 "\n", i, pwr_stats_count.mpllCumulativeDur[i]);
110         }
111 
112         for (i = 0; i < NUM_MPLL_CLK_FREQ; i++){
113             ALOGI("mpllTimeStamp (%d) Last time stamp value : %" PRIu64 "\n", i, pwr_stats_count.mpllTimeStamp[i]);
114             fprintf(stdout, "mpllTimeStamp (%d) Last time stamp value : %"PRIu64"\n", i, pwr_stats_count.mpllTimeStamp[i]);
115         }
116 
117         ALOGI("sleepModeTimeStamp, Last time stamp value for Sleep : %" PRIu64 "\n", pwr_stats_count.sleepModeTimeStamp);
118         fprintf(stdout, "sleepModeTimeStamp, Last time stamp value for Sleep : %" PRIu64"\n", pwr_stats_count.sleepModeTimeStamp);
119 
120         ALOGI("sleepModeCumulativeDur, Total cumulative sleep duration : %"PRIu64"\n", pwr_stats_count.sleepModeCumulativeDur);
121         fprintf(stdout, "sleepModeCumulativeDur, Total cumulative sleep duration : %"PRIu64"\n", pwr_stats_count.sleepModeCumulativeDur);
122 
123 	ALOGI("POWER STATS COUNT success\n");
124         fprintf(stdout, "POWER STATS COUNT success\n");
125     }
126 
127 out:
128     if (pwr) {
129         if (pwr->dev_node) {
130             fclose(pwr->dev_node);
131         }
132         free(pwr);
133         pwr = NULL;
134     }
135 
136     return err;
137 }
138