2 * Copyright (C) 2011-2013 Team XBMC
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)
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.
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/>.
28 #include "utils/CPUInfo.h"
29 #include "utils/log.h"
30 #include "utils/StringUtils.h"
31 #include "utils/AMLUtils.h"
33 int aml_set_sysfs_str(const char *path, const char *val)
35 int fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
38 write(fd, val, strlen(val));
45 int aml_get_sysfs_str(const char *path, char *valstr, const int size)
47 int fd = open(path, O_RDONLY);
50 read(fd, valstr, size - 1);
51 valstr[strlen(valstr)] = '\0';
56 sprintf(valstr, "%s", "fail");
60 int aml_set_sysfs_int(const char *path, const int val)
62 int fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
66 sprintf(bcmd, "%d", val);
67 write(fd, bcmd, strlen(bcmd));
74 int aml_get_sysfs_int(const char *path)
77 int fd = open(path, O_RDONLY);
81 read(fd, bcmd, sizeof(bcmd));
82 val = strtol(bcmd, NULL, 16);
90 static int has_aml = -1;
93 int rtn = aml_get_sysfs_int("/sys/class/audiodsp/digital_raw");
99 CLog::Log(LOGNOTICE, "aml_present, rtn(%d)", rtn);
104 bool aml_hw3d_present()
106 static int has_hw3d = -1;
109 if (aml_get_sysfs_int("/sys/class/ppmgr/ppmgr_3d_mode") != -1)
114 return has_hw3d == 1;
117 bool aml_wired_present()
119 static int has_wired = -1;
123 if (aml_get_sysfs_str("/sys/class/net/eth0/operstate", test, 63) != -1)
128 return has_wired == 1;
131 void aml_permissions()
136 // most all aml devices are already rooted.
137 int ret = system("ls /system/xbin/su");
140 CLog::Log(LOGWARNING, "aml_permissions: missing su, playback might fail");
144 // certain aml devices have 664 permission, we need 666.
145 system("su -c chmod 666 /dev/amvideo");
146 system("su -c chmod 666 /dev/amstream*");
147 system("su -c chmod 666 /sys/class/video/axis");
148 system("su -c chmod 666 /sys/class/video/screen_mode");
149 system("su -c chmod 666 /sys/class/video/disable_video");
150 system("su -c chmod 666 /sys/class/tsync/pts_pcrscr");
151 system("su -c chmod 666 /sys/class/audiodsp/digital_raw");
152 system("su -c chmod 666 /sys/class/ppmgr/ppmgr_3d_mode");
153 system("su -c chmod 666 /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq");
154 system("su -c chmod 666 /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq");
155 system("su -c chmod 666 /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor");
156 CLog::Log(LOGINFO, "aml_permissions: permissions changed");
160 enum AML_DEVICE_TYPE aml_get_cputype()
162 static enum AML_DEVICE_TYPE aml_cputype = AML_DEVICE_TYPE_UNKNOWN;
163 if (aml_cputype == AML_DEVICE_TYPE_UNKNOWN)
165 std::string cpu_hardware = g_cpuInfo.getCPUHardware();
167 // default to AMLogic M1
168 aml_cputype = AML_DEVICE_TYPE_M1;
169 if (cpu_hardware.find("MESON-M3") != std::string::npos)
170 aml_cputype = AML_DEVICE_TYPE_M3;
171 else if (cpu_hardware.find("MESON3") != std::string::npos)
172 aml_cputype = AML_DEVICE_TYPE_M3;
173 else if (cpu_hardware.find("Meson6") != std::string::npos)
174 aml_cputype = AML_DEVICE_TYPE_M6;
175 else if (cpu_hardware.find("Meson8") != std::string::npos)
176 aml_cputype = AML_DEVICE_TYPE_M8;
182 void aml_cpufreq_min(bool limit)
184 // do not touch scaling_min_freq on android
185 #if !defined(TARGET_ANDROID)
186 // only needed for m1/m3 SoCs
187 if (aml_get_cputype() <= AML_DEVICE_TYPE_M3)
189 int cpufreq = 300000;
193 aml_set_sysfs_int("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq", cpufreq);
198 void aml_cpufreq_max(bool limit)
200 if (!aml_wired_present() && aml_get_cputype() > AML_DEVICE_TYPE_M3)
202 // this is a MX Stick, they cannot substain 1GHz
203 // operation without overheating so limit them to 800MHz.
204 int cpufreq = 1000000;
208 aml_set_sysfs_int("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", cpufreq);
209 aml_set_sysfs_str("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor", "ondemand");
213 void aml_set_audio_passthrough(bool passthrough)
217 // m1 uses 1, m3 and above uses 2
218 int raw = aml_get_cputype() < AML_DEVICE_TYPE_M3 ? 1:2;
219 aml_set_sysfs_int("/sys/class/audiodsp/digital_raw", passthrough ? raw:0);
223 void aml_probe_hdmi_audio()
225 // Audio {format, channel, freq, cce}
233 int fd = open("/sys/class/amhdmitx/amhdmitx0/edid", O_RDONLY);
236 char valstr[1024] = {0};
238 read(fd, valstr, sizeof(valstr) - 1);
239 valstr[strlen(valstr)] = '\0';
242 std::vector<std::string> probe_str = StringUtils::Split(valstr, "\n");
244 for (std::vector<std::string>::const_iterator i = probe_str.begin(); i != probe_str.end(); ++i)
246 if (i->find("Audio") == std::string::npos)
248 for (std::vector<std::string>::const_iterator j = i + 1; j != probe_str.end(); ++j)
250 if (j->find("{1,") != std::string::npos)
251 printf(" PCM found {1,\n");
252 else if (j->find("{2,") != std::string::npos)
253 printf(" AC3 found {2,\n");
254 else if (j->find("{3,") != std::string::npos)
255 printf(" MPEG1 found {3,\n");
256 else if (j->find("{4,") != std::string::npos)
257 printf(" MP3 found {4,\n");
258 else if (j->find("{5,") != std::string::npos)
259 printf(" MPEG2 found {5,\n");
260 else if (j->find("{6,") != std::string::npos)
261 printf(" AAC found {6,\n");
262 else if (j->find("{7,") != std::string::npos)
263 printf(" DTS found {7,\n");
264 else if (j->find("{8,") != std::string::npos)
265 printf(" ATRAC found {8,\n");
266 else if (j->find("{9,") != std::string::npos)
267 printf(" One_Bit_Audio found {9,\n");
268 else if (j->find("{10,") != std::string::npos)
269 printf(" Dolby found {10,\n");
270 else if (j->find("{11,") != std::string::npos)
271 printf(" DTS_HD found {11,\n");
272 else if (j->find("{12,") != std::string::npos)
273 printf(" MAT found {12,\n");
274 else if (j->find("{13,") != std::string::npos)
275 printf(" ATRAC found {13,\n");
276 else if (j->find("{14,") != std::string::npos)
277 printf(" WMA found {14,\n");