Skip to content

Commit

Permalink
Support rapid charge mode (#180)
Browse files Browse the repository at this point in the history
* IdeaVPC: update headers, follow BM_RAPIDCHARGE_CAP

* Pane: enabled support for rapid charge mode, default state to off
  • Loading branch information
zhen-zen authored Nov 6, 2022
1 parent 89b0672 commit 88955f7
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 57 deletions.
50 changes: 27 additions & 23 deletions YogaSMC/YogaVPC/IdeaVPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool IdeaVPC::initVPC() {
setPropertyBoolean(capabilities, "Camera", (config >> CFG_CAMERA_BIT) & 0x1);
setPropertyBoolean(capabilities, "Touchpad", (config >> CFG_TOUCHPAD_BIT) & 0x1);

UInt8 cap_graphics = config >> CFG_GRAPHICS_BIT & 0x7;
UInt8 cap_graphics = config >> CFG_GRAPHICS_3BIT & 0x7;
switch (cap_graphics) {
case 1:
setPropertyString(capabilities, "Graphics", "Intel");
Expand Down Expand Up @@ -322,13 +322,13 @@ void IdeaVPC::updateKeyboardCapability() {
return;
}

backlightCap = BIT(HA_BACKLIGHT_CAP_BIT) & kbdState;
backlightCap = BIT(HA_BACKLIGHT_CAP) & kbdState;
if (!backlightCap)
setProperty(backlightPrompt, "unsupported");
else
setProperty(autoBacklightPrompt, automaticBacklightMode, 8);

FnlockCap = BIT(HA_FNLOCK_CAP_BIT) & kbdState;
FnlockCap = BIT(HA_FNLOCK_CAP) & kbdState;
if (!FnlockCap)
setProperty(FnKeyPrompt, "unsupported");

Expand All @@ -337,7 +337,7 @@ void IdeaVPC::updateKeyboardCapability() {
else
setProperty("PrimeKeyType", "FnKey");

alwaysOnUSBCap = BIT(HA_AOUSB_CAP_BIT) & kbdState;
alwaysOnUSBCap = BIT(HA_AOUSB_CAP) & kbdState;
if (!alwaysOnUSBCap)
setProperty(alwaysOnUSBPrompt, "unsupported");
}
Expand All @@ -352,16 +352,20 @@ void IdeaVPC::updateBatteryCapability() {
return;
}

rapidChargeCap = BIT(BM_RAPIDCHARGE_CAP) & batState;
if (!rapidChargeCap)
setProperty(rapidChargePrompt, "unsupported");

OSDictionary *bat0 = OSDictionary::withCapacity(5);
OSDictionary *bat1 = OSDictionary::withCapacity(5);
if (BIT(BM_BATTERY0BAD_BIT) & batState) {
if (BIT(BM0_BATTERYBAD_BIT) & batState) {
bat0->setObject("Critical", kOSBooleanTrue);
setProperty("Battery", "Critical");
AlwaysLog("Battery 0 critical!");
conservationModeLock = true;
}

if (BIT(BM_BATTERY1BAD_BIT) & batState) {
if (BIT(BM1_BATTERYBAD_BIT) & batState) {
bat1->setObject("Critical", kOSBooleanTrue);
setProperty("Battery", "Critical");
AlwaysLog("Battery 1 critical!");
Expand Down Expand Up @@ -546,15 +550,16 @@ bool IdeaVPC::updateBattery(bool update) {
return false;
}

conservationMode = BIT(BM_CONSERVATION_BIT) & state;
rapidChargeMode = BIT(BM_RAPIDCHARGE_BIT) & state;

if (update) {
DebugLog(updateSuccess, batteryPrompt, state);
}

conservationMode = BIT(BM0_CONSERVATION_BIT) & state;
setProperty(conservationPrompt, conservationMode);
setProperty(rapidChargePrompt, rapidChargeMode);
if (rapidChargeCap) {
rapidChargeMode = BIT(BM_RAPIDCHARGE_BIT) & state;
setProperty(rapidChargePrompt, rapidChargeMode);
}

return true;
}
Expand All @@ -577,21 +582,21 @@ bool IdeaVPC::updateKeyboard(bool update) {
// Inference from brightness cycle: 0 -> 1 -> 2 -> 0
backlightLevel = (BIT(HA_BACKLIGHT_BIT) & state) ? (backlightLevel ? 2 : 1) : 0;
}
if (FnlockCap)
FnlockMode = BIT(HA_FNLOCK_BIT) & state;
if (alwaysOnUSBCap)
alwaysOnUSBMode = BIT(HA_AOUSB_BIT) & state;

if (update) {
DebugLog(updateSuccess, keyboardPrompt, state);
}

if (backlightCap)
setProperty(backlightPrompt, backlightLevel, 32);
if (FnlockCap)
if (FnlockCap) {
FnlockMode = BIT(HA_FNLOCK_BIT) & state;
setProperty(FnKeyPrompt, FnlockMode);
if (alwaysOnUSBCap)
}
if (alwaysOnUSBCap) {
setProperty(alwaysOnUSBPrompt, alwaysOnUSBMode);
alwaysOnUSBMode = BIT(HA_AOUSB_BIT) & state;
}

return true;
}
Expand All @@ -617,33 +622,32 @@ bool IdeaVPC::toggleAlwaysOnUSB() {
}

bool IdeaVPC::toggleConservation() {
IOReturn ret = evaluateIntegerParam(setBatteryMode, nullptr, conservationMode ? BMCMD_CONSERVATION_OFF : BMCMD_CONSERVATION_ON);
IOReturn ret = evaluateIntegerParam(setBatteryMode, nullptr, conservationMode ? BMCMD0_CONSERVATION_OFF : BMCMD0_CONSERVATION_ON);
if (ret != kIOReturnSuccess) {
AlwaysLog(toggleError, conservationPrompt, ret);
return false;
}

conservationMode = !conservationMode;
DebugLog(toggleSuccess, conservationPrompt, (conservationMode ? BMCMD_CONSERVATION_ON : BMCMD_CONSERVATION_OFF), (conservationMode ? "on" : "off"));
DebugLog(toggleSuccess, conservationPrompt, (conservationMode ? BMCMD0_CONSERVATION_ON : BMCMD0_CONSERVATION_OFF), (conservationMode ? "on" : "off"));
setProperty(conservationPrompt, conservationMode);

notifyBattery();
return true;
}

bool IdeaVPC::toggleRapidCharge() {
// experimental, reset required
if (conservationModeLock)
return false;
if (!rapidChargeCap)
return true;

IOReturn ret = evaluateIntegerParam(setBatteryMode, nullptr, rapidChargeMode ? BMCMD_RAPIDCHARGE_OFF : BMCMD_RAPIDCHARGE_ON);
IOReturn ret = evaluateIntegerParam(setBatteryMode, nullptr, rapidChargeMode ? BMCMD0_RAPIDCHARGE_OFF : BMCMD0_RAPIDCHARGE_ON);
if (ret != kIOReturnSuccess) {
AlwaysLog(toggleError, rapidChargePrompt, ret);
return false;
}

rapidChargeMode = !rapidChargeMode;
DebugLog(toggleSuccess, rapidChargePrompt, (rapidChargeMode ? BMCMD_RAPIDCHARGE_ON : BMCMD_RAPIDCHARGE_OFF), (rapidChargeMode ? "on" : "off"));
DebugLog(toggleSuccess, rapidChargePrompt, (rapidChargeMode ? BMCMD0_RAPIDCHARGE_ON : BMCMD0_RAPIDCHARGE_OFF), (rapidChargeMode ? "on" : "off"));
setProperty(rapidChargePrompt, conservationMode);

return true;
Expand Down
127 changes: 100 additions & 27 deletions YogaSMC/YogaVPC/IdeaVPC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,113 @@
// from linux/drivers/platform/x86/ideapad-laptop.c

enum {
CFG_GRAPHICS_BIT = 8,
CFG_BT_BIT = 16,
CFG_3G_BIT = 17,
CFG_WIFI_BIT = 18,
CFG_CAMERA_BIT = 19,
CFG_TOUCHPAD_BIT = 30,
CFG_WIRELESS_COEX_BIT, /* 0 */
CFG_RESERVED_BIT, /* 1 */
CFG_PROCESSOR_BIT, /* 2 */
CFG_QUITE_BIT, /* 3 */
CFG_TOUCH_BIT, /* 4 */
CFG_SUPER_PERF_BIT, /* 5 */
CFG_UNDERCLOCK_BIT, /* 6 */
CFG_DCR_BIT, /* 7 */
CFG_GRAPHICS_3BIT, /* 8 */
CFG_HDMI_BIT = 11,
CFG_ACTIVE_VGA_BIT, /* 12 */
CFG_EQ_BUTTON_3BIT, /* 13 */
CFG_BT_BIT = 16,
CFG_3G_BIT, /* 17 */
CFG_WIFI_BIT, /* 18 */
CFG_CAMERA_BIT, /* 19 */
CFG_USER_BUTTON_3BIT, /* 20 */
CFG_COLOR_ENGINE_BIT, /* 23 */
CFG_PRODUCT_INFO_2BIT, /* 24 */
CFG_TOUCHPAD_OSD_BIT, /* 26 */
CFG_NUMLK_OSD_BIT, /* 27 */
CFG_CAPSLK_OSD_BIT, /* 28 */
CFG_MIC_OSD_BIT, /* 29 */
CFG_TOUCHPAD_BIT, /* 30 */
CFG_CAMERA_OSD_BIT, /* 31 */
};

enum {
BM_RAPIDCHARGE_BIT = 2,
BM_BATTERY0BAD_BIT = 3,
BM_BATTERY1BAD_BIT = 4,
BM_CONSERVATION_BIT = 5,
BM0_CALIBRATION_BIT, /* 0 */
BM_STORAGEMODE_SEL_BIT, /* 1 */
BM_RAPIDCHARGE_BIT, /* 2 */
BM0_BATTERYBAD_BIT, /* 3 */
BM1_BATTERYBAD_BIT, /* 4 */
BM0_CONSERVATION_BIT, /* 5 */
BM1_CONSERVATION_BIT, /* 6 */
BM0_ILLEGAL_BIT, /* 7 */
BM0_PERF_DEGRADE_BIT, /* 8 */
BM0_STORAGEMODE_STA_BIT, /* 9 */
BM1_ILLEGAL_BIT, /* 10 */
BM0_NORMAL_BIT, /* 11 */
BM1_NORMAL_BIT, /* 12 */
BM1_PERF_DEGRADE_BIT, /* 13 */
BM_BATTERYNUM_BIT, /* 14 */
BM_ADAPTER_TYPE_2BIT, /* 15 */
BM_RAPIDCHARGE_CAP, /* 17 */
BM_SHIPMODE_CAP, /* 18 */
BM_ADAPTER_TYPE_CAP, /* 19 */
BM1_CALIBRATION_BIT, /* 20 */
BM1_STORAGEMODE_STA_BIT, /* 21 */
BM_SHIPMODE_BIT, /* 22 */
BM_CHARGE_THRES_BIT, /* 23 */
};

enum {
BMCMD_CONSERVATION_ON = 0x3,
BMCMD_CONSERVATION_OFF = 0x5,
BMCMD_RAPIDCHARGE_ON = 0x7,
BMCMD_RAPIDCHARGE_OFF = 0x8,
BMCMD0_CALIBRATION_END, /* 0 */
BMCMD0_CALIBRATION_INIT, /* 1 */
BMCMD0_CONSERVATION_ON = 3,
BMCMD0_CONSERVATION_OFF = 5,
BMCMD0_STORAGEMODE_CLR, /* 6 */
BMCMD0_RAPIDCHARGE_ON, /* 7 */
BMCMD0_RAPIDCHARGE_OFF, /* 8 */
BMCMD_SHIPMODE_ENTER, /* 9 */
BMCMD1_CALIBRATION_END, /* 10 */
BMCMD1_CALIBRATION_INIT, /* 11 */
BMCMD1_CONSERVATION_ON = 13,
BMCMD1_CONSERVATION_OFF = 15,
BMCMD_SHIPMODE_EXIT, /* 16 */
};

enum {
HA_BACKLIGHT_CAP_BIT = 4,
HA_BACKLIGHT_BIT = 5,
HA_AOUSB_CAP_BIT = 6,
HA_AOUSB_BIT = 7,
HA_FNLOCK_CAP_BIT = 9,
HA_FNLOCK_BIT = 10,
HA_PRIMEKEY_BIT = 11,
HA_ALS_BIT, /* 0 */
HA_ALSKBC_BIT, /* 1 */
HA_ALSKBC_CAP, /* 2 */
HA_ALS_CAP, /* 3 */
HA_BACKLIGHT_CAP, /* 4 */
HA_BACKLIGHT_BIT, /* 5 */
HA_AOUSB_CAP, /* 6 */
HA_AOUSB_BIT, /* 7 */
HA_AUTODIS_CAP, /* 8 */
HA_FNLOCK_CAP, /* 9 */
HA_FNLOCK_BIT, /* 10 */
HA_PRIMEKEY_BIT, /* 11 */
HA_S0IL_CAP, /* 12 */
HA_ITS_BIT, /* 13 */
HA_AOUSBBAT_CAP, /* 14 */
HA_AOUSBBAT_BIT, /* 15 */
};

enum {
HACMD_BACKLIGHT_ON = 0x8,
HACMD_BACKLIGHT_OFF = 0x9,
HACMD_AOUSB_ON = 0xa,
HACMD_AOUSB_OFF = 0xb,
HACMD_FNLOCK_ON = 0xe,
HACMD_FNLOCK_OFF = 0xf,
HACMD_S0IL_OFF, /* 0 */
HACMD_S0IL_ON, /* 1 */
HACMD_COLOR_ENG_ON = 4,
HACMD_COLOR_ENG_OFF, /* 5 */
HACMD_ALS_OFF, /* 6 */
HACMD_ALS_ON, /* 7 */
HACMD_BACKLIGHT_ON, /* 8 */
HACMD_BACKLIGHT_OFF, /* 9 */
HACMD_AOUSB_ON, /* 10 */
HACMD_AOUSB_OFF, /* 11 */
HACMD_AUTOKT_ON, /* 12 */
HACMD_AUTOKT_OFF, /* 13 */
HACMD_FNLOCK_ON, /* 14 */
HACMD_FNLOCK_OFF, /* 15 */
HACMD_S0IL_ENTER, /* 16 */
HACMD_S0IL_EXIT, /* 17 */
HACMD_AOUSBBAT_OFF, /* 18 */
HACMD_AOUSBBAT_ON, /* 19 */
};

enum {
Expand Down Expand Up @@ -159,6 +227,11 @@ class IdeaVPC : public YogaVPC
*/
bool FnlockCap {false};

/**
* Rapid charge mode capability, will be update on init
*/
bool rapidChargeCap {false};

/**
* Rapid charge mode status
*/
Expand Down
10 changes: 5 additions & 5 deletions YogaSMCPane/Base.lproj/YogaSMCPane.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21219" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21506" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21219"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21506"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down Expand Up @@ -520,7 +520,7 @@
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="1qm-YF-8v2">
<rect key="frame" x="18" y="204" width="144" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Conservation Mode" bezelStyle="regularSquare" imagePosition="left" enabled="NO" state="on" inset="2" id="2fB-cH-iMt">
<buttonCell key="cell" type="check" title="Conservation Mode" bezelStyle="regularSquare" imagePosition="left" enabled="NO" inset="2" id="2fB-cH-iMt">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand All @@ -531,7 +531,7 @@
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="QxV-Zx-1xy">
<rect key="frame" x="18" y="174" width="146" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Rapid Charge Mode" bezelStyle="regularSquare" imagePosition="left" enabled="NO" state="on" inset="2" id="jXz-uU-Arn">
<buttonCell key="cell" type="check" title="Rapid Charge Mode" bezelStyle="regularSquare" imagePosition="left" enabled="NO" inset="2" id="jXz-uU-Arn">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand Down Expand Up @@ -617,7 +617,7 @@
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="MzN-Z3-hnK">
<rect key="frame" x="18" y="234" width="119" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Always On USB" bezelStyle="regularSquare" imagePosition="left" enabled="NO" state="on" inset="2" id="QwE-xk-acZ">
<buttonCell key="cell" type="check" title="Always On USB" bezelStyle="regularSquare" imagePosition="left" enabled="NO" inset="2" id="QwE-xk-acZ">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand Down
2 changes: 0 additions & 2 deletions YogaSMCPane/IdeaSMCPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ extension YogaSMCPane {

if let val = props["RapidChargeMode"] as? Bool {
vRapidChargeMode.state = val ? .on : .off
#if DEBUG
vRapidChargeMode.isEnabled = true
#endif
}

if let dict = props["Battery 0"] as? NSDictionary {
Expand Down

0 comments on commit 88955f7

Please sign in to comment.