20 设备树里声明i2c设备

时间:2021-02-10 16:53:57

参考内核源码目录里的Documentation/i2c/instantiating-devices
文档里主要的内容:

Method 1b: Declare the I2C devices via devicetree
-------------------------------------------------

This method has the same implications as method 1a. The declaration of I2C
devices is here done via devicetree as subnodes of the master controller.

Example:

i2c1: i2c@400a0000 {
/* ... master properties skipped ... */
clock-frequency = <100000>;

flash@50 {
compatible = "atmel,24c256";
reg = <0x50>;
};

pca9532: gpio@60 {
compatible = "nxp,pca9532";
gpio-controller;
#gpio-cells = <2>;
reg = <0x60>;
};
};

通过文档里的内容, 可以得知: i2c设备节点需要作为i2c控制器节点的子节点.
i2c设备节点的compatible属性值除了用于与设备驱动匹配以外,还作为设备名.
reg属性值为设备地址.也可以在设备节点里加入额外的属性.


如dht12接在第0个i2c控制器上,则修改内核源码目录arch/arm64/boot/dts/allwinner/sunxi-h3-h5.dts:

        i2c0: i2c@01c2ac00 {  /* 控制器的设备节点 */
compatible = "allwinner,sun6i-a31-i2c";
reg = <0x01c2ac00 0x400>;
interrupts = <GIC_SPI 6 IRQ_TYPE_LEVEL_HIGH>;
clocks = <&ccu CLK_BUS_I2C0>;
resets = <&ccu RST_BUS_I2C0>;
pinctrl-names = "default";
pinctrl-0 = <&i2c0_pins>;
status = "disabled";
#address-cells = <1>;
#size-cells = <0>;

mydht12 { /* 增加的设备子节点 */
compatible = "mydht12";
reg = <0x5c>; /* 设备地址 */

/* 额外增加的属性,供i2c设备驱动使用 */
led-gpios = <&pio 0 17 GPIO_ACTIVE_HIGH>;
};

};

重编设备树并更新,重启系统后:

^_^ / # ls /sys/bus/i2c/devices/
0-005c/ 0-0068/ i2c-0/ i2c-1/ i2c-2/
^_^ / # cat /sys/bus/i2c/devices/0-005c/name
mydht12