Merge pull request #2957 from jmbreuer/upstream-libdvdnav-seek
[vuplus_xbmc] / xbmc / utils / AMLUtils.cpp
1 /*
2  *      Copyright (C) 2011-2012 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <fcntl.h>
26 #include <string>
27
28 #include "utils/StringUtils.h"
29
30 int aml_set_sysfs_str(const char *path, const char *val)
31 {
32   int fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
33   if (fd >= 0)
34   {
35     write(fd, val, strlen(val));
36     close(fd);
37     return 0;
38   }
39   return -1;
40 }
41
42 int aml_get_sysfs_str(const char *path, char *valstr, const int size)
43 {
44   int fd = open(path, O_RDONLY);
45   if (fd >= 0)
46   {
47     read(fd, valstr, size - 1);
48     valstr[strlen(valstr)] = '\0';
49     close(fd);
50     return 0;
51   }
52
53   sprintf(valstr, "%s", "fail");
54   return -1;
55 }
56
57 int aml_set_sysfs_int(const char *path, const int val)
58 {
59   int fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
60   if (fd >= 0)
61   {
62     char bcmd[16];
63     sprintf(bcmd, "%d", val);
64     write(fd, bcmd, strlen(bcmd));
65     close(fd);
66     return 0;
67   }
68   return -1;
69 }
70
71 int aml_get_sysfs_int(const char *path)
72 {
73   int val = 0;
74   int fd = open(path, O_RDONLY);
75   if (fd >= 0)
76   {
77     char bcmd[16];
78     read(fd, bcmd, sizeof(bcmd));
79     val = strtol(bcmd, NULL, 16);
80     close(fd);
81   }
82   return val;
83 }
84
85 bool aml_present()
86 {
87   static int has_aml = -1;
88   if (has_aml == -1)
89   {
90     if (aml_get_sysfs_int("/sys/class/audiodsp/digital_raw") != -1)
91       has_aml = 1;
92     else
93       has_aml = 0;
94   }
95   return has_aml;
96 }
97
98 int aml_get_cputype()
99 {
100   static int aml_cputype = -1;
101   if (aml_cputype == -1)
102   {
103     // defualt to m1 SoC
104     aml_cputype = 1;
105
106     FILE *cpuinfo_fd = fopen("/proc/cpuinfo", "r");
107     if (cpuinfo_fd)
108     {
109       char buffer[512];
110       while (fgets(buffer, sizeof(buffer), cpuinfo_fd))
111       {
112         std::string stdbuffer(buffer);
113         if (stdbuffer.find("MESON-M3") != std::string::npos)
114         {
115           aml_cputype = 3;
116           break;
117         }
118         else if (stdbuffer.find("MESON3") != std::string::npos)
119         {
120           aml_cputype = 3;
121           break;
122         }
123         else if (stdbuffer.find("Meson6") != std::string::npos)
124         {
125           aml_cputype = 6;
126           break;
127         }
128       }
129       fclose(cpuinfo_fd);
130     }
131   }
132
133   return aml_cputype;
134 }
135
136 void aml_cpufreq_limit(bool limit)
137 {
138   int cpufreq = 300000;
139   if (limit)
140     cpufreq = 600000;
141
142   aml_set_sysfs_int("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq", cpufreq);
143 }
144
145 void aml_set_audio_passthrough(bool passthrough)
146 {
147   if (aml_present())
148   {
149     int raw = aml_get_cputype() < 3 ? 1:2;
150     aml_set_sysfs_int("/sys/class/audiodsp/digital_raw", passthrough ? raw:0);
151   }
152 }
153
154 void aml_probe_hdmi_audio()
155 {
156   std::vector<CStdString> audio_formats;
157   // Audio {format, channel, freq, cce}
158   // {1, 7, 7f, 7}
159   // {7, 5, 1e, 0}
160   // {2, 5, 7, 0}
161   // {11, 7, 7e, 1}
162   // {10, 7, 6, 0}
163   // {12, 7, 7e, 0}
164
165   int fd = open("/sys/class/amhdmitx/amhdmitx0/edid", O_RDONLY);
166   if (fd >= 0)
167   {
168     char valstr[1024] = {0};
169
170     read(fd, valstr, sizeof(valstr) - 1);
171     valstr[strlen(valstr)] = '\0';
172     close(fd);
173
174     std::vector<CStdString> probe_str;
175     StringUtils::SplitString(valstr, "\n", probe_str);
176
177     for (size_t i = 0; i < probe_str.size(); i++)
178     {
179       if (probe_str[i].find("Audio") == std::string::npos)
180       {
181         for (size_t j = i+1; j < probe_str.size(); j++)
182         {
183           if      (probe_str[i].find("{1,")  != std::string::npos)
184             printf(" PCM found {1,\n");
185           else if (probe_str[i].find("{2,")  != std::string::npos)
186             printf(" AC3 found {2,\n");
187           else if (probe_str[i].find("{3,")  != std::string::npos)
188             printf(" MPEG1 found {3,\n");
189           else if (probe_str[i].find("{4,")  != std::string::npos)
190             printf(" MP3 found {4,\n");
191           else if (probe_str[i].find("{5,")  != std::string::npos)
192             printf(" MPEG2 found {5,\n");
193           else if (probe_str[i].find("{6,")  != std::string::npos)
194             printf(" AAC found {6,\n");
195           else if (probe_str[i].find("{7,")  != std::string::npos)
196             printf(" DTS found {7,\n");
197           else if (probe_str[i].find("{8,")  != std::string::npos)
198             printf(" ATRAC found {8,\n");
199           else if (probe_str[i].find("{9,")  != std::string::npos)
200             printf(" One_Bit_Audio found {9,\n");
201           else if (probe_str[i].find("{10,") != std::string::npos)
202             printf(" Dolby found {10,\n");
203           else if (probe_str[i].find("{11,") != std::string::npos)
204             printf(" DTS_HD found {11,\n");
205           else if (probe_str[i].find("{12,") != std::string::npos)
206             printf(" MAT found {12,\n");
207           else if (probe_str[i].find("{13,") != std::string::npos)
208             printf(" ATRAC found {13,\n");
209           else if (probe_str[i].find("{14,") != std::string::npos)
210             printf(" WMA found {14,\n");
211           else
212             break;
213         }
214         break;
215       }
216     }
217   }
218 }