maniacx / Battery-Health-Charging

Gnome extension to Set battery charging threshold / charging limit / charging mode Battery Health Charging: An extension to maximize the battery life of laptops by setting their charging threshold or modes.
https://extensions.gnome.org/extension/5724/battery-health-charging/
GNU General Public License v3.0
212 stars 19 forks source link

Feature Request: Support Chromebooks running coreboot to use Linux "MrChromebox firmware" #104

Open lmigfernandes opened 2 months ago

lmigfernandes commented 2 months ago

Would be nice to have EC Chromebook “Adaptive Charging” since newer Chromebooks that use the UEFI custom firmware to run other OS use coreboot. I talk about Arch Linux. I can try another distros if necessary. On Arch Linux, I use AUR "ectool for the Framework laptop” or the official repo source here. To write I use “sudo ectool chargecontrol normal 75 80”

I find the Normal command of EC on official Chromium OS documentation https://chromium.googlesource.com/chromiumos/platform2/+/HEAD/power_manager/docs/adaptive_charging.md and found additional information here.

To read information about battery status: [arc@arcbook ~]$ whereis ectool ectool: /usr/bin/ectool [arc@arcbook ~]$ sudo ectool chargecontrol Charge mode = NORMAL (0) Battery sustainer = on (75% ~ 80%) [arc@arcbook ~]$ cat /sys/devices/virtual/dmi/id/sys_vendor Google

@maniacx Tried my best to help and describe what u asked me, since English is not my primary language.

Thanks :)

lmigfernandes commented 2 months ago

Additional information for installing EC tool on Fedora. https://docs.chrultrabook.com/docs/installing/ectool.html

lmigfernandes commented 2 months ago

"ectool” is a standalone file, can run like a AppImage.

arc@arcbook:~/.local/bin> file ectool ectool: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=17f3683fce2eb6efc3b4cbc3484b20991a8b0630, for GNU/Linux 3.2.0, stripped

maniacx commented 2 months ago

Thank you @lmigfernandes

I will try to implement this end of this week.

I just want to confirm some information again. Give me the output of these 2 command.

sudo ectool chargecontrol normal 75 80
sudo ectool chargestate show
ls /sys/class/power_supply/BAT0
uname -r

Post output on a new line. Example. Do not post like this

[arc@arcbook ~]$ whereis ectool ectool: /usr/bin/ectool [arc@arcbook ~]$ sudo ectool chargecontrol Charge mode = NORMAL (0) Battery sustainer = on (75% ~ 80%) [arc@arcbook ~]$ cat /sys/devices/virtual/dmi/id/sys_vendor Google

Post like this

[arc@arcbook ~]$ whereis ectool 

 ectool: /usr/bin/ectool

It helps me get the exact string output by the command.

I understand translation is difficult, and I apologise asking for too much details.

lmigfernandes commented 2 months ago

Now I'm using openSUSE with standalone ectool binary.

arc@arcbook:~/.local/bin> sudo ./ectool chargecontrol normal 75 80
Charge state machine is in normal mode with sustainer enabled.
arc@arcbook:~/.local/bin> sudo ./ectool chargestate show
ac = 1
chg_voltage = 8384mV
chg_current = 0mA
chg_input_current = 2850mA
batt_state_of_charge = 93%
arc@arcbook:~/.local/bin> ls /sys/class/power_supply/BAT0
alarm               charge_now   manufacturer   status      voltage_min_design
capacity            current_now  model_name     subsystem   voltage_now
capacity_level      cycle_count  power          technology
charge_full         device       present        type
charge_full_design  hwmon0       serial_number  uevent\
arc@arcbook:~/.local/bin> uname -r
6.10.5-1-default

Thank you, @maniacx.

maniacx commented 2 months ago

@lmigfernandes

I am working on the extension.

It was difficult for me to find the source code, and I finally found here.

https://github.com/coreboot/chrome-ec/blob/main/util/ectool.cc

But I am still not sure if you are using the same one.

Kindly tell me which website did you download it from?

Also I want to know the range of acceptable threshold values , command and exit codes, so I have created a script that contains 9 command that will change your threshold every 1 seconds.

download and extract the zip. ectooltest.zip

open terminal.

navigate to download folder which contain the bash script.

change script to executable

chmod +x ectooltest.sh

and run the script

sudo ./ectooltest.sh

Allow the script to complete.

Post the output here.

lmigfernandes commented 2 months ago

@maniacx The ectool blob is specific for Chromebooks. Normally I compile from source on Arch Linux AUR but can be found the .rpm on fedora Terra repository here (normally I download the .rpm and extract installing dependencies).

The output from script u asked:

Running: ectool chargecontrol normal 75 80
Output: [Charge state machine is in normal mode with sustainer enabled.]
Exit code: [0]

Running: ectool chargecontrol
Output: [Charge mode = DISCHARGE (2)
Battery sustainer = on (75% ~ 80%)]
Exit code: [0]

Running: ectool chargecontrol normal 95 100
Output: [Charge state machine is in normal mode with sustainer enabled.]
Exit code: [0]

Running: ectool chargecontrol
Output: [Charge mode = NORMAL (0)
Battery sustainer = on (95% ~ 100%)]
Exit code: [0]

Running: ectool chargecontrol normal 98 100
Output: [Charge state machine is in normal mode with sustainer enabled.]
Exit code: [0]

Running: ectool chargecontrol
Output: [Charge mode = NORMAL (0)
Battery sustainer = on (98% ~ 100%)]
Exit code: [0]

Running: ectool chargecontrol normal 55 60
Output: [Charge state machine is in normal mode with sustainer enabled.]
Exit code: [0]

Running: ectool chargecontrol
Output: [Charge mode = DISCHARGE (2)
Battery sustainer = on (55% ~ 60%)]
Exit code: [0]

Running: ectool chargecontrol normal 50 55
Output: [Charge state machine is in normal mode with sustainer enabled.]
Exit code: [0]

Running: ectool chargecontrol
Output: [Charge mode = DISCHARGE (2)
Battery sustainer = on (50% ~ 55%)]
Exit code: [0]

Running: ectool chargecontrol normal 40 50
Output: [Charge state machine is in normal mode with sustainer enabled.]
Exit code: [0]

Running: ectool chargecontrol
Output: [Charge mode = DISCHARGE (2)
Battery sustainer = on (40% ~ 50%)]
Exit code: [0]

Running: ectool chargecontrol normal 40 42
Output: [Charge state machine is in normal mode with sustainer enabled.]
Exit code: [0]

Running: ectool chargecontrol
Output: [Charge mode = DISCHARGE (2)
Battery sustainer = on (40% ~ 42%)]
Exit code: [0]

Running: ectool chargecontrol normal -1 -1
Output: [ectool: invalid option -- '1'
ectool: invalid option -- '1'
Usage: ectool [--dev=n] [--interface=dev|i2c|lpc] [--i2c_bus=n] [--device=vid:pid] [--name=cros_ec|cros_fp|cros_pd|cros_scp|cros_ish] [--ascii] <command> [params]

  --i2c_bus=n  Specifies the number of an I2C bus to use. For
               example, to use /dev/i2c-7, pass --i2c_bus=7.
               Implies --interface=i2c.

  --interface Specifies the interface.

  --device    Specifies USB endpoint by vendor ID and product
              ID (e.g. 18d1:5022).

Use 'ectool help' to print a list of commands.]
Exit code: [1]

Running: ectool chargecontrol
Output: [Charge mode = DISCHARGE (2)
Battery sustainer = on (40% ~ 42%)]
Exit code: [0]

Running: ectool chargecontrol normal 75 80
Output: [Charge state machine is in normal mode with sustainer enabled.]
Exit code: [0]

Running: ectool chargecontrol
Output: [Charge mode = DISCHARGE (2)
Battery sustainer = on (75% ~ 80%)]
Exit code: [0]

Thank you.

maniacx commented 2 months ago

@lmigfernandes

You can try the GNOME45 branch and let me if there are some issues.

https://github.com/maniacx/Battery-Health-Charging/commits/GNOME45/

lmigfernandes commented 2 months ago

@maniacx For now looks all is working :). In addition, I ask for feedback on Linux “Chromebook” forum.

@maniacx Again thanks you for all your work.

With love, Luís

lmigfernandes commented 1 month ago

Hello, @maniacx How this Linux kernel patch will change the Chromebook ectool blob method. Keep in mind, old Linux kernel's distributions like Debian still need to wait 1/2 years to get 6.11 release. As soon Arch gets 6.11 I'm open to any additional help.

Thank you.

maniacx commented 1 month ago

Yes, I saw the patch when I was finding out about ectools. sysfs is already supported.

https://github.com/maniacx/Battery-Health-Charging/blob/GNOME45/devices/Chromebook.js#L9-L12

If ectool is installed and it runs older kernel, extension will use the ectool. If ectool is not installed and it runs newer kernel to support charging threshold(sysfs) , it will use sysfs.

If running ectool and new kernel, you will get an option to choose what for changing threshold.

image

Althought I didnt add feature for charge_behavior. I think it is something that never be used or will be used once in a while, for battery calibration, if battery shows wrong value. In that case user will have to calibrate the battery using command line, but it is quite rare that happens.

lmigfernandes commented 1 month ago

Hey @maniacx ,

Looks like sysfs mode is working. I updated my Coreboot firmware and Linux Kernel. Thank you.

maniacx commented 1 month ago

Thank you for the update. I just want fix some minor issues, and release this version, but too busy. Give me two week, hopefully once the extension is finalize, I will request you to test again and release it on Gnome extensions.