diff options
author | Solomon Peachy <pizza@shaftnet.org> | 2021-03-10 18:10:00 -0500 |
---|---|---|
committer | Solomon Peachy <pizza@shaftnet.org> | 2021-03-10 18:29:29 -0500 |
commit | 555299af9f0ee71c1ad86a6c6846748861be458b (patch) | |
tree | 2237e2991231cf529420f52c10a91912ed94bf06 /firmware/target/hosted/power-linux.c | |
parent | b3b8310e4e07902a8280a120948f0c32c85e67de (diff) | |
download | rockbox-555299af9f0ee71c1ad86a6c6846748861be458b.tar.gz rockbox-555299af9f0ee71c1ad86a6c6846748861be458b.zip |
hosted: Consolidate the code that polls the battery charging status
affects all hiby targets, fiiom3k, and ibasso dx50/dx90
As well as deduplicating a small pile of code, this also implements
hysteresis so we're not doing a sysfs read/lookup multiple times
back-to-back every time the power management tick fires.
Change-Id: I2f7672acbb36341becf67e07960c24c681270d09
Diffstat (limited to 'firmware/target/hosted/power-linux.c')
-rw-r--r-- | firmware/target/hosted/power-linux.c | 62 |
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! */ | ||
37 | static long last_tick = 0; | ||
38 | static bool last_power = false; | ||
39 | |||
40 | bool 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 | |||
52 | unsigned 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 | } | ||