From c2edf2220361d5683f7b256bce54918336d98835 Mon Sep 17 00:00:00 2001 From: fengyc Date: Tue, 9 Apr 2024 10:38:43 +0800 Subject: [PATCH] chore: linux driver --- pisugar-module/Makefile | 16 ++++++++++++++++ pisugar-module/README.md | 30 ++++++++++++++++++++++++++---- pisugar-module/pisugar_3_battery.c | 1 + 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/pisugar-module/Makefile b/pisugar-module/Makefile index b6b59c3..ee9d9cd 100644 --- a/pisugar-module/Makefile +++ b/pisugar-module/Makefile @@ -7,8 +7,24 @@ KERN_VER = $(shell uname -r) SOURCEDIR = $(shell pwd) BUILDDIR = $(shell pwd)/build +I2C_BUS ?= 0x01 +I2C_ADDR ?= 0x57 + all: make -C /lib/modules/$(KERN_VER)/build M=$(SOURCEDIR) modules clean: rm -f *.cmd *.ko *.o Module.symvers modules.order *.mod.c .pisugar_3_battery.* .modules.* .Module.symvers.* pisugar_3_battery.mod + +install: all + cp -f pisugar_3_battery.ko /lib/modules/$(KERN_VER)/kernel/drivers/power/supply + grep -q pisugar_3_battery /etc/modules || sed -i '$$a pisugar_3_battery' /etc/modules + echo "options pisugar_3_battery i2c_bus=$(I2C_BUS) i2c_addr=$(I2C_ADDR)" > /etc/modprobe.d/pisugar_3_battery.conf + depmod -a + modprobe pisugar_3_battery + +uninstall: + rmmod pisugar_3_battery || true + rm -f /lib/modules/$(KERN_VER)/kernel/drivers/power/supply/pisugar_3_battery.ko + sed -i '/pisugar_3_battery/d' /etc/modules + rm -f /etc/modprobe.d/pisugar_3_battery.conf diff --git a/pisugar-module/README.md b/pisugar-module/README.md index d7278ec..38a3ce7 100644 --- a/pisugar-module/README.md +++ b/pisugar-module/README.md @@ -21,13 +21,30 @@ As kernel symbols is not included in Raspberry Pi OS (no `/lib/modules/$(uname - To build the kernel, see official doc: https://www.raspberrypi.com/documentation/computers/linux_kernel.html -## Compiling kernel module +## Compile/install/uninstall kernel module Clone this repository, make kernel modules: ```shell make ``` +Install: +```shell +sudo make install +``` + +Install with parameters: +```shell +sudo make install i2c_bus=0x01 i2c_addr=0x57 +``` + +Uninstall: +```shell +sudo make uninstall +``` + +## Manually load kernel module + Load module: ```shell sudo insmod pisugar_3_battery.ko @@ -50,15 +67,20 @@ Remove module: sudo rmmod pisugar_3_battery.ko ``` -Now, you can enable [battery monitor plugin](https://github.com/raspberrypi-ui/lxplug-ptbatt). +Now, you can enable a battery monitor plugin that reads battery status from power supply subsystem (OS battery monitor plugin or a 3rd party plugin). -If you want to load kernel module at boot time, copy it to `/lib/modules/$(uname -r)/kernel/drivers` +If you want to load kernel module at boot time, copy it to `/lib/modules/$(uname -r)/kernel/drivers/power/supply` ```shell -sudo cp -f pisugar_3_battery.ko /lib/modules/$(uname -r)/kernel/drivers +sudo cp -f pisugar_3_battery.ko /lib/modules/$(uname -r)/kernel/drivers/power/supply sudo echo pisugar_3_battery >> /etc/modules sudo depmod -a ``` +You may want to change module parameters: +```shell +echo "options pisugar_3_battery i2c_bus=0x01 i2c_addr=0x57" | sudo tee /etc/modprobe.d/pisugar_3_battery.conf +``` + ## License GPL \ No newline at end of file diff --git a/pisugar-module/pisugar_3_battery.c b/pisugar-module/pisugar_3_battery.c index 2c222c0..78990f6 100644 --- a/pisugar-module/pisugar_3_battery.c +++ b/pisugar-module/pisugar_3_battery.c @@ -324,6 +324,7 @@ static int pisugar_3_monitor(void *args) } } sleep: + set_current_state(TASK_RUNNING); schedule_timeout(HZ); }