Skip to content

Instantly share code, notes, and snippets.

@m-roberts
Last active March 11, 2022 12:21
Show Gist options
  • Save m-roberts/b860e08a945128ce96eb95ef12b126a5 to your computer and use it in GitHub Desktop.
Save m-roberts/b860e08a945128ce96eb95ef12b126a5 to your computer and use it in GitHub Desktop.
Understanding and working with I2C driver with sysfs interface
0x68 device address is defined in the device tree: https://github.com/vuorioi/amg88xx-kernel-driver/blob/master/amg88xx-overlay.dts
dmesg output when not present:
# [ 66.832427] amg88xx: probe of 1-0068 failed with error -121
Therefore, without the device connected, the sysfs paths are not loaded
Kernel docs:
* https://www.kernel.org/doc/html/latest/i2c/writing-clients.html
e.g. note: this explains how "i2c_rdwr_ioctl_data" can be used for combined read/write transaction without stop in between
Other kernel module projects:
* https://github.com/akoskovacs/kmod_example
* https://github.com/tejasatgithub/Linux-i2c-client-driver
* https://github.com/greendrm/pb206x-i2c
* https://github.com/vpcola/chip_i2c
# Install kernel headers
sudo apt-get install -y raspberrypi-kernel-headers
# Upgrade system (install latest kernel, etc.)
sudo apt upgrade -y
# Reboot
git clone https://github.com/vuorioi/amg88xx-kernel-driver
cd amg88xx-kernel-driver
# Create device tree overlay and load
dtc -W no-unit_address_vs_reg -O dtb -I dts -o amg88xx.dtbo amg88xx-overlay.dts
sudo dtoverlay amg88xx.dtbo
# Build kernel module and load
make
sudo insmod amg88xx.ko
# Initialise
sysfs_root="/sys/bus/i2c/devices/$(grep "amg88xx$" /sys/bus/i2c/devices/*/name | cut -d'/' -f6)"
sudo sh -c "echo normal > ${sysfs_root}/device_mode"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment