From 555299af9f0ee71c1ad86a6c6846748861be458b Mon Sep 17 00:00:00 2001 From: Solomon Peachy Date: Wed, 10 Mar 2021 18:10:00 -0500 Subject: 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 --- firmware/target/hosted/power-linux.c | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 firmware/target/hosted/power-linux.c (limited to 'firmware/target/hosted/power-linux.c') 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 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * + * Copyright (C) 2021 by Solomon Peachy + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include +#include +#include +#include +#include + +#include "system.h" +#include "power.h" +#include "panic.h" +#include "sysfs.h" + +#include "tick.h" + +#define BATTERY_STATUS_PATH "/sys/class/power_supply/" BATTERY_DEV_NAME "/status" +#define POWER_STATUS_PATH "/sys/class/power_supply/" POWER_DEV_NAME "/online" + +/* We get called multiple times per tick, let's cut that back! */ +static long last_tick = 0; +static bool last_power = false; + +bool charging_state(void) +{ + if ((current_tick - last_tick) > HZ/2 ) { + char buf[12] = {0}; + sysfs_get_string(BATTERY_STATUS_PATH, buf, sizeof(buf)); + + last_tick = current_tick; + last_power = (strncmp(buf, "Charging", 8) == 0); + } + return last_power; +} + +unsigned int power_input_status(void) +{ + int present = 0; + sysfs_get_int(POWER_STATUS_PATH, &present); + +#ifdef FIIO_M3K_LINUX + usb_enable(present ? true : false); +#endif + + return present ? POWER_INPUT_USB_CHARGER : POWER_INPUT_NONE; +} -- cgit v1.2.3