summaryrefslogtreecommitdiff
path: root/firmware/target/hosted/power-linux.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/hosted/power-linux.c')
-rw-r--r--firmware/target/hosted/power-linux.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/firmware/target/hosted/power-linux.c b/firmware/target/hosted/power-linux.c
new file mode 100644
index 0000000000..ea8d787ad5
--- /dev/null
+++ b/firmware/target/hosted/power-linux.c
@@ -0,0 +1,62 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2021 by Solomon Peachy
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20#include <sys/types.h>
21#include <fcntl.h>
22#include <string.h>
23#include <unistd.h>
24#include <stdio.h>
25
26#include "system.h"
27#include "power.h"
28#include "panic.h"
29#include "sysfs.h"
30
31#include "tick.h"
32
33#define BATTERY_STATUS_PATH "/sys/class/power_supply/" BATTERY_DEV_NAME "/status"
34#define POWER_STATUS_PATH "/sys/class/power_supply/" POWER_DEV_NAME "/online"
35
36/* We get called multiple times per tick, let's cut that back! */
37static long last_tick = 0;
38static bool last_power = false;
39
40bool charging_state(void)
41{
42 if ((current_tick - last_tick) > HZ/2 ) {
43 char buf[12] = {0};
44 sysfs_get_string(BATTERY_STATUS_PATH, buf, sizeof(buf));
45
46 last_tick = current_tick;
47 last_power = (strncmp(buf, "Charging", 8) == 0);
48 }
49 return last_power;
50}
51
52unsigned int power_input_status(void)
53{
54 int present = 0;
55 sysfs_get_int(POWER_STATUS_PATH, &present);
56
57#ifdef FIIO_M3K_LINUX
58 usb_enable(present ? true : false);
59#endif
60
61 return present ? POWER_INPUT_USB_CHARGER : POWER_INPUT_NONE;
62}