2522b0b066a910cefb198dbafaa480e51c8f80c8
[pcsx_rearmed.git] / frontend / plat_pandora.c
1 /*
2  * (C) notaz, 2011
3  *
4  * This work is licensed under the terms of the GNU GPLv2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <sys/ioctl.h>
15 #include <unistd.h>
16 #include <linux/input.h>
17 #include <errno.h>
18
19 #include "common/input.h"
20 #include "plugin_lib.h"
21 #include "plat.h"
22 #include "main.h"
23
24 static const char * const pandora_gpio_keys[KEY_MAX + 1] = {
25         [0 ... KEY_MAX] = NULL,
26         [KEY_UP]        = "Up",
27         [KEY_LEFT]      = "Left",
28         [KEY_RIGHT]     = "Right",
29         [KEY_DOWN]      = "Down",
30         [KEY_HOME]      = "(A)",
31         [KEY_PAGEDOWN]  = "(X)",
32         [KEY_END]       = "(B)",
33         [KEY_PAGEUP]    = "(Y)",
34         [KEY_RIGHTSHIFT]= "(L)",
35         [KEY_RIGHTCTRL] = "(R)",
36         [KEY_LEFTALT]   = "Start",
37         [KEY_LEFTCTRL]  = "Select",
38         [KEY_MENU]      = "Pandora",
39 };
40
41 static const struct in_default_bind in_evdev_defbinds[] = {
42         { KEY_UP,       IN_BINDTYPE_PLAYER12, DKEY_UP },
43         { KEY_DOWN,     IN_BINDTYPE_PLAYER12, DKEY_DOWN },
44         { KEY_LEFT,     IN_BINDTYPE_PLAYER12, DKEY_LEFT },
45         { KEY_RIGHT,    IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
46         { KEY_PAGEUP,   IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
47         { KEY_PAGEDOWN, IN_BINDTYPE_PLAYER12, DKEY_CROSS },
48         { KEY_END,      IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
49         { KEY_HOME,     IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
50         { KEY_LEFTALT,  IN_BINDTYPE_PLAYER12, DKEY_START },
51         { KEY_LEFTCTRL, IN_BINDTYPE_PLAYER12, DKEY_SELECT },
52         { KEY_RIGHTSHIFT,IN_BINDTYPE_PLAYER12, DKEY_L1 },
53         { KEY_RIGHTCTRL, IN_BINDTYPE_PLAYER12, DKEY_R1 },
54         { KEY_Q,        IN_BINDTYPE_PLAYER12, DKEY_L2 },
55         { KEY_P,        IN_BINDTYPE_PLAYER12, DKEY_R2 },
56         { KEY_TAB,      IN_BINDTYPE_EMU, SACTION_MINIMIZE },
57         { KEY_SPACE,    IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
58         { KEY_1,        IN_BINDTYPE_EMU, SACTION_SAVE_STATE },
59         { KEY_2,        IN_BINDTYPE_EMU, SACTION_LOAD_STATE },
60         { KEY_3,        IN_BINDTYPE_EMU, SACTION_PREV_SSLOT },
61         { KEY_4,        IN_BINDTYPE_EMU, SACTION_NEXT_SSLOT },
62         { KEY_5,        IN_BINDTYPE_EMU, SACTION_TOGGLE_FSKIP },
63         { KEY_6,        IN_BINDTYPE_EMU, SACTION_SCREENSHOT },
64         { 0, 0, 0 }
65 };
66
67 int plat_pandora_init(void)
68 {
69         in_evdev_init(in_evdev_defbinds);
70         in_probe();
71         in_set_config(in_name_to_id("evdev:gpio-keys"), IN_CFG_KEY_NAMES,
72                       pandora_gpio_keys, sizeof(pandora_gpio_keys));
73         in_adev[0] = in_name_to_id("evdev:nub0");
74         in_adev[1] = in_name_to_id("evdev:nub1");
75
76         return 0;
77 }
78
79 static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
80
81 int plat_cpu_clock_get(void)
82 {
83         FILE *f;
84         int ret = 0;
85         f = fopen("/proc/pandora/cpu_mhz_max", "r");
86         if (f) {
87                 fscanf(f, "%d", &ret);
88                 fclose(f);
89         }
90         return ret;
91 }
92
93 int plat_cpu_clock_apply(int cpu_clock)
94 {
95         char buf[128];
96
97         if (cpu_clock != 0 && cpu_clock != plat_cpu_clock_get()) {
98                 snprintf(buf, sizeof(buf), "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
99                          pnd_script_base, cpu_clock);
100                 system(buf);
101         }
102         return 0;
103 }
104
105 int plat_get_bat_capacity(void)
106 {
107         FILE *f;
108         int ret = 0;
109         f = fopen("/sys/class/power_supply/bq27500-0/capacity", "r");
110         if (f) {
111                 fscanf(f, "%d", &ret);
112                 fclose(f);
113         }
114         return ret;
115 }