Skip to content

Commit

Permalink
chore: linux driver
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyc committed Apr 9, 2024
1 parent cabccf7 commit c2edf22
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
16 changes: 16 additions & 0 deletions pisugar-module/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 26 additions & 4 deletions pisugar-module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions pisugar-module/pisugar_3_battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ static int pisugar_3_monitor(void *args)
}
}
sleep:
set_current_state(TASK_RUNNING);
schedule_timeout(HZ);
}

Expand Down

0 comments on commit c2edf22

Please sign in to comment.