As of June 2016 Raspbian already has the necessary modifications. You only need to enable the overlay in config.txt (step 6 onwards).
I finally got this working as follows:
- Rebuild the kernel with i2c_gpio module.
You must edit the bcm2708_defconfig or bcmrpi_defconfig and add the line
CONFIG_I2C_GPIO=m before running the make defconfig.
- Install kernel and modules using mkknlimg for device tree support as described in the kernel building docs.
Convert the example i2c_gpio config to a device tree fragment as follows:
// Overlay for i2c_gpio bitbanging host bus.
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2708";
fragment@0 {
target-path = "/";
__overlay__ {
i2c_gpio: i2c@0 {
compatible = "i2c-gpio";
gpios = <&gpio 23 0 /* sda */
&gpio 24 0 /* scl */
>;
i2c-gpio,delay-us = <2>; /* ~100 kHz */
#address-cells = <1>;
#size-cells = <0>;
};
};
};
__overrides__ {
i2c_gpio_sda = <&i2c_gpio>,"gpios:4";
i2c_gpio_scl = <&i2c_gpio>,"gpios:16";
i2c_gpio_delay_us = <&i2c_gpio>,"i2c-gpio,delay-us:0";
};
};
- This will default to using gpio 23 and 24 for software i2c.
- delay-us should be 2 for 100kHz operation.
- The following lines should be removed because they will prevent the driver from working on Raspberry Pi:
i2c-gpio,sda-open-drain;
i2c-gpio,scl-open-drain;
Build the device tree blob with dtc -@ -I dts -O dtb -o i2c-gpio.dtb i2c-gpio.dts
Copy the blob to /boot/overlays/
Add a line in /boot/config.txt dtoverlay=i2c-gpio
If you want to use different pins, put dtoverlay=i2c-gpio,i2c_gpio_sda=<pin>,i2c_gpio_scl=<pin> instead. You can also change the rate with i2c_gpio_delay_us=<usecs>.
- Reboot.
modprobe i2c-dev and you now should have /dev/i2c-3 in addition to any others you previously configured.