在写linux 驱动中,使用dts/dtsi 会发现在填充 struct i2c_driver 与 struct platform_driver 是不一样,
struct i2c_driver 需要id_match_table,同时需要id_table,而struct platform_driver 只要id_match_table,不需要id_table。
来一探究竟吧
i2c_add_driver()
└── i2c_register_driver
└── driver_register
├── driver_find
│ ├── kset_find_obj
│ ├── kobject_put
│ └── to_driver
└── bus_add_driver
└── driver_attach
└── bus_for_each_dev
├── next_device
└── __driver_attach
└─ driver_match_device
└── i2c_device_match
├── acpi_driver_match_device
├── i2c_match_id
└── of_driver_match_device
└── of_match_device
└── of_match_node
└── __of_match_node
└── __of_device_is_compatible
78 static int i2c_device_match(struct device *dev, struct device_driver *drv)
79 {
80 struct i2c_client *client = i2c_verify_client(dev);
81 struct i2c_driver *driver;
82
83 if (!client)
84 return 0;
85
86 /* Attempt an OF style match */
87 if (of_driver_match_device(dev, drv))
88 return 1;
89
90 /* Then ACPI style match */
91 if (acpi_driver_match_device(dev, drv))
92 return 1;
93
94 driver = to_i2c_driver(drv);
95 /* match on an id table if there is one */
96 if (driver->id_table)
97 return i2c_match_id(driver->id_table, client) != NULL;
98
99 return 0;
100 }
从i2c_device_match的定义可以看出, 和platform总线一样, i2c的match函数也是优先选择设备树, 如果设备树已经匹配了, 函数就会返回, 不会再最平台文件的设备信息进行判断, 即不会理会id_table的值, 确保匹配是一定不需要id_table了,而事实上probe确实没有执行,那么问题就可能出现在probe的回调过程了,和所有的总线设备一样,回调probe的过程始于driver_match_id,于是又是一路狂追,终于在i2c_device_probe()中找到了我臆想中的对id_table的检测
i2c_add_driver()
└── i2c_register_driver
└── driver_register
├── driver_find
│ ├── kset_find_obj
│ ├── kobject_put
│ └── to_driver
└── bus_add_driver
└── driver_attach
└── bus_for_each_dev
├── next_device
└── __driver_attach
├── driver_match_device
└── driver_probe_device
└── really_probe
└── i2c_device_probe
└── i2c_match_id
所以,结论是:即使使用设备树来匹配,也要对id_table进行有效的赋值,否则probe不会被回调!!!如果你也遇到了这个问题, 可以试试在驱动中定义一个空数组, 将其赋值给id_table